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

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
bharatb
Posts: 2
Joined: 2019-03-11T22:52:23-07:00
Authentication code: 1152

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
bharatb
Posts: 2
Joined: 2019-03-11T22:52:23-07:00
Authentication code: 1152

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
Post Reply