5

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.

Community
  • 1
  • 1
wesleywh
  • 1,053
  • 1
  • 13
  • 30
  • i) This is likely to be a bug with your version of ImageMagick or GhostScript if either is older than about 2 years. ii) can you post an example image? – Danack Jan 27 '16 at 18:56
  • 3/2/2016 - This is still an issue that has never been resolved. Any other image conversions that I might use would be helpful – wesleywh Mar 02 '16 at 18:35
  • Please post an example PDF somewhere - if necessary on the Imagick issue page at https://github.com/mkoppanen/imagick – Danack Mar 02 '16 at 20:39
  • Here is a link to a PDF that I have been testing with: https://www.dropbox.com/s/pzv5cowulz827hd/tree-blackandwhite.pdf?dl=0 Honestly you don't really need a test PDF from me. Just take any pdf and change the color space to CMYK and it will reproduce the error with the above code. All I did here was download a picture from the web. Convert it to a pdf, changed the color space to CMYK. If I change it to RGB it will come out as color. – wesleywh Mar 07 '16 at 17:31

3 Answers3

4

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.
bobmagicii
  • 49
  • 6
0

You may try changing:

$image->transformImageColorSpace(1);
to
$image->transformImageColorSpace(Imagick::COLORSPACE_RGB);.

I'm not sure what you are trying to do with the 1, but according to PHP.net, there are predefined colorspaces that can be added to transformImageColorSpace(); to have the image output with the correct colorspace.

DCUnit711
  • 38
  • 7
  • I actually have tried that with no luck. Here is a link to the number color codes. http://php.net/manual/en/imagick.setimagecolorspace.php That is why I use "1". It is exactly the same as "Imagick::COLORSPACE_RGB". – wesleywh Jan 28 '16 at 21:12
0

The problem you are seeing is likely to be an old version of GhostScript that is not doing the conversion correctly. Below is the image produced when calling your code on a system that has GhostScript version 8.70 installed:

a pwetty twee

btw you almost certainly want to be using SRGB colorspace, not plain old RGB. SRGB is the correct one to use for displaying images on computer screens.

$image->transformImageColorSpace(\Imagick::COLORSPACE_SRGB);

Here is a command line to test ghostscript by itself doing the conversion:

./gs-916-linux_x86_64 \
  -q -dQUIET -dSAFER -dBATCH \
  -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
  -dAlignToPixels=0 -dGridFitTT=1 -sDEVICE=pngalpha \
  -dFirstPage=1 -dLastPage=3 \
  -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72 \
  -sOutputFile=gs-%d.png tree-blackandwhite.pdf

It should convert the image to a PNG. You'd probably need to replace ./gs-916-linux_x86_64 with just gs.

Danack
  • 24,939
  • 16
  • 90
  • 122
  • Ah good to know on the SRGB color space. I was using GS version 9.18. If your using GhostScript 8.70 I will have to try and find a lower version and try this one out and see how this works out. – wesleywh Mar 07 '16 at 18:53
  • Just uninstalled GS 9.18 and installed GS 8.70 running the same code and it is still producing a black and white photo... I'm not sure what I am doing different from you. – wesleywh Mar 07 '16 at 19:10
  • I installed the x86 version since I am running XAMPP on windows. So I installed using the exe to my Program Files(x86) directory. Are you running a linux setup? – wesleywh Mar 07 '16 at 19:18
  • Yes, I'm running on Centos. And the GS 9.16 works fine for me as well. – Danack Mar 07 '16 at 20:47
  • [DROPBOX Command Line Image](https://www.dropbox.com/s/d049r31syk5zf2c/Capture.png?dl=0) Here is a picture of my command that I ran. It ran but didn't output anything. No errors or the image. Is there a log file that I can find? – wesleywh Mar 08 '16 at 17:27
  • Sorry no idea - it should either work or give an error message. – Danack Mar 08 '16 at 19:18