go to bug id or search bugs for
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: http://bugs.php.net/bug.php?id=49600 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:
http://img407.imageshack.us/img407/1888/mbxe.png (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.
$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
sizeof($polygon) / 2,
imagecolorallocatealpha($im, 255, 0, 0, 64)
The polygon should enclose the character 'C' without overlapping the character.
The polygon overlap the character 'C' because every character is shifted right.
Add a Patch
Add a Pull Request
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 http://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html#section-3 - 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
Fixed with <http://git.php.net/?p=php-src.git;a=commit;h=f5f94d9c>.
Actually, this is a duplicate of bug #53504.