php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73291 imagecropauto() $threshold differs from external libgd
Submitted: 2016-10-11 14:53 UTC Modified: 2018-12-01 17:50 UTC
From: cmb@php.net Assigned: cmb (profile)
Status: Closed Package: GD related
PHP Version: 7.0.11 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: cmb@php.net
New email:
PHP Version: OS:

 

 [2016-10-11 14:53 UTC] cmb@php.net
Description:
------------
Depending on whether GD is built with the bundled or an external
libgd, imagecropauto()'s $threshold argument is treated
differently. External libgd assumes mostly much larger $threshold
values to crop the same amount from the image than the bundled
libgd. The different behavior stems from the different
algorithms[1][2].

Actually, I think the algorithm in the bundled is slightly saner
than the one in external libgd (but both appear to be somewhat
arbitrary). Nonetheless the bundled libgd should be fixed to match
the external libgd; otherwise portable code would have to cater to
these differences.

[1] <https://github.com/libgd/libgd/blob/gd-2.2.3/src/gd_color.c#L14-L16>
[2] <https://github.com/php/php-src/blob/PHP-7.0.12/ext/gd/libgd/gd_crop.c#L350-L352>


Test script:
---------------
<?php

$src = imagecreatetruecolor(255, 255);
$white = imagecolorallocate($src, 255, 255, 255);
imagefilledrectangle($src, 0, 0, 254, 254, $white);

for ($i = 254; $i > 0; $i--) {
    $color = imagecolorallocate($src, $i, $i, $i);
    imagefilledellipse($src, 127, 127, $i, $i, $color);
}

foreach ([0.1, 0.5, 1.0, 10.0] as $threshold) {
    $dst = imagecropauto($src, IMG_CROP_THRESHOLD, $threshold, $white);
    if ($dst !== false) {
        printf("size: %d*%d\n", imagesx($dst), imagesy($dst));
    } else {
        printf("cropped to zero size\n");
    }
}


Expected result:
----------------
[external libgd]

size: 247*247
size: 237*237
size: 229*229
size: 175*175


Actual result:
--------------
[bundled libgd]

size: 253*253
size: 219*219
size: 109*109
cropped to zero size


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-11 15:07 UTC] cmb@php.net
FTR: I've filed a suggestion to improve the algorithm, see
<https://github.com/libgd/libgd/issues/334>.
 [2018-12-01 17:49 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=dcad13e8c936646ae9cfd4a4ef97a7443a23e774
Log: Fix #73291: imagecropauto() $threshold differs from external libgd
 [2018-12-01 17:49 UTC] cmb@php.net
-Status: Open +Status: Closed
 [2018-12-01 17:50 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2018-12-01 17:50 UTC] cmb@php.net
The behavior is adjusted as of PHP 7.4.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 08:01:28 2024 UTC