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

I need to generate jpg images from PDF files (first page only). PDF files are created by the user, so they can contain anything. I am 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 that transparency becomes white, not black. However, I ran into problems with some jpg images created, so I ran jpeginfoon them:

$ jpeginfo -c test.jpg

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

It turns out that some of the original PDF files do use CMYK and apparently do not convert to RGB when saving as jpg. So I changed my code to the following (adding one 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");

jpeg RGB, . - . : . Imagick , , , CMYK?

+4
1

- transformImageColorspace not setImageColorspace. TransformImageColorspace , setImageColorspace - , . svg ..

, .

+5

All Articles