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
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";
$strlen = strlen($string);
for ($i = 0; $i < $strlen; $i++)
{
$dimensions = imagettfbbox($size, $angle, $fontfile, $string[$i]);
echo "Width of ".$string[$i]." is ".$dimensions[2]."<br>";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With