Problem/Motivation
The GD toolkit does not correctly handle gamma when resizing images, because of a limitation of the GD library.
Proposed resolution
As explained here:
http://www.4p8.com/eric.brasseur/gamma.html
A workaround is to alter image gamma to achieve a linear scale before resizing, and change it back to original after having resized.
The patch is quite simple, it just calls imagegammacorrect before and after imagecopyresampled.
Result comparison
Using this original image for example:

This is what Drupal currently gives after resizing and cropping to 75x75px:

and after patching:

The second scaled image is visually closer to the unscaled image.
Steps to reproduce
- Use the image in the Issue Summary
- Make a image style, resize 75x75px
- Change the article content type to use the new image style
- Upload the image in the issue summary
Proposed resolution
Remaining tasks
Update the patch
Add a test
Review
Commit
User interface changes
API changes
Data model changes
Release notes snippet
Comments
Comment #1
SeeSchloss commentedBy the way, I just checked that the exact same code is used in 8.x, and the same patch could be applied there.
Comment #2
SeeSchloss commentedWell... what's happening? Any problem with this?
Comment #3
attiks commentedCan you first upload a patch for drupal 8, once it's fixed it can be back ported to drupal 7
Comment #4
SeeSchloss commentedWell okay, here it is (the line numbers have changed).
Comment #6
SeeSchloss commentedAh, well from what I can see the color test is broken at least because it only actually does the assert on the last corner (even though all four corners are tested). I'll try to find time to look further into it.
Comment #7
SeeSchloss commentedHA. So the problem is caused by a PHP bug with imagegammacorrect not keeping alpha channel with PNG images: https://bugs.php.net/bug.php?id=61221
But the test is still wrong, and it does not catch this bug when I use a test image with the transparent colour in another corner than the bottom-left one.
After I corrected the test and made it check all four corners, a few other tests which used to pass do not pass anymore. How do I submit a patch for this test? Do I simply open a new issue?
Comment #8
attiks commentedIf other tests start failing I think you better open a new issue to fix the tests first.
Comment #9
SeeSchloss commentedYes, here it is: #1579810: GD tests do not test check every case and code cleanup
About this issue now, it won't pass the (fixed) tests until the bug is fixed in PHP, or until I (or someone else!) implement a workaround to keep transparency in resized PNG images, so it still needs work I guess。
Comment #10
SeeSchloss commented#4: drupal-image-gammacorrect-1480428.patch queued for re-testing.
Comment #12
cweagansFixing tags per http://drupal.org/node/1517250
Comment #13
claudiu.cristea@SeeSchloss, the code has changed in D8. Can you rework the patch?
Comment #14
dman commentedTagging as novice, as it's a nice one for sprinters to re-roll, and a nice one for non-developer testers to test.
Comment #15
thoth commentedtaking this for initial sprint
Comment #16
thoth commentedGrepping around I found this file:
core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php:
which contained the function 'resize', which is the newly renamed image_gd_resize from the old image.gd.inc
I made a new patch, submitting for review
Comment #18
thoth commentederrant curly bracket, new patch submitted
Comment #20
thoth commentedhanged from $image->resource to $this->GetResource, resubmitting for review
Comment #22
thoth commentedAfter failing tests with
" 178 passes 28 fails 37 exceptions"
I tried to checking out a clean 8.x branch and rerunning the tests. I get the same fails and exceptions:
" 178 passes 28 fails 37 exceptions "
So it's not my patch that introduced these issues. As there is lots of work going on today I will continue to look at this issue to see if any of the issues clear up, otherwise I'll start poking around and seeing if I can fix the overall problem.
Comment #23
amitgoyal commentedPatch in #20 no longer applies. Please review revised patch which applies cleanly.
Comment #25
l0keGDToolkit was refactored. And resize action execution was moved to separate class. Simply re-appling the changes above there and let's see how the tests will run.
Comment #27
l0keTest fails, I think, related to https://bugs.php.net/bug.php?id=61221.
Because debugging shows that test failing on the corner 3, that contains transparent color.
Expectet color in the corner 3 is:
actual color after resize:
If someone have more info, you are welcome to share it.
Comment #28
roderikNot novice anymore then.
Comment #43
quietone commented@SeeSchloss, Thank you for reporting this problem. We rely on issue reports like this one to resolve bugs and improve Drupal core.
I tested this on 10.1.x, standard install, and was able to reproduce this error. I uses the image in the Issue Summary and a image style that resized the image to 75x75px.
The patch no longer applies and there are not tests.
I have update the tags per the guidelines, and the IS.
Comment #44
tanuj. commentedadding a reroll for Drupal: 10.1.x.
Comment #45
tanuj. commentedComment #46
smustgrave commentedTest failures were legit so not ready for review.
Tests still need to be added also.
Comment #48
vsujeetkumar commentedRe-roll patch needed for version 11.x.
Comment #49
vsujeetkumar commentedRe-roll patch created. Please have a look
Comment #50
vsujeetkumar commentedFixed the CCF issues along with deprecation message, Patch created, Keeps is as "Needs works" to fix the failed test cases.