php.net |  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
Votes:2
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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: c dot madmax at gmail dot com
New email:
PHP Version: OS:

 

 [2013-10-05 10:39 UTC] c dot madmax at gmail dot com
Description:
------------
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)
http://img223.imagevenue.com/img.php?image=61219_imagettftext_shifted_122_1017lo.jpg

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.

Note:
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:
---------------
<?php

$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(
	$im,
	480,	// size
	0,		// angle
	0,		// x
	480,	// y
	$fgcolor,
	'/usr/share/fonts/truetype/msttcorefonts/verdana.ttf',
	'C'		// text
);

imagesetthickness($im, 2);

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

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

imagepng($im);

exit;

?>

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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-05 16:15 UTC] felipe@php.net
-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 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
 [2016-09-23 12:56 UTC] cmb@php.net
-Status: Assigned +Status: Closed -Assigned To: tabe +Assigned To: cmb
 [2016-09-23 13:03 UTC] cmb@php.net
-Status: Closed +Status: Duplicate
 [2016-09-23 13:03 UTC] cmb@php.net
Actually, this is a duplicate of bug #53504.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 12:01:33 2024 UTC