php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21560 imagettfbbox() returns different values in 4.3.0 and 4.2.1
Submitted: 2003-01-09 18:49 UTC Modified: 2003-08-11 14:39 UTC
Votes:20
Avg. Score:4.7 ± 0.6
Reproduced:18 of 19 (94.7%)
Same Version:10 (55.6%)
Same OS:5 (27.8%)
From: ljpersson at hotmail dot com Assigned: pajoye (profile)
Status: Wont fix Package: GD related
PHP Version: 4.3.1-dev, 4.3.2RC2, 4.3.2RC3 OS: SusE 8.0,SuSE 8.2
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2003-01-09 18:49 UTC] ljpersson at hotmail dot com
For the given string string 

"Demonstration of different\ntext of two lines"

(ArialTTF font  font 90 degrees rotation containing two lines separated by a single "\n".)

The imagettfbbox() returns different value arrays in PHP 4.2.1 using GD 2.01 and
PHP 4.3.0 using builtin GD 2.08+

(You get the same difference for 4.2.1 by using GD 2.08 so the problem must be in GD 2.08)

For GD 2.01  it returns (-29,18,29,-277,-21,-277,-21,18)
For GD 2.08+ it returns (-29,18,29,-279,-20,-279,-20,-1)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-09 18:59 UTC] ljpersson at hotmail dot com
For the given string string

"Demonstration of different\ntext of two lines"

(ArialTTF font  font 90 degrees rotation containing two lines separated
by a single "\n".)

The imagettfbbox() returns different value arrays in PHP 4.2.1 using GD
2.01 and
PHP 4.3.0 using builtin GD 2.08+

(You get the same difference for 4.2.1 by using GD 2.08 so the problem
must be in GD 2.08)

For GD 2.01  it returns (29,-1,29,-279,-20,-279,-20,-1)
For GD 2.08+ it returns (29,18,29,-277,-21,-277,-21,18)
 [2003-01-09 20:11 UTC] iliaa@php.net
Try non-bundled gd 2.0.9 and see if you get the same results as the ones you've got with bundled GD library. I suspect that the behaviour of GD between 2.0.1 and later versions had changed.
 [2003-01-27 22:39 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.


 [2003-02-07 10:03 UTC] ljpersson at hotmail dot com
The problem still exists in snapshot 200302071430

for the _exact_ same text string imagettfbbox() (and the _exact_ same test script) returns the following (different) values depending on version of PHP and GD

Snapshot 200302071430
Array ( [0] => -1 [1] => 14 [2] => -1 [3] => -226 [4] => -16 [5] => -226 [6] => -16 [7] => 14 )

PHP 4.3 (official release)
Array ( [0] => -1 [1] => 13 [2] => -1 [3] => -226 [4] => -16 [5] => -226 [6] => -16 [7] => 13 )

PHP 4.2.3 (with GD 2.01)
Array ( [0] => 3 [1] => -1 [2] => 3 [3] => -234 [4] => -16 [5] => -234 [6] => -16 [7] => -2 )

The only correct values are with GD 2.01 for both PHP 4.3 and snapshot the bounding box is incorrect since it starts 14 (in snapshot) or 13 (in 4.3) pixels in into the text.
 [2003-02-09 02:41 UTC] pajoye@php.net
Hi,

Can you provide a link with the test script and the font used ?

thank's

pierre
 [2003-02-13 05:26 UTC] ljpersson at hotmail dot com
Below is a simple test script that illustates the problem. You might need to change the path for the fonts to suit your setup.

The script strokes a text and then draws the bounding box around the text. For angle = 0 the bounding box is correct but for any other angle it is off proportional to the angle. 

(With GD 2.01 this is correct but for higher versions the problem exists)

/Johan

<?php
DEFINE('TTF_DIR','/usr/X11R6/lib/X11/fonts/truetype/');

$w=400; $h=300;
$img = @imagecreatetruecolor($w,$h);
$black = imagecolorallocate ($img, 0, 0, 0);
$white = imagecolorallocate ($img, 255, 255, 255);
$red = imagecolorallocate ($img, 255, 0, 0);
imagefilledrectangle($img,0,0,$w,$h,$white);

$font = TTF_DIR.'arial.ttf';
$font_size = 14;
$txt = 'Some dummy text';
$x = 75; $y=250; 

// For angle == 0 the TTF box is correct 
$angle = 90;

imagettftext($img,$font_size,$angle,$x,$y,$black,$font,$txt);
$bbox = imagettfbbox(14,$angle,$font,$txt);

// Drwa the bounding box
imageline($img,$x+$bbox[0],$y+$bbox[1],$x+$bbox[2],$y+$bbox[3],$red);
imageline($img,$x+$bbox[0],$y+$bbox[1],$x+$bbox[6],$y+$bbox[7],$red);
imageline($img,$x+$bbox[6],$y+$bbox[7],$x+$bbox[4],$y+$bbox[5],$red);
imageline($img,$x+$bbox[4],$y+$bbox[5],$x+$bbox[2],$y+$bbox[3],$red);

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

/* 

print_r($bbox);

On SuSE 8.0 this gives (for angle=90):

PHP 4.2.3 With GD 2.01 gives [CORRECT]:  
Array ( [0] => 3 [1] => -1 [2] => 3 [3] => -154 
        [4] => -16 [5] => -154 [6] => -16 [7] => -2 ) 

PHP 4.2.3 With GD 2.08 gives [WRONG]:
Array ( [0] => -1 [1] => 13 [2] => -1 [3] => -153 
        [4] => -17 [5] => -153 [6] => -17 [7] => 13 ) 

PHP 4.3.0 With built-in GD gives [WRONG]:
Array ( [0] => -1 [1] => 13 [2] => -1 [3] => -153 
        [4] => -17 [5] => -153 [6] => -17 [7] => 13 ) 

PHP 4.3.1-dev (2002-02-07) [WRONG]
Array ( [0] => -1 [1] => 13 [2] => -1 [3] => -153 
        [4] => -17 [5] => -153 [6] => -17 [7] => 13 ) 
*/

?>
 [2003-02-13 11:45 UTC] sniper@php.net
Please use the 'Edit submission' link when you reply to 
your _own_ report, otherwise the status remains at 'feedback'..and eventually gets automatically suspended.

 [2003-02-13 12:19 UTC] iliaa@php.net
Verified with latest CVS, this however appears to be an issue with the GD library itself since the problem is not particular to the bundled GD.
 [2003-02-26 19:55 UTC] pajoye@php.net
I confirm it too. I found where is the problem. I will try to fix it before the next release.


 [2003-02-27 08:47 UTC] sniper@php.net
don't forget.. :)

 [2003-05-05 13:23 UTC] ljpersson at hotmail dot com
This problem still exists in 4.3.2RC2. This returns yet another set of (wrong) coordinates for the bounding box.

With 4.3.2RC2 (Compiled on SuSE 8.2) gives the following wrong return array:

Array ( [0] => -1 [1] => 14 [2] => -1 [3] => -146 
        [4] => -17 [5] => -146 [6] => -17 [7] => 14 )


Which is again different from what was returned in 4.3.1
 [2003-05-16 19:37 UTC] ljpersson at hotmail dot com
Just checked with 4.3.2RC3 and the problem still exists.
 [2003-06-18 03:03 UTC] guno at guno dot nl
I'm experiencing the same problem, using php 4.3.2 with GD 2.0.15
Normal text (with no rotation) results in a correct bounding box, but as soon as I rotate the text, it goes wrong, no matter which font I use.

Here's a sample code which displays a text with it's bounding box at 0, 90, 180 and 170 degrees:

<?php
$image=imagecreate(500, 500);
$imagedum=imagecreate(500, 500);
$color1=imagecolorallocate($image, 0xff, 0xff, 0xff);
$color2=imagecolorallocate($image, 0x3f, 0x3f, 0x3f);
$color3=imagecolorallocate($image, 0xff, 0x00, 0x00);

for ($angle=0; $angle<360; $angle+=90) {
    $text="ABCpqrs";
    $font = "./verdana.ttf";

    // Draw the text into the image:
    $result=imagettftext($imagedum, 30, $angle, 250, 250, $color2, $font, $text);
            imagettftext($image,    30, $angle, 250, 250, $color2, $font, $text);

    // Draw the bounding-box into the image:
    imageline($image, $result[0], $result[1], $result[2], $result[3], $color3);
    imageline($image, $result[2], $result[3], $result[4], $result[5], $color3);
    imageline($image, $result[4], $result[5], $result[6], $result[7], $color3);
    imageline($image, $result[6], $result[7], $result[0], $result[1], $color3);
}
// Draw the image to the screen:
header("Content-Type: image/jpeg");
imagejpeg($image);

imagedestroy($image);
imagedestroy($imagedum);
?>
 [2003-06-24 01:33 UTC] kyle at putnamcabinets dot com
I also experience the problem with PHP 4.3.2 and bundled GD version '2.0.12 compatible'. Recompiled with Debian package version (2.0.12-2... damn!) and same problem (obviously). It seems to be some how proportional to the angle of the text; smaller angles yield more accuracy.

My implementation:
<?
$res   = imagecreatetruecolor(400, 400);
$white = imagecolorallocate($res, 0xff, 0xff, 0xff);
$red   = imagecolorallocate($res, 0xcc, 0x00, 0x00);
$blue  = imagecolorallocate($res, 0x00, 0x00, 0xcc);
imagefill($res, 0, 0, $white);

for ($angle = 0; $angle < 360; $angle += 60)
{
    $wrong1 = imagettftext($res,20,$angle,200,200,$blue,
		"/usr/share/fonts/truetype/Courier_New.ttf",
        "ABCDEF 12345");
    $t = imagettfbbox(20, $angle,
        "/usr/share/fonts/truetype/Courier_New.ttf",
        "ABCDEF 12345");
    
    $wrong2 = array(200+$t[0],200+$t[1],200+$t[2],
    200+$t[3],200+$t[4],200+$t[5],200+$t[6],200+$t[7]);
    
    imagepolygon($res,$wrong2,count($wrong2)/2,$red);
    imagepolygon($res,$wrong1,count($wrong1)/2,$red);
}

header("Content-type: image/png");
imagepng($res);
?>
 [2003-07-20 03:29 UTC] kyle at putnamcabinets dot com
I've upgraded to PHP 4.3.3RC1 and the problem still exists. Maybe a problem with GD? I've reported the bug to boutell.com and haven't recieved a response in over three weeks. An exmple of this bug at work (problem with imagettfbbox and imagettftext): http://webdev.quiteuseless.com/damn-gd-ttf-bugs/test.php
 [2003-08-11 14:39 UTC] pajoye@php.net
There are BC issues with FT1 support in gd. The code requires deep changes to solve this issue.

pierre
 [2004-02-08 14:28 UTC] jtopland at hive dot no
Here's a fixed version of imagettfbbox().
All angles returns correct values.
Except that imagettftext() returns different trackings (space between each character) when rotating.

<?php
	// Set some test variables
	$font = "d://www//tahoma.ttf";
	$text = "Finally, I can center rotated text!";
	$size = 20;
	$angle = 20;

	// Create an image and fill the background with lightgray
	$image = imagecreatetruecolor(500, 400);
	imagefill($image, 0, 0, hexdec("dddddd"));

	// Make a cross to make it easier to analyze
	imageline($image, 0, 0, imagesx($image), imagesy($image), hexdec("000000"));
	imageline($image, imagesx($image), 0, 0, imagesy($image), hexdec("000000"));

	// Run a fixed version of imagettfbbox()
	$bbox = imagettfbbox_fixed($size, $angle, $font, $text);

	// Make some text and center the text on the image.
	// imagettftext() pivot is on lower left
	imagettftext($image, $size, $angle, imagesx($image) / 2 - $bbox['width'] / 2, imagesy($image) / 2 + $bbox['height'] / 2, hexdec("0000ff"), $font, $text);

	// Show the image
	imagepng($image);


	function imagettfbbox_fixed($size, $angle, $font, $text)
	{
		// Get the boundingbox from imagettfbbox(), which is correct when angle is 0
		$bbox = imagettfbbox($size, 0, $font, $text);

		// Rotate the boundingbox
		$angle = pi() * 2 - $angle * pi() * 2 / 360;
		for ($i=0; $i<4; $i++)
		{
			$x = $bbox[$i * 2];
			$y = $bbox[$i * 2 + 1];
			$bbox[$i * 2] = cos($angle) * $x - sin($angle) * $y;
			$bbox[$i * 2 + 1] = sin($angle) * $x + cos($angle) * $y;
		}

		// Variables which tells the correct width and height
		$bbox['width'] = $bbox[0] + $bbox[4];
		$bbox['height'] = $bbox[1] - $bbox[5];

		return $bbox;
	}
?>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Dec 01 06:01:23 2020 UTC