14
$image = "[...]"; //binary string containing PNG image
$file = fopen('image.tmp', 'wb');
fputs($file, $image);
fclose($file);
$image = new Imagick('PNG:image.tmp');
$image->thumbnailImage($width, $height);
$image->setImageFormat('jpg');
$image->setCompressionQuality(97);
$image->writeImage('image.jpg');

The above doesn't work and gives me a black image for this image. When doing instead

[...]
$image->setImageFormat('png');
$image->setCompressionQuality(97);
$image->writeImage('image.png');

all is fine again. I think it has to do something with transparent background, which isn't available in JPG format. Can anyone help to solve this (imagick isn't documented very well, so I don't know how to help myself).

apaderno
  • 28,547
  • 16
  • 75
  • 90
rabudde
  • 7,498
  • 6
  • 53
  • 91

4 Answers4

26

Found a solution:

$white=new Imagick();
$white->newImage($width, $height, "white");
$white->compositeimage($image, Imagick::COMPOSITE_OVER, 0, 0);
$white->setImageFormat('jpg');
$white->writeImage('image.jpg');
rabudde
  • 7,498
  • 6
  • 53
  • 91
  • 3
    This is very useful when dealing with PDF's as well, since the `flattenImages` method resets the page to the last one... Thanks @rabudde! – Roberto Oct 09 '12 at 05:12
11

Another way to convert transparent png to jpg, as mentioned in Imagick::flattenImages:

$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 time moves on, flattenImages() has been deprecated.
Instead of the line above use:

$im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
wp78de
  • 18,207
  • 7
  • 43
  • 71
Bastien
  • 119
  • 1
  • 3
2

You can use setBackgroundColor to set the default background color to something else than black. The PNG transparency will be replaced by the background color when saving to JPG.

Edit: Use it like so:

$img->setBackgroundColor(new ImagickPixel('#FFFFFF'));
Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
  • You need to pass an ImagickPixel object, not a color string if you have Imagick <= 2.1. See my edited response. – Sander Marechal Jul 07 '11 at 13:49
  • I've added `setBackgroundColor` just after calling `new Imagick()`. But that doesn't seem to have any effect. I'm using PHP 5.3.6, ImageMagick 6.7.0, Imagick 3.1.0. – rabudde Jul 07 '11 at 18:08
  • 1
    Calling `exec("convert png.png -resize 500x400 -background white -flatten png_small.jpg")` works perfectly, but why not in PHP with `$image->setBackgroundColor(new ImagickPixel('#FFFFFF'));$image->flattenImages();`? – rabudde Jul 07 '11 at 18:29
0

Try adding $image->setBackgroundColor(0xFFFFFF); after $image = new Imagick('PNG:image.tmp');

binaryLV
  • 9,002
  • 2
  • 40
  • 42
  • 1
    `PHP Fatal error: Uncaught exception 'ImagickException' with message 'Invalid parameter provided'`, using '#ffffff' instead doesn't result in a failure but doesn't help also. `setImageBackgroundColor` doesn't work too – rabudde Jul 07 '11 at 12:59