|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65837 imageTTFText text shifted right
Submitted: 2013-10-05 10:39 UTC Modified: 2016-09-23 13:03 UTC
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: c dot madmax at gmail dot com Assigned: cmb (profile)
Status: Duplicate Package: GD related
PHP Version: Irrelevant OS: Ubuntu 12.04
Private report: No CVE-ID: None
 [2013-10-05 10:39 UTC] c dot madmax at gmail dot com
Hello PHP Developers,

I want to inform you about a imageTTFText() bug that exists since four years. The problem is that imageTTFText text is shifted right. The bug is only visible when using a large fontsize. The bigger the font the larger the shift.

The bug was introduced with PHP 5.2.10 and reported here: As you can see in the history of bug 49600, a fix was made in revision 293268 and the bug was closed. But then the fix was reverted in revision 296693 and obviously no one has noticed that.

I strongly urge you to fix this problem. I am using PHP 5.4.0 on Ubuntu 12.04. I am sure the bug still exists in all php versions >=5.2.10 until this day (except revision 293268-296693).

I have made a test script and uploaded a sample image on two image hosters to illustrate how massive the shift is: (best quality)

As you can see on the image, the character 'C' is NOT left aligned as it should because the x position is zero. The character is shifted approximately 20 pixels instead and overlap the polygon (which is properly left-aligned). Some characters like 'A' or 'V' are only a little bit shifted, but also overlap the polygon. It does not matter what font you use, all are right shifted.

The array that is returned from imagettfbbox produces values that differ 1 pixel from the values returned from imageTTFText (when using x=0; y=0) which is also a bug.

Please use my test script and verify the problem. If a php developper whould use this test script each time he made a change in imageTTFText, then such bugs would not occour. It seems the developer do not have enough carefulness. That's a point you should think about.

Test script:

$im = imagecreatetruecolor(500, 500);

$bgcolor = imagecolortransparent($im, imagecolorallocate($im, 255, 255, 255));
$fgcolor = imagecolorallocatealpha($im, 0, 0, 255, 64);

imagefill($im, 0, 0, $bgcolor);

$polygon = imagettftext(
	480,	// size
	0,		// angle
	0,		// x
	480,	// y
	'C'		// text

imagesetthickness($im, 2);

	sizeof($polygon) / 2,
	imagecolorallocatealpha($im, 255, 0, 0, 64)

header('Content-Type: image/png');




Expected result:
The polygon should enclose the character 'C' without overlapping the character.

Actual result:
The polygon overlap the character 'C' because every character is shifted right.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-05 16:15 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: tabe
 [2016-03-31 11:48 UTC] mplomer at gmx dot de
Drawing text is always based on the "origin"-point of a glyph ... there is a glyph-specific "left margin" (the bearingX) which is correct, to get an "optical" (not "physical"!) vertical baseline over multiple lines (see - the graphic below).

So the bug is not about the text is shifted right - but the bounding box is shifted left:

For proposed fix and further info/discussion, see bug #53504
 [2016-09-23 12:56 UTC]
-Status: Assigned +Status: Closed -Assigned To: tabe +Assigned To: cmb
 [2016-09-23 13:03 UTC]
-Status: Closed +Status: Duplicate
 [2016-09-23 13:03 UTC]
Actually, this is a duplicate of bug #53504.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 15 21:01:28 2024 UTC