Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF Thumbnail always generated as grayscale using Imagick

I am stuck and could really use some help on this one. I am using PHP and Imagick to generate a thumbnail which is working great. However, I noticed that CMYK PDFs are generated always as grayscale. So I tested this by taking the CMYK PDF and manually converting it to RGB with Adobe Acrobat Pro. Then I re-ran it through the following code and it produced a color image. I know about

$image->transformImageColorSpace(1);
or
$image->setImageColorSpace(1);

However this doesn't work. What is the correct way for converting a pdf to a color PNG image? I have looked at the following links with no luck:

http://php.net/manual/en/imagick.setimagecolorspace.php

Convert PDF to JPEG with PHP and ImageMagick

Any help on this one would be great.

Here is the code:

$filePath = fileSaveUserUpload("path/to/file", ""); //path changed here...
    $_SESSION['FILEPATH'] = $filePath;

    //-------------first makes a thumbnail of first page in image/pdf
    $extension_pos = strrpos($filePath, '.');                        // find position (number) of the last dot, so where the extension starts

    $image = new Imagick();
    $image->readImage($filePath."[0]");                              //reads an image at a path(first page only in this case)
    $image->transformImageColorSpace(1);                             //convert to RGB
    $image->setbackgroundcolor('white');                             //replace transparency with this color
    $image->setCompression(Imagick::COMPRESSION_LOSSLESSJPEG);
    $image->setCompressionQuality(150);
    $image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);      //remove transparency
    $image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);          //make everything that was transparent white
    $image->thumbnailImage(0,250);                                   //max height 300 but try and preserve aspect ratio (wdithxheight)
    $thumbnail = substr($filePath, 0, $extension_pos) . '_thumb.png';// . substr($filePath, $extension_pos);

    $image->writeImage($thumbnail);
    $image->clear();
    $image->destroy();

UPDATE:

I am using the following imagick version:

ImageMagick 6.9.1-2 Q16 x86 2015-04-14

3.3.0RC2

GhostScript Version: 9.18

Here is the original PDF (changed it to a picture here):

enter image description here

Here is the thumbnail that it produced:

enter image description here

This ONLY happens with CMYK PDFs. If I take this same PDF and convert it to RGB through adobe acrobat it comes out color. I tested this and it still holds true.

like image 857
wesleywh Avatar asked Sep 26 '22 13:09

wesleywh


1 Answers

greeting from 2019. was having this problem still on gs 9.26 (9.27 doesnt work at all)

set the colourspace BEFORE loading the file, transform it AFTER.

// setup imagick for colour
$Img = new Imagick();
$Img->SetResolution(200,200);
$Img->SetColorspace(Imagick::COLORSPACE_SRGB);

// now read pdf first page.
$Img->ReadImage("{$File}[0]");
$Img->TransformImageColorSpace(Imagick::COLORSPACE_SRGB);

// the rest of your stuff.
like image 166
bobmagicii Avatar answered Oct 11 '22 07:10

bobmagicii