dqde43215
2012-10-23 06:47
浏览 169
已采纳

如何使用PHP IMagick将CMYK / RGB TIFF转换为RGB JPEG

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.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douren1891 2013-01-07 12:40
    最佳回答

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题