note 29770 added to function.imagettfbbox
| From: | nprice at tesseract dot NoSpam dot net | Date: | Mon, 24 Feb 2003 19:55:09 +0000 |
| Subject: | note 29770 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-44446@lists.php.net to get a copy of this message | ||
In the code provided by skhanna at csc dot tntech dot edu to find the height and width of the
bounding-box returned by imagettfbox() I found a fencepost error while running the resulting graphic
through my image editor. Thus, the code should be:
$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]) +1 );
$height = ( ($bbox[1] > 0 && $bbox[5] > 0) || ($bbox[1] < 0 && $bbox[5]
< 0) ) ? abs($bbox[1] - $bbox[5]) : ( abs($bbox[1]) + abs($bbox[5]) +1 );
Why? An example: Lets say that to find the height, we use the lower left Y coordinate and the upper
right Y coordinate. Lets say that imagegettfbbox() returns the following values:
$bbox[1] => 2 #lower left Y coord.
$bbox[5] => -15 #upper right Y coord.
That means that from the baseline, it extends 2 pixels below, and 15 pixels above it, right? Wrong.
It actually extends 3 pixels below (or 16 above, depending how you want to look at it), because the
math above fails to account for the '0th' pixel at Y-coord 0. At least, that's what I
observed. So, if you're wondering why your text gets clipped or positioned wrong by 1 pixel,
hopefully this will give you a clue.
--
http://www.php.net/manual/en/function.imagettfbbox.php
http://master.php.net/manage/user-notes.php?action=edit+29770
http://master.php.net/manage/user-notes.php?action=delete+29770
http://master.php.net/manage/user-notes.php?action=reject+29770