note 97211 added to function.imagettfbbox
| From: | peterjwest3 at gmail dot com | Date: | Thu, 08 Apr 2010 13:18:04 +0000 |
| Subject: | note 97211 added to function.imagettfbbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168013@lists.php.net to get a copy of this message | ||
As many of you know, this function is bugged in several versions of PHP. It should return the
coordinates relative to the baseline of the font. So if your text includes characters like g and p
then the bounding box should extend below zero on the Y axis, however it doesn't. This is a
problem because imagettftext() positions text using the baseline, so all your text will be
misaligned.
My solution is to create an image of the desired font and font-size using all ascii characters with
imagettfbbox() and imagettftext(). The height of this image is used as the height for the real
image.
I then analyse the image to get the vertical offset of the text (the background color should be
$baseColor)
<?php
function getYOffset($image, $baseColor) {
for($y = 0; $y < $this->height(); $y++)
for($x = 0; $x < $this->width(); $x++)
if (imagecolorat($image, $x, $y) !== $baseColor)
return $y; }
?>
This offset can be used as the baseline for the font (for this font-size). You can use a similar
trick for the horizontal offset, but that changes depending on the first character.
----
Server IP: 69.147.83.197
Probable Submitter: 84.13.60.134
----
Manual Page -- http://www.php.net/manual/en/function.imagettfbbox.php
Edit -- https://master.php.net/note/edit/97211
Del: integrated -- https://master.php.net/note/delete/97211/integrated
Del: useless -- https://master.php.net/note/delete/97211/useless
Del: bad code -- https://master.php.net/note/delete/97211/bad+code
Del: spam -- https://master.php.net/note/delete/97211/spam
Del: non-english -- https://master.php.net/note/delete/97211/non-english
Del: in docs -- https://master.php.net/note/delete/97211/in+docs
Del: other reasons-- https://master.php.net/note/delete/97211
Reject -- https://master.php.net/note/reject/97211
Search -- https://master.php.net/manage/user-notes.php