php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64856 Wrong results with imagettfbbox(..)
Submitted: 2013-05-16 09:09 UTC Modified: 2013-05-16 10:54 UTC
From: gert-rainer dot bitterlich at ima-dresden dot de Assigned: ab (profile)
Status: Closed Package: GD related
PHP Version: 5.5.0RC1 OS: Windows 7 Pro
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gert-rainer dot bitterlich at ima-dresden dot de
New email:
PHP Version: OS:

 

 [2013-05-16 09:09 UTC] gert-rainer dot bitterlich at ima-dresden dot de
Description:
------------
The function imagettfbbox(...) calculates wrong result with PHP 5.5.0RC1.
With older PHP Version the result is correct.

Test script:
---------------
<?php
function calculateTextBox($text,$fontFile,$fontSize,$fontAngle) {
     /************
     simple function that calculates the *exact* bounding box (single pixel precision).
     The function returns an associative array with these keys:
     left, top:  coordinates you will pass to imagettftext
     width, height: dimension of the image you have to create
     *************/
     $rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text);
     $minX = min(array($rect[0],$rect[2],$rect[4],$rect[6]));
     $maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6]));
     $minY = min(array($rect[1],$rect[3],$rect[5],$rect[7]));
     $maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7]));

     return array(
      "left"   => abs($minX) - 1,
      "top"    => abs($minY) - 1,
      "width"  => $maxX - $minX,
      "height" => $maxY - $minY,
      "box"    => $rect
     );
 }

// Example usage - gif image output

$text_string    = "Hello World";
$font_ttf        = "c:/windows/fonts/arial.ttf";
$font_size        = 22;
$text_angle        = 0;
$text_padding    = 10; // Img padding - around text

$the_box        = calculateTextBox($text_string, $font_ttf, $font_size, $text_angle);
echo'<pre>';print_r($the_box);echo'</pre>';
?>

Expected result:
----------------
Array
(
    [left] => 0
    [top] => 21
    [width] => 149
    [height] => 21
    [box] => Array
        (
            [0] => -1
            [1] => -1
            [2] => 148
            [3] => -1
            [4] => 148
            [5] => -22
            [6] => -1
            [7] => -22
        )
)

Actual result:
--------------
Array
(
    [left] => 1729848214
    [top] => -1
    [width] => 3723327540
    [height] => 1993479376
    [box] => Array
        (
            [0] => 1993479325
            [1] => 1716
            [2] => 0
            [3] => 1993479376
            [4] => -1729848215
            [5] => 0
            [6] => 0
            [7] => 1516706532
        )
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-16 09:31 UTC] ab@php.net
This should have been fixed yesterday, please test this 
http://windows.php.net/downloads/snaps/php-5.5/r07bd1fa/ or any later snap.

Thanks.
 [2013-05-16 09:31 UTC] ab@php.net
-Status: Open +Status: Feedback
 [2013-05-16 10:31 UTC] gert-rainer dot bitterlich at ima-dresden dot de
-Status: Feedback +Status: Open
 [2013-05-16 10:31 UTC] gert-rainer dot bitterlich at ima-dresden dot de
With developer version http://windows.php.net/downloads/snaps/php-5.5/r07bd1fa/ it works correct now. Thank You.
 [2013-05-16 10:54 UTC] ab@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: ab
 [2013-05-16 10:54 UTC] ab@php.net
Great, thanks for taking time )
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Jul 06 17:05:44 2022 UTC