|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43073 TrueType bounding box is wrong for angle<>0
Submitted: 2007-10-23 00:18 UTC Modified: 2009-05-06 08:00 UTC
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:0 (0.0%)
From: lbayuk at pobox dot com Assigned: tabe
Status: Closed Package: GD related
PHP Version: 5.2CVS-2008-11-02 OS: Linux
Private report: No CVE-ID:
 [2007-10-23 00:18 UTC] lbayuk at pobox dot com
The bounding box returned from imagettfbbox() and imagettftext() is incorrect for anything except horizontal text at 0 degrees. For all other angles, the text is partially or completely outside the returned bounding box. (Yes, somebody reported the same problem in #37730, but that was marked "no feedback" and is not getting any attention. Could someone please-please look at this issue?) The PHP script below demonstrates. Using the bundled GD library, and any TrueType font.

Change $font to point to any TrueType font file you have.
Run via PHP CLI, sending out to filename.png and view, or access via browser.

I wrote an equivalent C program with gd-2.0.35 (Slackware 12.0 Linux distribution), and the bounding boxes are correct. So there is probably something wrong in the GD library bundled with PHP.

Reproduce code:
$font = '/usr/share/fonts/TTF/luximr.ttf'; # Full path to any TTF Font
$delta_t = 360.0 / 16; # Make 16 steps around
$g = imagecreate(800, 800);
$bgnd  = imagecolorallocate($g, 255, 255, 255);
$black = imagecolorallocate($g, 0, 0, 0);
$x = 100;
$y = 0;
$cos_t = cos(deg2rad($delta_t));
$sin_t = sin(deg2rad($delta_t));
for ($angle = 0.0; $angle < 360.0; $angle += $delta_t) {
  $bbox = imagettftext($g, 24, $angle, 400+$x, 400+$y, $black, $font, 'ABCDEF');
  imagepolygon($g, $bbox, 4, $black);
  imagefilledellipse($g, $bbox[0], $bbox[1], 8, 8, $black);
  $temp = $cos_t * $x + $sin_t * $y;
  $y    = $cos_t * $y - $sin_t * $x;
  $x    = $temp;
header("Content-type: image/png");

Expected result:
In the image output: For each angle of text, the box should enclose the text, with the dot to the lower left (relative to the text baseline) of the first character in the string.

Actual result:
The bounding box for 0 degree text is correct. All the others are off by different amounts. Worst is the text between 180 and 270 degrees, where the text is completely outside the bounding box.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-02 13:07 UTC]
Please try using this CVS snapshot:
For Windows:

 [2008-11-02 22:16 UTC] lbayuk at pobox dot com
I tried php5.2-200811022130 (PHP 5.2.7RC3-dev).
The problem is still there, and the image is identical to PHP-5.2.6.
(I was using 5.2.4 when I originally submitted this report, so I changed the version in the report to 5.2.6).
 [2009-03-17 17:24 UTC] mmcnicklebugs at googlemail dot com

For reference: pre-patch image of test-case [1]

The problem was due to using the wrong center of rotation when rotating the bounding box. I've written a patch [2] to set the correct the coordinates. This appears to have fixed the problem as seen in this image [3].

In image [3] the bounding boxes don't seem to correspond to the actual bounds of the text (the fitting is a bit loose). Do you think this is another (separate) bug?


-- Martin McNickle
 [2009-03-18 00:32 UTC]
Thanks for your patch, applied to 5.3!

I will merge it to HEAD and add a test later this week, after 5.3.0RC1.
 [2009-05-06 08:00 UTC]
Tabe applied it to HEAD and added tests upstream (libgd).
 [2009-07-27 22:11 UTC]
Automatic comment from SVN on behalf of tabe
Log: added test for #43073
 [2009-07-27 22:12 UTC]
Automatic comment from SVN on behalf of tabe
Log: MFB: added test for #43073
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Jun 27 22:01:44 2017 UTC