|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61221 imagegammacorrect function loses alpha channel
Submitted: 2012-03-01 14:56 UTC Modified: 2015-06-06 13:34 UTC
Avg. Score:4.8 ± 0.4
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: efbiaiinzinz at hotmail dot com Assigned:
Status: Closed Package: GD related
PHP Version: 5.6.9 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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: efbiaiinzinz at hotmail dot com
New email:
PHP Version: OS:


 [2012-03-01 14:56 UTC] efbiaiinzinz at hotmail dot com
When issuing imagegammacorrect function on an image resource that has alpha 
channel, the alpha channel gets lost.

I looked at the source of 5.4.0RC8 and have also made a minor patch that adds 
alpha channel support by modifying the ext/gd/gd.c file.

Test script:
$img = imagecreatefrompng('input.png');
imagegammacorrect($img, 1.0, 1.0);
imagepng($img, 'result.png');

Expected result:
I would expect to see output.png retain its alpha channel.

Actual result:
The result file does not have any transparency, since the current source code 
indeed does not do gamma correction to alpha channel.


imagegammacorrect-alpha (last revision 2015-06-06 00:45 UTC by
gd.patch (last revision 2012-03-01 14:57 UTC by efbiaiinzinz at hotmail dot com)

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-02 10:17 UTC] efbiaiinzinz at hotmail dot com
In the original proposed patch, alpha shannel should not actually be modified by 
imagegammacorrect, only rgb should get multiplied.

Sample test case to show alpha channel loss:

$imagew = 50;
$imageh = 50;
//truecolor means color allocations just combine ARGB into one integer value
$img = imagecreatetruecolor($imagew, $imageh);
//make sure alpha gets stored
imagesavealpha($img, true);
$blacktransparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
$redsolid = imagecolorallocate($img, 255, 0, 0);
//fill with transparent black background
imagefill($img, 0, 0, $blacktransparent);
//draw solid red cross over the image for contrast
imageline($img, $imagew / 2, 0, $imagew / 2, $imageh - 1, $redsolid);
imageline($img, 0, $imageh / 2, $imagew - 1, $imageh / 2, $redsolid);
//execute imagecammacorrect with same input-/output gamma values so that colors 
should actually remain same
imagegammacorrect($img, 1, 1);
//ask top-left corner color, for truecolor it is ARGB combined into one integer 
$color = imagecolorat($img, 0, 0);
//correct result should be same black-based transparent color that we used for 
echo $color === $blacktransparent ? 'ok' : 'failure';
//comment the echo line above and uncomment the two lines below to see visually 
how transparent color gets changed to solid black
//header('Content-Type: image/png');
 [2015-06-06 00:45 UTC]
The following patch has been added/updated:

Patch Name: imagegammacorrect-alpha
Revision:   1433551535
 [2015-06-06 00:47 UTC]
-Status: Open +Status: Analyzed
 [2015-06-06 00:47 UTC]
I can still confirm the issue with current master (1123032) – even
when `imagesavealpha($img, true)` is added to the supplied test
script, the alpha channel is lost when `imagegammacorrect` is
applied. I agree that this is a bug.

I'm in no way an expert on image manipulation, but I agree that
the alpha channel should not be modified in *any* *way* when
`imagegammacorrect` is called, i.e. the given patch should not be
applied, but rather imagegammacorrect-alpha.patch.
 [2015-06-06 00:53 UTC]
-Operating System: +Operating System: * -PHP Version: 5.4.0RC8 +PHP Version: 5.6.9
 [2015-06-06 08:56 UTC] efbiaiinzinz at hotmail dot com
Glad that someone finally discovered this issue after 3 years :)

Yes indeed, the original patch I did was made in a hurry and then forgotten.
Keeping the alpha value the same is correct behaviour and imagegammacorrect-alpha thus should work correctly.

I wrote a PHP function to work around the issue at the time of the report, discovered the alpha problem some time later in my code but didn't bother to add info here after a year or so total silence from PHP team.
 [2015-06-06 13:34 UTC]
Thanks for the confirmation regarding the patch, and sorry for
having had to wait so long. And, of course, thanks for your report
and the supplied test case.

I have submitted a respective PR now.
 [2015-06-17 00:29 UTC]
Automatic comment on behalf of
Log: Fixed bug #61221 - imagegammacorrect function loses alpha channel
 [2015-06-17 00:29 UTC]
-Status: Analyzed +Status: Closed
 [2015-06-23 18:04 UTC]
Automatic comment on behalf of
Log: Fixed bug #61221 - imagegammacorrect function loses alpha channel
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jun 13 17:01:32 2024 UTC