note 27850 added to function.imagettfbbox

From: Date: Sat, 21 Dec 2002 00:53:39 +0000
Subject: note 27850 added to function.imagettfbbox
Groups: php.notes 
Request: Send a blank email to php-notes+get-41165@lists.php.net to get a copy of this message
It took me a while to figure out what was going on with the bounding box. The hardest part is basically getting the width and height for your entire string rendered in a given font: $bbox = Imagettfbbox($fontsize,0,$font,$string) ; $width = ( ($bbox[0] > 0 && $bbox[2] > 0) || ($bbox[0] < 0 && $bbox[2] < 0) ) ? abs($bbox[2] - $bbox[0]) : ( abs($bbox[2]) + abs($bbox[0]) ); $height = ( ($bbox[1] > 0 && $bbox[5] > 0) || ($bbox[1] < 0 && $bbox[5] < 0) ) ? abs($bbox[1] - $bbox[5]) : ( abs($bbox[1]) + abs($bbox[5]) ); The trick is that some text will render totally above the horizontal axis, some will render totally bellow it and some will render on both sides of it (same with the vertical axis). The two functions listed above will give you the width and height in every possibly case (given that your angle is set to 0). After that, finding the lower left corner to draw your text to an image using the ImageTTFText() is as simple as: $X = (imagesx($image)-$width)/2; $Y = ((imagesy($image)-$height)/2)+$height; -- http://www.php.net/manual/en/function.imagettfbbox.php http://master.php.net/manage/user-notes.php?action=edit+27850 http://master.php.net/manage/user-notes.php?action=delete+27850 http://master.php.net/manage/user-notes.php?action=reject+27850

« previous php.notes (#41165) next »