I\'m creating transparent text -> png images with php and so far so good. The only problem is that I want the ability to have the text word wrap due to a fixed width.. Or alter
Simply explode the text on spaces to get an array of words, then start building lines by looping through the words array, testing the addition of each new word via imagettfbbox to see if it creates a width that exceeds the maxwidth you set. If it does, start the next word on a fresh new line. I find it easier to simply create a new string with special line breaks characters added, and then just explode that string again to create an array of lines, each of which you will write onto the final image separately.
Something like this:
$words = explode(" ",$text);
$wnum = count($words);
$line = '';
$text='';
for($i=0; $i<$wnum; $i++){
$line .= $words[$i];
$dimensions = imagettfbbox($font_size, 0, $font_file, $line);
$lineWidth = $dimensions[2] - $dimensions[0];
if ($lineWidth > $maxwidth) {
$text.=($text != '' ? '|'.$words[$i].' ' : $words[$i].' ');
$line = $words[$i].' ';
}
else {
$text.=$words[$i].' ';
$line.=' ';
}
}
Where the pipe character is the line break character.