|  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: Assigned: cmb (profile)
Status: Closed Package: GD related
PHP Version: 7.0.11 OS: *
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.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2016-10-11 14:53 UTC]
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

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] <>
[2] <>

Test script:

$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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-11 15:07 UTC]
FTR: I've filed a suggestion to improve the algorithm, see
 [2018-12-01 17:49 UTC]
Automatic comment on behalf of
Log: Fix #73291: imagecropauto() $threshold differs from external libgd
 [2018-12-01 17:49 UTC]
-Status: Open +Status: Closed
 [2018-12-01 17:50 UTC]
-Assigned To: +Assigned To: cmb
 [2018-12-01 17:50 UTC]
The behavior is adjusted as of PHP 7.4.0.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jun 13 03:01:33 2024 UTC