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