使用 Imagick 将 PDF 转换为 PNG
PDF to PNG conversion with Imagick
我有一个网页,其中有许多 pdf 文档,它们是 CMYK 格式的一页图像。我需要将它们转换为 png/jpg 才能在网页上显示。我正在尝试使用 PHP 原生 Imagick,但偶然发现了一个奇怪的问题。进行转换的代码如下所示:
$im = new Imagick();
$im->setResolution(200, 200);
$im->readimage($file->getAbsolutePath());
$im->setImageFormat('png');
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$im->writeImage($file->getAbsolutePath() . '.png');
现在在我的本地安装上一切正常,PNG 文件看起来像 PDF 文档。但在我的服务器上,我注意到有时颜色完全不准确。
这是一个例子:
我注意到的唯一区别是 phpinfo 报告的 Imagick 版本:
本地:PHP 5.5.9 Imagick:6.7.7-10 2014-03-06 Q16
服务器:PHP 5.4.42 Imagick:6.8.9-7 Q16 x86_64 2015-03-21
有谁知道如何让服务器使用正确的颜色 space 将 pdf 转换为 png?
[编辑/更新]
根据@fab-sa 的建议,我尝试使用 icc 配置文件,现在代码如下所示:
$icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebUncoated.icc');
$icc_rgb = file_get_contents(dirname(__FILE__).'/sRGB_v4_ICC_preference.icc');
$im->setResolution(200,200);
$im->readimage($file->getAbsolutePath());
$im->setImageFormat('png');
$im->profileImage('icc', $icc_cmyk);
$im->profileImage('icc', $icc_rgb);
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$im->writeImage($file->getAbsolutePath().'.png');
和 icc 配置文件:
https://github.com/vivid-planet/library/blob/master/icc/sRGB_v4_ICC_preference.icc
然而仍然没有预期的结果。
您服务器上使用的 Ghostscript 版本似乎无法正确处理 pdf。我在 gs 版本 8.7 中得到了相同的错误结果,在 9.16
中得到了正确的结果
ghostscript 版本 8.7
gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=1 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r50 -sOutputFile=docgs8-%d.png doc.pdf
ghostscript 版本 9.16 下载自 http://ghostscript.com/download/gsdnld.html
./gs-916-linux_x86_64 -q
-dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=1 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r50 -sOutputFile=docgs9-%d.png doc.pdf
我有一个网页,其中有许多 pdf 文档,它们是 CMYK 格式的一页图像。我需要将它们转换为 png/jpg 才能在网页上显示。我正在尝试使用 PHP 原生 Imagick,但偶然发现了一个奇怪的问题。进行转换的代码如下所示:
$im = new Imagick();
$im->setResolution(200, 200);
$im->readimage($file->getAbsolutePath());
$im->setImageFormat('png');
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$im->writeImage($file->getAbsolutePath() . '.png');
现在在我的本地安装上一切正常,PNG 文件看起来像 PDF 文档。但在我的服务器上,我注意到有时颜色完全不准确。
这是一个例子:
我注意到的唯一区别是 phpinfo 报告的 Imagick 版本:
本地:PHP 5.5.9 Imagick:6.7.7-10 2014-03-06 Q16
服务器:PHP 5.4.42 Imagick:6.8.9-7 Q16 x86_64 2015-03-21
有谁知道如何让服务器使用正确的颜色 space 将 pdf 转换为 png?
[编辑/更新]
根据@fab-sa 的建议,我尝试使用 icc 配置文件,现在代码如下所示:
$icc_cmyk = file_get_contents(dirname(__FILE__).'/USWebUncoated.icc');
$icc_rgb = file_get_contents(dirname(__FILE__).'/sRGB_v4_ICC_preference.icc');
$im->setResolution(200,200);
$im->readimage($file->getAbsolutePath());
$im->setImageFormat('png');
$im->profileImage('icc', $icc_cmyk);
$im->profileImage('icc', $icc_rgb);
$im->transformImageColorspace(Imagick::COLORSPACE_SRGB);
$im->writeImage($file->getAbsolutePath().'.png');
和 icc 配置文件:
https://github.com/vivid-planet/library/blob/master/icc/sRGB_v4_ICC_preference.icc
然而仍然没有预期的结果。
您服务器上使用的 Ghostscript 版本似乎无法正确处理 pdf。我在 gs 版本 8.7 中得到了相同的错误结果,在 9.16
中得到了正确的结果ghostscript 版本 8.7
gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=1 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r50 -sOutputFile=docgs8-%d.png doc.pdf
ghostscript 版本 9.16 下载自 http://ghostscript.com/download/gsdnld.html
./gs-916-linux_x86_64 -q
-dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=1 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r50 -sOutputFile=docgs9-%d.png doc.pdf