php / php/php-src

`imagecropauto()` not working with transparent background

Open
#13,163 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: gd Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php
$gdImage = imagecreatetruecolor(100,100);
imagesavealpha($gdImage, true);
imagefill($gdImage, 0, 0, imagecolorallocatealpha($gdImage, 0, 0, 0, 127));
// imagepng($gdImage, '/tmp/image_transparent.png');
$result = imagecropauto($gdImage, IMG_CROP_TRANSPARENT);
var_dump($result); // object(GdImage)#2 (0) { }
// imagepng($result, '/tmp/image_black.png');

Resulted in this output:

object(GdImage)#2 (0) { }

But I expected this output instead:

bool(false)

Docs at https://www.php.net/manual/en/function.imagecropauto.php are saying:

If the complete image was cropped, imagecrop() returns false

And this should be the case here IMO.

When doing the same with white background, it works as expected:

<?php
$gdImage = imagecreatetruecolor(100,100);
imagesavealpha($gdImage, true);
imagefill($gdImage, 0, 0, imagecolorallocate($gdImage, 255, 255, 255));
// imagepng($gdImage, '/tmp/image_white.png');
$result = imagecropauto($gdImage, IMG_CROP_WHITE);
var_dump($result); // bool(false)
PHP Version

PHP 8.3.1

Operating System

Kubuntu 22.04

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided PHP snippet with IMG_CROP_TRANSPARENT and compare it with the IMG_CROP_WHITE case on fully transparent and fully white 100x100 images. Trace the imagecropauto() entry point and add coverage for the transparent case; done means a fully cropped transparent image returns false, matching the documented behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
computer-graphics
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.