How to stop GD2 from washing away the colors upon resizing images?

I've managed to further test this with Imagick:

Imagick sRGB Test

The left half of the image was processed with Imagick and the sRGB_IEC61966-2-1_no_black_scaling.icc color profile, the right half has no color profile associated and shows exactly the same if processed with Imagick or GD; here is the code I used:

header('Content-type: image/jpeg');

$image = new Imagick('/path/to/DSC07275.jpg');

if (($srgb = file_get_contents('http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc')) !== false)
{
    $image->profileImage('icc', $srgb);
    $image->setImageColorSpace(Imagick::COLORSPACE_SRGB);
}

$image->thumbnailImage(1024, 0);

echo $image;

Here is a comparison of the several sRGB profiles available on the color.org website:

sRGB Comparison

It seems to me that the third profile produces the most vivid results, other than that I have no idea how one would make a definitive choice.


EDIT: Apparently, Imagick comes with a bundled sRGB profile, so you don't need to download the one from the Image Color Consortium website, the following code should handle all scenarios:

header('Content-type: image/jpeg');

$image = new Imagick('/path/to/DSC07275.jpg');
$version = $image->getVersion();
$profile = 'http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc';

if ((is_array($version) === true) && (array_key_exists('versionString', $version) === true))
{
    $version = preg_replace('~ImageMagick ([^-]*).*~', '$1', $version['versionString']);

    if (is_file(sprintf('/usr/share/ImageMagick-%s/config/sRGB.icm', $version)) === true)
    {
        $profile = sprintf('/usr/share/ImageMagick-%s/config/sRGB.icm', $version);
    }
}

if (($srgb = file_get_contents($profile)) !== false)
{
    $image->profileImage('icc', $srgb);
    $image->setImageColorSpace(Imagick::COLORSPACE_SRGB);
}

$image->thumbnailImage(1024, 0);

echo $image;

Your original image has a Adobe RGB (1998) ICC profile attached. I think GD, not knowing about profiles, is interpreting the image data incorrectly. Here's a related PHP bug confirming this.

You would need to prepare the image with the correct profile, most likely sRGB. If you have an application that can do that, try converting it to sRGB and re-uploading.

If you need a permanent server-side solution to the problem, I think you will need an image processing library that can deal with profiles. To be honest, I don't know how ImageMagick deals with these things, but at least it is familiar with the basic concept of colour profiles.

Related: Comparison of sRGB and Adobe RGB


I have a small bit of info to contribute to this thread. I'm a photographer and not a web developer so my technical knowledge is not great, however, I have been dealing with this issue so I hope this post will help someone else down the line.

I use an online photo sales tool that resamples all my images using GD. I was having issues with images looking way funky even when uploaded with proper sRGB conversion and ICC profiling attached, when viewing on my wide-gamut monitor.

What I found the problem to be is that GD strips all metadata and ICC profiles from the original files. Then the browsers, not seeing any profile, are not displaying them correctly. It's slight on a standard-gamut monitor but very obvious on wide-gamut.

If you're having problems with this, you can test my theory by using Firefox and changing a setting in about:config. Change the value of "gfx.color_management.mode" from the default of "2" to "1". This setting will force Firefox to assume any image without an ICC profile is sRGB and will display it as such. Images should then appear as you would expect and identical to Photoshop/Lightroom/etc. Why all browsers do not use this common-sense approach as their default is beyond me.

Unfortunately, my shopping cart is only setup to use PHP GD so I cannot get good results at this time. I would really like to see GD updated to leave ICC profiles attached or to have the option of adding a simple sRGB profile on export.

More info here: http://www.gballard.net/psd/go_live_page_profile/embeddedJPEGprofiles.html#