imagettfbox issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

imagettfbox issue

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: imagettfbox issue

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 7:08 pm, edited 1 time in total.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: imagettfbox issue

Post by SidewinderX »

I am taking the absolute value. The issue was with imagettftext - that link helped. Thanks.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re

Post by McInfo »

[...]
Post Reply