php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49815 Problem with imagettfbbox
Submitted: 2009-10-08 15:07 UTC Modified: 2016-09-23 13:03 UTC
Votes:10
Avg. Score:4.7 ± 0.6
Reproduced:8 of 8 (100.0%)
Same Version:4 (50.0%)
Same OS:1 (12.5%)
From: christian dot roy at orange dot fr Assigned: cmb (profile)
Status: Duplicate Package: GD related
PHP Version: 5.2.11 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: christian dot roy at orange dot fr
New email:
PHP Version: OS:

 

 [2009-10-08 15:07 UTC] christian dot roy at orange dot fr
Description:
------------
delivered value problem : x coord are wrong.

Seems to be very similar with bug #48801 taht has been fixed in 5.2.11 for y coordinates

Reproduce code:
---------------
$fontPath = 'Labo/Includes/Times.ttf'; // std Times.ttf
$testChars = array( 'a', 'k', 'j' );

echo '<pre>';
foreach( $testChars as $char )
	{
    $dims = imagettfbbox( 60, 0, $fontPath, $char );
    echo '<br>bbox returned rectangle for ' . $char . ' : ';
	echo $dims[0]."  *  ";
	echo $dims[1]."  *  ";
	echo $dims[2]."  *  ";
	echo $dims[3]."  *  ";
	echo $dims[4]."  *  ";
	echo $dims[5]."  *  ";
	echo $dims[6]."  *  ";
	echo $dims[7]."<br>";
	}
echo '</pre>';



Expected result:
----------------
With PHP 4.4.9 :

bbox returned rectangle for a : 2  *  1  *  35  *  1  *  35  *  -38  *  2  *  -38

bbox returned rectangle for k : 0  *  -1  *  38  *  -1  *  38  *  -57  *  0  *  -57

bbox returned rectangle for j : -9  *  17  *  14  *  17  *  14  *  -57  *  -9  *  -57



Actual result:
--------------
With PHP 5.2.11

bbox returned rectangle for a : 2  *  1  *  35  *  1  *  35  *  -38  *  2  *  -38

bbox returned rectangle for k : 0  *  -1  *  38  *  -1  *  38  *  -57  *  0  *  -57

bbox returned rectangle for j : -9  *  17  *  14  *  17  *  14  *  -57  *  -9  *  -57


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-08 19:49 UTC] Sjoerd@php.net
Thank you for your bug report.

The expected and actual results in your bug report are the same. Please describe what is wrong with the x-coordinate and what you expect.
 [2009-10-08 20:14 UTC] christian dot roy at orange dot fr
Sorry. 

"Actual result" should have been :

with 5.2.11 :

bbox returned rectangle for a : -1  *  1  *  32  *  1  *  32  *  -38  *  -1  *  -38

bbox returned rectangle for k : -1  *  -1  *  37  *  -1  *  37  *  -57  *  -1  *  -57

bbox returned rectangle for j : -1  *  17  *  21  *  17  *  21  *  -57  *  -1  *  -57
 [2009-10-08 20:19 UTC] pajoye@php.net
Tabe, can you look at this one too please? :)
 [2009-12-08 13:57 UTC] rasmus at mindplay dot dk
This problem occurs with imageftbbox() as well.

I developed a font-embedding system (http://fontjazz.com) and it has 
worked for a long time with PHP5.1 and earlier, but never with PHP 5.2 
or 5.3, where the coordinates (both X and Y) come out all wrong.

I'm using Windows for development, Linux for deployment, and I have to 
keep the production server running with PHP 5.1 for this reason - both 
Linux and Windows versions fail to give the right coordinates.
 [2009-12-16 21:56 UTC] rasmus at mindplay dot dk
To help you debug this issue, I created the following test-script:

<?php

define('SAMPLE', @$_SERVER['QUERY_STRING'] ? $_SERVER['QUERY_STRING'] : 'abc');
define('SAMPLE_TTF', dirname(__FILE__).'/georgiai.ttf');
define('SAMPLE_SIZE', 100);
define('SAMPLE_PAD', 50);

$m = imagettfbbox(SAMPLE_SIZE, 0, SAMPLE_TTF, SAMPLE);

$width = $m[4]-$m[6] + 2*SAMPLE_PAD;
$height = $m[1]-$m[7] + 2*SAMPLE_PAD;

$baseline = abs($m[7]);

$img = imagecreatetruecolor($width,$height);
$red = imagecolorallocate($img,255,0,0);
$white = imagecolorallocate($img,255,255,255);

imagettftext(
  $img,
  SAMPLE_SIZE, # size
  0,           # angle
  SAMPLE_PAD,  # x
  SAMPLE_PAD+$baseline,  # y
  $white,
  SAMPLE_TTF,
  SAMPLE
);

imagerectangle(
  $img,
  $m[6]+SAMPLE_PAD, $m[7]+SAMPLE_PAD+$baseline, # upper left x,y
  $m[2]+SAMPLE_PAD, $m[3]+SAMPLE_PAD+$baseline, # lower right x,y
  $red
);

header('Content-type: image/png');
imagepng($img);

?>

You will need to adjust the SAMPLE_TTF path to point to a valid truetype file - the one I'm using is Windows standard Georgia Italics.

The output should be a white sample of text with a red border, and the border should grace the text, not overlap it.

For reference, here's what it looks like on Linux (GOOD):

http://mindplay.dk/temp/metrics/imagettf-good.png

And here's what it looks like on Windows (BAD):

http://mindplay.dk/temp/metrics/imagettf-bad.png

I just tried the latest XAMPP build (1.7.2 beta 2) for Windows, and the bug is still present.

If you run the script from a browser, you can add your own sample text by adding a query string, e.g.:

http://localhost/sample.php?abcdefg

Note that using a larger font-size (SAMPLE_SIZE) seems to increase the error. It seems that all the returned coordinates are off by a percentage, not by a fixed number of pixels.

I hope this helps solve the problem...
 [2009-12-23 17:49 UTC] christian dot roy at orange dot fr
Hi,

Did you progress on this issue whicj sticks me to PHP 4.

Thanks for your answer.
 [2010-01-14 04:59 UTC] tabe@php.net
rasmus, your observation is essential to understand this bug.
fix for bug#45600 also seems to work well.

With PHP_5_2 or trunk:

bbox returned rectangle for a : -1  *  2  *  38  *  2  *  38  *  -42  *  -1  *  -42

bbox returned rectangle for k : -1  *  -1  *  42  *  -1  *  42  *  -62  *  -1  *  -62

bbox returned rectangle for j : -1  *  17  *  23  *  17  *  23  *  -57  *  -1  *  -57
 [2010-01-14 05:00 UTC] tabe@php.net
s/#45600/#49600/
 [2010-01-22 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".
 [2010-10-31 19:55 UTC] php at zunderer dot de
It seems like this Bug is still present, both of the Testscripts 
provided by rasmus here and by ch+php in Bug #49600 produce 
correct results in PHP 5.0.5 and 5.2.5 but wrong results 
(x-coord of boundingbox shifted to left or right depending on 
Font and letter) on PHP 5.2.11 and 5.3.3
 [2013-01-19 20:25 UTC] philip@php.net
-Status: No Feedback +Status: Open
 [2016-03-31 11:38 UTC] mplomer at gmx dot de
For proposed fix and further info/discussion, see bug #53504
 [2016-09-23 12:54 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 Dec 22 03:01:28 2024 UTC