PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 19, 2022

[FIXED] How to convert CMYK/RGB TIFF to RGB JPEG using PHP IMagick

 September 19, 2022     cmyk, image-conversion, imagick, php, tiff     No comments   

Issue

I have a PHP application which needs to deal with incoming TIFF files. I have neither control nor knowledge over the colorspaces of this TIFFs and the application should store all incoming images as RGB JPEGs.

Problem is, incoming TIFF files are anything: CMYK, RGB, some sort of YCbCr wrapped in sRGB, and so on, and I need to convert them somehow to RGB JPEGs before saving.

I need some sort of a conversion function in PHP which uses IMagick extension which can get any binary TIFF data and convert it to proper RGB JPEG binary data. It needs to handle different colorspaces inside TIFF images correctly. Output format (RGB JPEG) stays the same for any input file.

The following obvious solution converts some CMYK TIFFs correctly, some CMYK TIFFs get inverted colors and YCbCr RGB TIFFs get totally corrupted by 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 doesn't work for all colorspaces, because it doesn't detect them reliably. As you can see, it even uses the sRGB_v4 ICC color profile downloaded from it's homepage.

Google finds me one particular solution to the red overlay problem (just one of the conversion screw-ups), but it's only for console and when you know beforehand that you deal with YCbCr images:

convert some.tif -set colorspace YCbCr -colorspace RGB some.jpg

I can live with passthru-ing convert and pass to convert all the magical switches needed, but I suppose I need to detect the source image's colorspace beforehand and call a identify | grep before every convert in an otherwise PHP application is an overkill.


Solution

I've experienced this same issue. It also came up in the imagick forums and the correction was pushed into ImageMagick 6.8.0-4 .

So upgrading should solve this issue. I've upgraded to ImageMagick 6.8.1-9 and haven't encountered this since.



Answered By - Pomme.Verte
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing