Creating web thumbnails with PHP Imagick
It may seem like a subject that has been done to death, but creating thumbnails from images can be a complex and frustrating task, especially when you’re faced with a wide variety of formats in your source images.
The script below uses PHP’s Imagick extension to create a thumbnail from pretty much any source image. It can handle RGB & CMYK images, TIFFs, PNGs, JPEGs, GIFs, BMPs and just about any other format in just about any size, and it can also handle images with embedded colour profiles (this is very helpful when converting images created in Adobe software).
You’ll firstly need to grab the standard sRGB colour profile from the International Color Consortium (ICC) website. Place this somewhere accessible to your script and you’re ready to go.
$imagePath = '/path/to/source_image';
// these are treated as maximums and aspect ratio is maintained
$thumbnailWidth = 100;
$thumbnailHeight = 100;
// path to the sRGB ICC profile
$srgbPath = '/path/to/sRGB_v4_ICC_preference.icc';
// load the original image
$image = new Imagick($imagePath);
// get the original dimensions
$width = $image->getImageWidth();
$height = $image->getImageHeight();
// set colour profile
// this step is necessary even though the profiles are stripped out in the next step to reduce file size
$srgb = file_get_contents($srgbPath);
$image->profileImage('icc', $srgb);
// strip colour profiles
$image->stripImage();
// set colorspace
$image->setImageColorspace(Imagick::COLORSPACE_SRGB);
// determine which dimension to fit to
$fitWidth = ($thumbnailWidth / $width) < ($thumbnailHeight / $height);
// create thumbnail
$image->thumbnailImage(
$fitWidth ? $thumbnailWidth : 0,
$fitWidth ? 0 : $thumbnailHeight
);
// generate a thumbnail filename
$imagePathParts = pathinfo($imagePath);
$thumbnailPath =
$imagePathParts['dirname'].'/'.
$imagePathParts['filename'].'_'.
$thumbnailWidth.'x'.$thumbnailHeight.
'.jpg';
// save thumbnail and free up memory
$image->writeImage($thumbnailPath);
$image->clear();
$image->destroy();
7 Comments »
RSS feed for comments on this post. TrackBack URL
This was very helpful (and timely!), thanks! The only issue I had was that the colours in the converted image (in my case, going from a high-resolution CMYK JPEG to a web-ready JPEG) were coming out much lighter than in the original, so I used RGB instead of SRGB instead and I find that I’m getting consistent colours now.
MANY thanks! I had some CMYK jpegs for the first time recently, and my thumbnail script was pumping out 1mb thumbs! I tried converting colorspace with no luck. Then your page mentions using the ICC profile. That was the key! Again thank you.
Thank you Erin for this article.
Unfortunately, I get an unexpected result with a CMYK photo : I get some kind of negative colors :-/
I made some more tests and found the problem concerning CMYK photos: if the color profile is not embedded in the file, ImageMagick produces a very bad result at color profile conversion. With a CMYK photo embedding its color profile, it works really well!
Next step for me: detect if the current color profile is AdobeRGB or sRGB before converting to sRGB.
Interesting… so you had some images which used a colour profile, but the colour profile was not actually embedded into the image? That would certainly be a bit trickier!
That didn’t do it for me.
Had an AdobeRGB image and converted with your script.
The image colors are very light and there is no profile assigned.
Just how light are we talking? Keep in mind that the script will never produce an image that looks like what you would get from Photoshop.
Generally speaking, Photoshop will do a much better job at converting image colours between modes and profiles, but there’s not much you can do to improve what ImageMagick gives you (as far as I know).
Lastly, the fact that there’s no profile assigned is expected. The script strips the profile information as it is not usually needed for web thumbnails.