How to get ICC profiles working with TimThumb


I was working on a web site that used TimThumb, and ran into an issue.

TimThumb uses the PHP GD library, which does not support color profiles. It is a very common library used in WordPress themes. This can be an issue with web sites which are for photographers, as sometimes, it may be desired to have embedded color profiles in thumbnails. But when timthumb processes an images, it strips the embedded color profile and all metadata.

The metadata I am not too fussed about, but I need to have the sRGB profile embedded in the displayed profile. For visitors using a wide gamut monitor, untagged sRGB images are not displayed properly in Firefox, as color management by default is set such that color management only works on tagged images (see this link on how to enable it for all images). This is really only an issue for visitors on wide gamut monitors.

Here is how to fix this problem.

We modify timthumb so when it writes images to the cache, it adds the ICC profile back in. I used the PHP JPEG ICC profile manipulator by Richard Toth

Download this and place class.jpeg_icc.php into the same directory as timthumb.php

Now download this file in-srgb.jpg (right click and “Save link as…” or “Save target as…”) and place it in the same directory as timthumb.php as well.

Add the following at the start of timthumb.php:


Look for the processImageAndWriteToCache() method and look for the following snippet:

if(preg_match('/^image\/(?:jpg|jpeg)$/i', $mimeType)){
    $imgType = 'jpg';
    imagejpeg($canvas, $tempfile, $quality);
} else if(preg_match('/^image\/png$/i', $mimeType)){

And replace it with the following:

$tempfile = tempnam($this->cacheDirectory, 'timthumb_tmpimg_');
if(preg_match('/^image\/(?:jpg|jpeg)$/i', $mimeType)){
    $imgType = 'jpg';
    imagejpeg($canvas, $tempfile, $quality);

    // Add sRGB ICC profile
    $icc = new JPEG_ICC();

} else if(preg_match('/^image\/png$/i', $mimeType)){

Now it would be a good idea to clear the timthumb cache, just go into the cache directory and delete its contents.

Now whenever timthumb generates a thumbnail, the thumbnail will have an embedded sRGB ICC profile.