Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert PDF (with transparency *and* CMYK) to jpg

Tags:

php

imagick

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");

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 32bit JFIF N 9481 Unsupported color conversion request [ERROR]

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");

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?

like image 502
aross Avatar asked Mar 27 '15 09:03

aross


1 Answers

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.

like image 78
Danack Avatar answered Sep 20 '22 19:09

Danack