Bug #53504 [Com]: imagettfbbox gives incorrect values for bounding box

From: Date: Thu, 31 Mar 2016 11:32:13 +0000
Subject: Bug #53504 [Com]: imagettfbbox gives incorrect values for bounding box
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200257@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=53504&edit=1

 ID:                 53504
 Comment by:         mplomer at gmx dot de
 Reported by:        chris at nugc dot net
 Summary:            imagettfbbox gives incorrect values for bounding box
 Status:             Open
 Type:               Bug
 Package:            GD related
 Operating System:   Windows 7
 PHP Version:        5.3.3
 Block user comment: N
 Private report:     N

 New Comment:

I had similar problems and created a pull-request which should fix several (hopefully all :-))
concurring bugs/problems in calculating the bounding box in imagettfbbox/imageftbbox. Also fixed
kerning for angle != 0.

It would be nice to have the fix in PHP 5.6 branch (and above of course):

https://github.com/php/php-src/pull/1845


Previous Comments:
------------------------------------------------------------------------
[2016-02-23 12:43:55] cmb@php.net

Related To: Bug #71602

------------------------------------------------------------------------
[2010-12-09 10:59:20] chris at nugc dot net

Description:
------------
imagettfbbox() function is unreliable. Tried at several different angles (including 0) with several
characters at the test script. The spacing around some letters is wrong, seemingly random parts of
letters are chopped off, some have too much space etc. I have tested this with 4 or 5 different TTF
fonts at different sizes, and the results are the same.

I am trying to use this to calculate spacing between letters, and can't, because of this
function's behaviour. I am having to write my own php function to accomplish the same thing,
and cache the results due to the poorer performance.

Script used in the example is 'Bauhaus 93', a font bundled with Windows, but the behaviour
is common to all fonts.

Test script:
---------------
<?php
  $strText = "EXAMPLE";
  $imgOutput = imagecreate(1640, 1640);
  $colWhite = imagecolorallocate($imgOutput, 255, 255, 255);
  $colBlack = imagecolorallocate($imgOutput, 0, 0, 0);
  $colRed = imagecolorallocate($imgOutput, 255, 0, 0);
  imagefilledrectangle($imgOutput, 0, 0, 1640, 1640, $colWhite);
  for ($angle = 0; $angle < 360; $angle += 30)
    for ($i = 0; $i < strlen($strText); $i++) {
      $intX = 820 + (200 + ($i * 90)) * cos(deg2rad($angle));
      $intY = 820 - (200 + ($i * 90)) * sin(deg2rad($angle));
      imagettftext($imgOutput, 70, $angle, $intX, $intY, $colBlack, "./fonts/BAUHS93.TTF",
$strText{$i});
      $arrBoundingBox = imagettfbbox(70, $angle, "./fonts/BAUHS93.TTF", $strText{$i});
      imageline($imgOutput, $arrBoundingBox[0] + $intX, $arrBoundingBox[1] + $intY,
$arrBoundingBox[2] + $intX, $arrBoundingBox[3] + $intY, $colRed);
      imageline($imgOutput, $arrBoundingBox[2] + $intX, $arrBoundingBox[3] + $intY,
$arrBoundingBox[4] + $intX, $arrBoundingBox[5] + $intY, $colRed);
      imageline($imgOutput, $arrBoundingBox[4] + $intX, $arrBoundingBox[5] + $intY,
$arrBoundingBox[6] + $intX, $arrBoundingBox[7] + $intY, $colRed);
      imageline($imgOutput, $arrBoundingBox[6] + $intX, $arrBoundingBox[7] + $intY,
$arrBoundingBox[0] + $intX, $arrBoundingBox[1] + $intY, $colRed);
    }//for
  header('Content-type: image/png');
  imagepng($imgOutput);

Expected result:
----------------
An image with the word 'EXAMPLE' printed out in angles incrementing by 30 degrees, and
with a red box around each letter marking the bounding box of each character in turn.

Actual result:
--------------
The red boxes do not indicate the bounds of the character, but chop parts off and leave big spaces
in other places, seemingly unpredictably.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=53504&edit=1


Thread (5 messages)

« previous php.bugs (#200257) next »