php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53504 imagettfbbox gives incorrect values for bounding box
Submitted: 2010-12-09 10:59 UTC Modified: 2016-09-23 12:50 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: chris at nugc dot net Assigned: cmb (profile)
Status: Closed Package: GD related
PHP Version: 5.3.3 OS: Windows 7
Private report: No CVE-ID: None
 [2010-12-09 10:59 UTC] 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.

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-31 11:32 UTC] mplomer at gmx dot de
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
 [2016-04-03 10:58 UTC] mplomer at gmx dot de
I added another test-case and visualized the changes.

See Pull-Request: https://github.com/php/php-src/pull/1845
 [2016-04-03 15:46 UTC] pajoye@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: Pajoye
 [2016-09-23 12:50 UTC] cmb@php.net
-Status: Assigned +Status: Closed -Assigned To: Pajoye +Assigned To: cmb
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC