1--TEST-- 2Bug #53504 imagettfbbox/imageftbbox gives incorrect values for bounding box 3--SKIPIF-- 4<?php 5 if(!extension_loaded('gd')){ die('skip gd extension not available'); } 6 if(!function_exists('imageftbbox')) die('skip imageftbbox() not available'); 7?> 8--FILE-- 9<?php 10$cwd = dirname(__FILE__); 11$font = "$cwd/Tuffy.ttf"; 12 13$g = imagecreate(800, 800); 14$bgnd = imagecolorallocate($g, 255, 255, 255); 15$black = imagecolorallocate($g, 0, 0, 0); 16$red = imagecolorallocate($g, 255, 0, 0); 17$blue = imagecolorallocate($g, 0, 0, 255); 18 19$tests = [ 20 // Kerning examples (unfortunately not available in "Tuffy" test font): 21 ['fontSize' => 50, 'angle' => 0, 'x' => 20, 'y' => 70, 'text' => 'AV Teg', 'exp' => [2,15, 208,15, 208,-48, 2,-48]], 22 ['fontSize' => 50, 'angle' => 90, 'x' => 70, 'y' => 350, 'text' => 'AV Teg', 'exp' => [15,-1, 15,-208, -48,-208, -48,-2]], 23 ['fontSize' => 50, 'angle' => 40, 'x' => 130, 'y' => 280, 'text' => 'AV Teg', 'exp' => [11,11, 169,-122, 129,-171, -30,-39]], 24 25 // Shift-Test: 26 ['fontSize' => 100, 'angle' => 0, 'x' => 350, 'y' => 110, 'text' => 'H-Shift', 'exp' => [8,2, 386,2, 386,-97, 8,-97]], 27 28 // Small/single chars: 29 ['fontSize' => 100, 'angle' => 0, 'x' => 350, 'y' => 220, 'text' => '-', 'exp' => [7,-37, 51,-37, 51,-46, 7,-46]], 30 ['fontSize' => 100, 'angle' => 0, 'x' => 430, 'y' => 220, 'text' => ',', 'exp' => [7,15, 21,15, 21,-13, 7,-13]], 31 ['fontSize' => 100, 'angle' => 0, 'x' => 510, 'y' => 220, 'text' => '.', 'exp' => [7,1, 21,1, 21,-13, 7,-13]], 32 ['fontSize' => 100, 'angle' => 0, 'x' => 590, 'y' => 220, 'text' => '|', 'exp' => [8,0, 17,0, 17,-95, 8,-95]], 33 ['fontSize' => 100, 'angle' => 0, 'x' => 670, 'y' => 220, 'text' => 'g', 'exp' => [5,29, 60,29, 60,-72, 5,-72]], 34 35 // Multi-Line + rotation: 36 ['fontSize' => 30, 'angle' => 0, 'x' => 20, 'y' => 400, 'text' => "Multi\nLine\nTest", 'exp' => [2,107, 80,107, 80,-29, 2,-29]], 37 ['fontSize' => 30, 'angle' => 40, 'x' => 150, 'y' => 420, 'text' => "Multi\nLine\nTest", 'exp' => [70,81, 131,31, 43,-74, -18,-24]], 38 ['fontSize' => 30, 'angle' => 90, 'x' => 250, 'y' => 340, 'text' => "Multi\nLine\nTest", 'exp' => [107,-1, 107,-80, -29,-80, -29,-2]], 39 40 // Some edge case glyphs: 41 ['fontSize' => 50, 'angle' => 90, 'x' => 70, 'y' => 750, 'text' => "iiiiiiiiiiii", 'exp' => [0,-4, 0,-165, -47,-165, -47,-4]], 42 ['fontSize' => 50, 'angle' => 90, 'x' => 150, 'y' => 750, 'text' => "~~~~~~~", 'exp' => [-19,-2, -18,-167, -29,-167, -29,-2]], 43 ['fontSize' => 50, 'angle' => 50, 'x' => 210, 'y' => 750, 'text' => "iiiiiiiiiiii", 'exp' => [3,-3, 107,-127, 70,-157, -34,-33]], 44 ['fontSize' => 50, 'angle' => 50, 'x' => 300, 'y' => 750, 'text' => "~~~~~~~", 'exp' => [-13,-13, 93,-141, 85,-147, -21,-20]], 45 ['fontSize' => 50, 'angle' => 0, 'x' => 430, 'y' => 650, 'text' => "iiiiiiiiiiii", 'exp' => [4,0, 165,0, 165,-47, 4,-47]], 46 ['fontSize' => 50, 'angle' => 0, 'x' => 430, 'y' => 750, 'text' => "~~~~~~~", 'exp' => [2,-19, 167,-19, 167,-29, 2,-29]], 47 48 // "Big" test: 49 ['fontSize' => 200, 'angle' => 0, 'x' => 400, 'y' => 500, 'text' => "Big", 'exp' => [16,59, 329,59, 329,-190, 16,-190]], 50]; 51 52foreach ($tests as $testnum => $test) { 53 $bbox = imageftbbox($test['fontSize'], $test['angle'], $font, $test['text']); 54 printf('%2d: ', $testnum); 55 for ($i = 0; $i < 8; $i++) { 56 $exp = $test['exp'][$i]; 57 if ($bbox[$i] >= $exp - 2 && $bbox[$i] <= $exp + 2) { 58 echo '.'; 59 } else { 60 echo "(expected $exp, got $bbox[$i])"; 61 } 62 } 63 echo "\n"; 64 65 $bboxDrawn = imagefttext($g, $test['fontSize'], $test['angle'], 66 $test['x'], $test['y'], $black, $font, $test['text']); 67 68 // check if both bboxes match when adding x/y offset: 69 for ($i = 0; $i < count($bbox); $i += 2) { 70 if ($bbox[$i] + $test['x'] !== $bboxDrawn[$i]) echo "imageftbbox and imagefttext differ!\n"; 71 if ($bbox[$i + 1] + $test['y'] !== $bboxDrawn[$i + 1]) echo "imageftbbox and imagefttext differ!\n"; 72 } 73 74 // draw bounding box: 75 imagepolygon($g, $bboxDrawn, 4, $red); 76 77 // draw baseline: 78 $width = sqrt(pow($bboxDrawn[2] - $bboxDrawn[0], 2) + pow($bboxDrawn[3] - $bboxDrawn[1], 2)); 79 imageline($g, $test['x'], $test['y'], 80 $test['x'] + $width * cos(deg2rad($test['angle'])), 81 $test['y'] - $width * sin(deg2rad($test['angle'])), $blue); 82} 83 84imagepng($g, "$cwd/bug53504.png"); 85?> 86--CLEAN-- 87<?php @unlink(dirname(__FILE__) . '/bug53504.png'); ?> 88--EXPECT-- 89 0: ........ 90 1: ........ 91 2: ........ 92 3: ........ 93 4: ........ 94 5: ........ 95 6: ........ 96 7: ........ 97 8: ........ 98 9: ........ 9910: ........ 10011: ........ 10112: ........ 10213: ........ 10314: ........ 10415: ........ 10516: ........ 10617: ........ 10718: ........ 108