note 29770 deleted from function.imagettfbbox by didou
| From: | didou@php.net | Date: | Fri, 15 Aug 2003 22:13:52 +0000 |
| Subject: | note 29770 deleted from function.imagettfbbox by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-54273@lists.php.net to get a copy of this message | ||
Note Submitter: nprice@tesseract.NoSpam.net
----
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.