将PDF(具有透明度*和* CMYK)转换为jpg [英] Convert PDF (with transparency *and* CMYK) to jpg

查看:197
本文介绍了将PDF(具有透明度*和* CMYK)转换为jpg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从PDF文件生成jpg图片(仅首页). PDF文件是用户生成的,因此它们可以包含任何内容.我目前正在使用以下代码:

I need to generate jpg images from PDF files (first page only). The PDF files are user generated, so they can contain anything. I'm currently using the following code:

// Load PDF.
$i = new Imagick;

// Create thumbnail of first page of PDF.
$i->setResolution(150, 150);
$i->loadImage("test.pdf[0]");
$i->thumbnailImage(640, 480, true);

// Remove transparency, fill transparent areas with white rather than black.
$i->setImageBackgroundColor("white");
$i->setImageAlphaChannel(11); // Imagick::ALPHACHANNEL_REMOVE
$i->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

// Output.
$i->writeImage("test.jpg");

这可以正常工作,因为透明度变为白色而不是黑色.但是,我遇到了一些生成的jpg图像的问题,因此我在它们上运行了jpeginfo:

This works as expected in that transparency becomes white instead of black. However, I've run into problems with some generated jpg images, so I ran jpeginfo on them:

$ jpeginfo -c test.jpg

test.jpg 960 x 480 32位JFIF N 9481不支持的颜色转换请求[错误]

test.jpg 960 x 480 32bit JFIF N 9481 Unsupported color conversion request [ERROR]

事实证明,某些源PDF实际上使用CMYK,并且另存为jpg时显然不会转换为RGB.因此,我将代码更改为以下内容(添加了一行)以显式转换为RGB:

It turns out that some source PDFs actually use CMYK, and apparently are not converted to RGB when saved as jpg. So I changed my code to the following (addition of a single line) to explicitly convert to RGB:

// Load PDF.
$i = new Imagick;

// Create thumbnail of first page of PDF.
$i->setResolution(150, 150);
$i->loadImage("test.pdf[0]");
$i->thumbnailImage(640, 480, true);

// Remove transparency, fill transparent areas with white rather than black.
$i->setImageBackgroundColor("white");
$i->setImageAlphaChannel(11); // Imagick::ALPHACHANNEL_REMOVE
$i->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

// Convert to RGB to prevent creating a jpg with CMYK colors.
$i->setImageColorspace(Imagick::COLORSPACE_RGB);

// Output.
$i->writeImage("test.jpg");

这将创建一个具有RGB颜色配置文件的jpeg.但是,由于某些晦涩的原因,它会再次导致图像具有黑色背景.换句话说:透明度问题又回来了.为什么Imagick这样做,更重要的是,透明度问题和CMYK问题的解决方案是什么?

This creates a jpeg with an RGB color profile, all right. However, for some obscure reason it results in an image with a black background again. In other words: the transparency problem is back. Why does Imagick do this, and more importantly, what's the solution to both the transparency problem and the CMYK problem?

推荐答案

要使用的正确函数是transformImageColorspace而不是setImageColorspace. TransformImageColorspace用于现有图像,setImageColorspace用于新图像,例如svg绘图..

The correct function to use is transformImageColorspace not setImageColorspace. TransformImageColorspace is used for existing images, setImageColorspace is for new images e.g. svg drawing..

我已将其添加到手册中,并且应该会很快出现.

I've added it to the manual and it should show up soon.

这篇关于将PDF(具有透明度*和* CMYK)转换为jpg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆