Page 1 of 1

imagettfbox issue

Posted: Fri Aug 21, 2009 7:34 pm
by SidewinderX
I am using GD to create an image with text. Since the font, font size, and amount of text can vary, the idea was to use imagettfbox to determine the width and height of the text. Then use those values as the parameters for imagecreatetruecolor. However, for some reason, the $width and $height seems to be incorrect as the text is getting cut off.

Code: Select all

 //...
$bbox = imagettfbbox($size, 0, $font, $xml->channel->item->title[0]);
 
$width = abs($bbox[2]-$bbox[0]);
$height = abs($bbox[7]-$bbox[1]);
 
$im = imagecreatetruecolor($width, $height);
$bg = imagecolorallocatealpha($im, 255, 255, 255, 127);
$txt = imagecolorallocate($im, 0, 0, 0); 
imagesavealpha($im, true);
imagefill($im, 0, 0 , $bg);
imagettftext($im, $size, 0, 10, 20, $txt, $font, $xml->channel->item->title[0]);
imagepng($im);
imagedestroy($im);
Is there something I'm not understanding?

Thanks,
John

Re: imagettfbox issue

Posted: Fri Aug 21, 2009 7:48 pm
by McInfo
Images are created top-to-bottom, left-to-right; so the top-left coordinate is (0, 0). When your script calculates the text box height, it subtracts the lower-left corner of the text box from the upper-left corner, which <false>will result in a negative number</false>.

Related post: 544649

Edit: This post was recovered from search engine cache.

Re: imagettfbox issue

Posted: Fri Aug 21, 2009 7:59 pm
by SidewinderX
I am taking the absolute value. The issue was with imagettftext - that link helped. Thanks.

Re

Posted: Fri Aug 21, 2009 8:08 pm
by McInfo
[...]