1
votes

I used PHP and the GD Library to make a code that receives a string as an input and it breaks it in lines so it can fit inside an image. The problem is that depending on what text I type, it stops at a random point. For example, using the following text as an input:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

the output image is like that: enter image description here

My code is this:

<?php
function createStory($content){
    $text = $content;
    $jpg_image = imagecreatefromjpeg('imagebuilder/footage/story1.jpg');
    $white = imagecolorallocate($jpg_image, 255, 255, 255);
    $font_path = 'Arial.ttf';
    $words = explode(" ",$text);
    $proccessedtext = "";
    $line = "";
    $line .= $words[0] . " ";
    for($i = 1; $i < count($words); $i++){
        $bbox = imagettfbbox(25, 0, $font_path, $line);
        $width = $bbox[4]-$bbox[0];
        if($width<700){
            $line .= $words[$i] . " ";
        }else{
            $proccessedtext .= $line . " \n".$words[$i]. " ";
            $line = "";
        }
    }
    imagettftext($jpg_image, 25, 0, 75, 600, $white, $font_path, $proccessedtext);
    imagejpeg($jpg_image, "imagebuilder/created/readyStory.jpg");
    imagedestroy($jpg_image);
    return("/imagebuilder/created/readyStory.jpg");
}
?>

Are there any mistakes in my code or is it a bug in the library?

1

1 Answers

1
votes

Easy enough: notice that $processedText doesn't receive the contents of $line until you exceed the maximum width! So at any given time, it only ever receives a full line plus the one word that overflows. Thus, if you have remaining text that doesn't exceed the current line by one additional word, then there is a remainder that still need to be processed. Try adding $processedText .= $line; directly after the for loop:

<?php
function createStory($content){
    $text = $content;
    $jpg_image = imagecreatefromjpeg('imagebuilder/footage/story1.jpg');
    $white = imagecolorallocate($jpg_image, 255, 255, 255);
    $font_path = 'Arial.ttf';
    $words = explode(" ",$text);
    $proccessedtext = "";
    $line = "";
    $line .= $words[0] . " ";
    for($i = 1; $i < count($words); $i++){
        $bbox = imagettfbbox(25, 0, $font_path, $line);
        $width = $bbox[4]-$bbox[0];
        if($width<700){
            $line .= $words[$i] . " ";
        }else{
            $proccessedtext .= $line . " \n".$words[$i]. " ";
            $line = "";
        }
    }
    $processedText .= $line;
    imagettftext($jpg_image, 25, 0, 75, 600, $white, $font_path, $proccessedtext);
    imagejpeg($jpg_image, "imagebuilder/created/readyStory.jpg");
    imagedestroy($jpg_image);
    return("/imagebuilder/created/readyStory.jpg");
}
?>