I have a PHP application that needs to deal with incoming TIFF files. I have neither control nor knowledge of the color spaces of these TIFFs, and the application should store all incoming images in the form of RGB-JPEG.
The problem is that the input TIFF files are something: CMYK, RGB, some YCbCr wrapped in sRGB , etc., and I need to convert them somehow to RGB-JPEG before saving.
I need some kind of conversion function in PHP that uses the IMagick extension, which can receive any binary TIFF data and convert it to the corresponding binary JPEG RGB data. He needs to handle the different color spaces inside the TIFF images correctly. The output format (RGB JPEG) remains unchanged for any input file.
The following obvious solution correctly converts some CMYK TIFFs, some CMYK TIFFs get inverted colors, and YCbCr RGB TIFFs are completely damaged by the red overlay:
$converter = new IMagick(); $converter->setResourceLimit(6, 1); $converter->readImageBlob($data); if ($converter->getImageColorspace() != IMagick::COLORSPACE_RGB && $converter->getImageColorspace() != IMagick::COLORSPACE_GRAY ) { $icc_rgb = file_get_contents('sRGB_v4_ICC_preference.icc'); $converter->profileImage('icc', $icc_rgb); $converter->setImageColorspace(IMagick::COLORSPACE_RGB); } $converter->setImageFormat('jpeg'); $converter->setImageCompression(Imagick::COMPRESSION_JPEG); $converter->setImageCompressionQuality(60); $converter->resizeImage(1000, 1000, IMagick::FILTER_LANCZOS, 1, true); $converter->stripImage(); $result = $converter->getImagesBlob();
This solution is taken from there: http://blog.rodneyrehm.de/archives/4-CMYK-Images-And-Browsers-And-ImageMagick.html Obviously, it does not work for all color spaces, as it does not detect them reliably. As you can see, it even uses the ICC sRGB_v4 color profile downloaded from the home page .
Google finds me one specific solution to the problem of the red overlay (only one of the conversions), but only for the console and when you know in advance that you are dealing with YCbCr images:
convert some.tif -set colorspace YCbCr -colorspace RGB some.jpg
I can live with passthru -ing convert and pass in to convert all the necessary magic switches, but I assume that I need to pre-define the color space of the original image and call identify | grep identify | grep before each convert to a different PHP application is redundant.