views:

254

answers:

1

Hello

I'm creating an dynamic image, which creates headers on my page using PHPs GD-library. The problem is, that I need a line-wrapping system. It's not a problem itself, but first I need to get the width (in pixels) of current character.

I'm pretty curious about this, is there any way? Or do I need to manually specify width of every abc?

Martti Laine

+1  A: 

You would have to do a imagettfbbox() on each single character.

Untested but should work:

$string = "Lorem Ipsum"
$size = 20;
$angle = 0;
$fontfile = "ARIAL.TTF";

for ($i = 0; $i <= strlen($string); $i++)
 {
    $dimensions = imagettfbbox($size, $angle, $fontfile, $string[$i]);
    echo "Width of ".$string[$i]." is ".$dimensions[2]."<br>";

 }
Pekka
Thanks for this fast answer!
Martti Laine
@Martti you're welcome. If you can use it, also consider ImageMagick: http://www.imagemagick.org/Usage/text/#caption It can do word wrap out of the box and handles TTF text much, much better, but needs to be installed and accessible on the server.
Pekka
Okay, I'll check that out also.
Martti Laine