Problem with Image Magick thumbnaill background colour

Hi there, long time since I have posted here but I have a question that is causing me trouble. I am using PHP’s Image Magick functions to create a thumbnail from a pdf file. The problem is that the white pdf background is being rendered transparently.

Here is my code:

		$thumbnail = new Imagick($copy_path);
		$thumbnail->thumbnailImage( $_POST[thumb_width], 0 );
		$thumbnail->writeImage($filename_long);

The image is saved in png format. I have also tried jpeg but that gives a black background.

I have also tried inserting this line in various places:

		$thumbnail->setImageBackgroundColor("white");

And alternatively:

		$thumbnail->setImageBackgroundColor( new ImagickPixel( 'white' ) );

Is anybody able to shed any light on this for me? Thanks

Why are you trying to make a 0-height image?

It’s not very well documented on the PHP site, but if you set any parameter to 0 then it sizes the image proportionally using the parameter that you have set.

I do not use pdf or Imagick much but looking on the forum to get a white backgroung try adding your background then flatten the image.
Checking out the php imagick pages there is this piece of code in the Imagick::flattenImages section that may help:


<?php

//Note that the function returns an Imagick object and does not modify the existing object. Below is my code for converting a PNG with transparency into a JPG with a background color. This code illustrates the difference.


$im = new Imagick('image.png');
 $im->setImageBackgroundColor('white');

$im->flattenImages(); // This does not do anything.
 $im = $im->flattenImages(); // Use this instead.

$im->setImageFormat('jpg');
 $im->writeImage('image.jpg');

?>

As you say the documentation is not very good and I do not see it improving.

That’s fantastic. Solved the problem. Thanks!