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