Page 1 of 1

Creating JPG thumbnails from PDF causes problems with version 6.8.9 of ImageMagick

Posted: 2019-03-11T22:55:02-07:00
by bharatb
I am using Wordpress 5.1 and ImageMagick 6.8.9. When I upload PDF from the Media menu of WordPress then it creates JPG thumbnail with a black background.

First, I used default media import functionality of WP to import PDF files then I tried with my own script to create JPG thumbnail of the first page. Here it is

Code: Select all

$uploads = wp_upload_dir();
$imagick = new Imagick();
$imagick->readImage('test.pdf[0]');
$imagick->trimImage(2);
$imagick->setImagePage(0, 0, 0, 0);
$imagick->setImageFormat('jpeg');
file_put_contents($uploads['path'] . '/thumbnail.png', $imagick);
In both cases, it creates an image with a black background.
Does anyone have any idea about this?

Re: Creating JPG thumbnails from PDF causes problems with version 6.8.9 of ImageMagick

Posted: 2019-03-12T07:14:42-07:00
by snibgo
wrote:Does anyone have any idea about this?
What is wrong with the result? What do you want to change?

JPG format can't store transparency. Perhaps your PDF has a transparent background. Instead of writing a JPG, you could save to a format that can record transparency such as PNG or TIFF. Or flatten against a white background before saving as JPG.

Re: Creating JPG thumbnails from PDF causes problems with version 6.8.9 of ImageMagick

Posted: 2019-03-12T23:21:46-07:00
by bharatb
snibgo wrote:
2019-03-12T07:14:42-07:00
wrote:Does anyone have any idea about this?
What is wrong with the result? What do you want to change?

JPG format can't store transparency. Perhaps your PDF has a transparent background. Instead of writing a JPG, you could save to a format that can record transparency such as PNG or TIFF. Or flatten against a white background before saving as JPG.
Yes, I have a transparent background.
Do you mean I have to use PNG as formate?
Like :

Code: Select all

$imagick->setImageFormat('png');
I don't know how to use 'flatten' option.
I am checking http://php.net/manual/en/imagick.flattenimages.php

Re: Creating JPG thumbnails from PDF causes problems with version 6.8.9 of ImageMagick

Posted: 2019-03-13T09:09:34-07:00
by fmw42
If saving as PNG (as above from snibgo) does not work. The if your PDF file is CMYK with transparency, you must convert it to RGB before reading the PDF using the equivalent -colorspace sRGB. Ghostscript does not handle CMYK PDFs that have transparency.