I have the following code on my site where i have to create images out of uploaded pdf files, my problem is that i cannot get the colors right (and yes there can be both text and images in the pdf files):
$img = new imagick();
$img->setResolution(72, 72);
$img->readImage($dir_f.$file);
$img->set ImageCompression(imagick::COMPRESSION_NO);
$img->setImageCompressionQuality(100);
$num_pages = $img->getNumberImages();
for($i = 0;$i < $num_pages; $i++) {
$fina l_name = str_replace(" ", "+", basename(str_replace(".".$file_ext,"", $file_name)));
$final_name = preg_replace("/[^a-zA-Z0-9-+]/", "", $final_name);
$save_to = $pdf_dir."/".str_replace(".".$file_ext,"", $final_name).'-'.$i.'.png';
$img->setIteratorIndex($i);
$img->setImageCompression(imagick::COMPRESSION_NO);
$img->setImageCompressionQuality(100);
$img->setimageformat("png");
$img->writeImage($save_to);
$file_image = str_replace(dirname(__FILE__)."/../../", "/", $save_to);
$file_images[] = array($file_image, $index[$ii]);
}
$img->destroy();
I have gotten different results depending on wich type of colorspace i have been using but non of them are correctly made colors.
when i dont set a colorspace it gets to bright. when i use colorspace srgb or cmyk the image gets to bright, when i use colorspace COMPRESSION_NO it gets to bright when i use colorspace rgb it gets to dark
Hopefully someone is able to help me out here.
This looks like it's a bug in Ghostscript, which is the delegate program ImageMagick normally uses to convert PDFs to bitmap format.
Using Ghostscript version 8.70 I see the same output you are seeing, i.e. the image is lighter than it is when viewing the PDF directly.
After downloading Ghostscript version 9 and running the command below to do the conversion, I see the same colours as in the PDF.
You're probably going to need to either upgrade to Ghostscript 9 or figure out what is special about the PDF that makes Ghostscript not convert it correctly. I think the PDF might either have a non-standard color profile or weird colorspace. But as converting it with GS9 works fine, that is probably the easiest way to fix the problem.
./ghostscript-9.16-linux-x86_64/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 153311-flyers-sample-papers-volume-2.pdf