php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37730 ImageTTFText & ImageTTFBBox don't give accurate rectangle with angle
Submitted: 2006-06-07 15:28 UTC Modified: 2009-03-18 08:42 UTC
Votes:12
Avg. Score:4.4 ± 0.8
Reproduced:12 of 12 (100.0%)
Same Version:1 (8.3%)
Same OS:3 (25.0%)
From: marc dot lazzaro at st dot com Assigned: pajoye (profile)
Status: Not a bug Package: GD related
PHP Version: 5.1.4 OS: Win XP
Private report: No CVE-ID: None
 [2006-06-07 15:28 UTC] marc dot lazzaro at st dot com
Description:
------------
Hello,
ImageTTFText & ImageTTFBBox don't give accurate rectangle with angle other than 0.
I work on a png image.

Reproduce code:
---------------
$xx = 30;$yy = 200;$tsize = 18;$tangle = 45;
$p2 = ImageTTFText ($img, $tsize, $tangle, $xx, $yy, 'blue','C:/Windows/Fonts/arial.ttf',"MAgjpabiT01234567"); 
$p3 = @ImageTTFBBox($tsize, $tangle,'C:/Windows/Fonts/arial.ttf',"MAgjpabiT01234567"); 
for($i=0; $i < 4; ++$i) {
  $p3[$i*2] = round($p3[$i*2]+$xx+0.5);
  $p3[$i*2+1] = round($p3[$i*2+1]+$yy+0.5);
}
imageline($img,$p2[0],$p2[1],$p2[2],$p2[3],'red');
imageline($img,$p2[2],$p2[3],$p2[4],$p2[5],'red');
imageline($img,$p2[4],$p2[5],$p2[6],$p2[7],'red');
imageline($img,$p2[6],$p2[7],$p2[0],$p2[1],'red');

Expected result:
----------------
I would expext to draw a rectangle fitting exactly around the drawn text. This is true when angle is 0 but no more when rotating the text. Looks like the surrounding box shift on the left versus the text when angle increase.

Actual result:
--------------
I only have png image to show. If needed you can contact me by email.

Thanks for all your efforts in developping PHP.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-07 16:44 UTC] pajoye@php.net
Please provide a link to the font you use. Don't use '@' to hide possible errors or notices.
 [2006-06-08 06:57 UTC] marc dot lazzaro at st dot com
My company policy forbides external access. If you could provide an email address I will send you the font file. But I have to say that the behavior remains the same whatever the font is.
Also pls note that I got no errors when the @ is removed.
 [2007-01-15 12:45 UTC] pajoye@php.net
mail to pierre[at]libgd.org.
 [2007-01-23 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2007-02-27 15:10 UTC] hopka at hopka dot net
I have the same problem with PHP 5.1.2 on Linux and 5.2.0 on Windows XP. I use imagepolygon to render the bounding box and with a rotation of 0 degrees (or 360, 720, etc) it fits around the text perfectly. As soon as I set a different angle, the bounding box is too large and at the wrong position.

I made a little demonstration script (includes source + phpinfo): http://hopka.net/imagettfbbox.php5
 [2007-03-01 00:14 UTC] pajoye@php.net
"I made a little demonstration script (includes source + phpinfo):
http://hopka.net/imagettfbbox.php5"

Your script is wrong, you forgot that each position is relative to the origin of the text. Adding 100 to each of them is not correct. You have to rotate the offset.
 [2007-03-01 11:40 UTC] hopka at hopka dot net
If i draw the box that imagettftext (sorry, did not try that before) returns without modifying the coordinates, it is almost (1 pixel off) the same as the box i get after adding 100 to each coordinate of imagettfbbox' return value. That would mean that either imagettftext is also wrong (because it produces the same results as my script) or that one would also have to modify the coordinates returned by imagettftext.
Also, the rectangles returned by both functions are already rotated, so i don't see how i should rotate the offset to get the correct bounding box.
 [2007-03-01 11:45 UTC] pajoye@php.net
"so i don't see how i should rotate the offset to get the correct bounding box."

Your script is wrong, you add 100 to each vertex, no matter where they are. The result rectangle is then drawn at the wrong place.

About the offset pixel, what I asked here remains. Please provide the font as well.
 [2007-03-01 12:00 UTC] hopka at hopka dot net
The font is standard Arial, the ttf i use is here: http://hopka.net/Arial.ttf
However, i did try other fonts and i could not find any font that produced different results.
 [2007-03-09 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2007-09-28 00:53 UTC] lbayuk at pobox dot com
I am also finding that the TrueType font bounding box from ImageTTFBBox and ImageTTFText seems to be wrong for text at angles other than 0 degrees.  Using PHP-5.2.4 on Linux.  My test script just draws the returned bounding box for text and angles from 0 to 360. It shows that for text at 0 degrees, the box encloses the text, but for all other angles the box is off by different amounts. The worst case is for text between 180 and 270 degrees, where the text is completely outside the bounding box.

My test script is about 50 lines long, and I would be happy to post or mail it but I'm not sure what is correct. Let's not abandon this bug report until we figure out whether PHP is at fault or we are using it wrong.
 [2009-03-18 08:42 UTC] pajoye@php.net
see #43073.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 20 03:01:29 2024 UTC