|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 00:00:02 2025 UTC |
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 ) */ ?>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.1I'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); ?>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); ?>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; } ?>