It does work with ttf files if you're using GD2, you just need to put the absolute path to the ttf (I've experienced problems when refering to a relative path).
Thas algorithm first separated all word in the string and put them into an array (splitting the spaces). After that we rejoin the whole array into a new variable ($out1) and check the length. If it's too long, we decrement $split (which is the last piece of the array we will join into $out1). This means thatwe will try each time one word less until it no longer isbigger than maxlen.
Once this done, we keep $out1 and rejoin the rest of the array into $out2.
Having $out2, you can repeat the process as if it was the original text: split, join decrementally until it fits, join the rest into a new variable $out3. And so on
code:
// Keystorms Improvemend by slicing
$slices = explode(' ',$textRegel);
$split = count($slices);
do{
$split--;
$out1 = NULL;
for($i=0;$i<=$split;$i++)
{
$out1 .= $slices[$i]." ";
}
$break = imagettfbbox($size,$angle,$fontpath,$out1);
} while ($break[2] > $maxlen);
$out2 = NULL;
for($j=$i; $j<count($slices); $j++)
{
$out2 .= $slices[$j].' ';
}
$slices = explode(' ',$out2);
$split = count($slices);
do{
$split--;
$out2 = NULL;
for($i=0;$i<=$split;$i++)
{
$out2 .= $slices[$i]." ";
}
$break = imagettfbbox($size,$angle,$fontpath,$out2);
} while ($break[2] > $maxlen);
$out3 = NULL;
for($j=$i; $j<count($slices); $j++)
{
$out3 .= $slices[$j].' ';
}
...
Edit: I forgot to remind you about the height of the image. Check whether $out2, $out3 and $out4 are filled with something (strlen($outX) > 0, for example) and decide the height from those checks.