How can I use ImageMagick (with the php extension) to set the transparent background to white when converting an image from PNG to JPEG?
Asked
Active
Viewed 2.5k times
37
-
1Which php extension, there are three. MagickWand, IMagick or phMagick? – Orbling Mar 12 '11 at 01:16
2 Answers
53
At time of writing, you have not specified which extension you are using, but if you were using the commandline, the command would be:
convert image.png -background white -flatten -alpha off image.jpg
More information can be found on the Masking Usage documentation.
Using IMagick for instance, I think you could do this as follows:
(totally untested, never used IMagick and don't have it installed to test)
$image = new IMagick('image.png');
$flattened = new IMagick();
$flattened->newImage($image->getImageWidth(), $image->getImageHeight(), new ImagickPixel("white"));
$flattened->compositeImage($image, imagick::COMPOSITE_OVER, 0, 0);
$flattened->setImageFormat("jpg");
$flattened->writeImage('image.jpg');
$image->clear();
$image->destroy();
$flattened->clear();
$flattened->destroy();

Orbling
- 20,413
- 3
- 53
- 64
-
I believe you should use `$sizes = $image->getImageGeometry(); $width = $sizes['width']; $height = $sizes['height'];` instead of `$image->getWidth(), $image->getHeight()` – CappY Mar 12 '11 at 23:17
-
1@CappY: That works too, any reason for the function over the other two, except perhaps a few microseconds performance boost? – Orbling Mar 13 '11 at 00:15
-
@Orbling : `Fatal error: Call to undefined method Imagick::getWidth()` Dunno if it's works for you. – CappY Mar 13 '11 at 01:40
-
@CappY: How bizarre... it's in the doc: http://www.php.net/manual/en/function.imagick-getimagewidth.php – Orbling Mar 13 '11 at 07:09
-
It's `$image->getImageWidth();` and `$image->getImageHeight();` I didn't saw these functions. :) – CappY Mar 13 '11 at 07:24
-
1@Orbling, You use two steps to convert format. First you sets the new format (jpg), then you `writeImage('image.jpg')`. I found that it's not necessary to set the format. It's just enough to write desired image extension when you save image - `writeImage('image.jpg')` or `writeImage('image.gif')`. It converts format automatically. When you check saved images in console using `identify -verbose` they have exactly new formats. But there is no any official docs about this behavior. Should I use `setImageFormat()` first or it is really not necessary? – Green Aug 22 '12 at 18:35
-
@Green: Image Magick itself, or rather the command line version behaves as you say, so I would expect the `writeImage()` command to work the same as you have verified. As I say above, the code was untested by myself, just written in the browser - as the command to change format existed, I used it just in case. Dare say it allows the use of non-standard extensions, or output to the browser. Might be good style to leave it in if it causes no additional time, if it does, I would remove it and allow the automatic conversion. – Orbling Aug 23 '12 at 13:41
-
1
5
If you are using the Imagick extension:
<?php
// load the source transparent png
$i = new IMagick('image.png');
// set the background to white
// you can also use 'rgb(255,255,255)' in place of 'white'
$i->setImageBackgroundColor(new ImagickPixel('white'));
// flattens multiple layers
$i = $i->flattenImages();
// the output format
$i->setImageFormat('jpg');
// save to disk
$i->writeImage('image.jpg');
// and/or output directly
// header('Content-Type: '.$i->getFormat());
// echo $i->getImageBlob();
// cleanup
$i->clear();
$i->destroy();

Mike Causer
- 8,196
- 2
- 43
- 63
-
1Old answer but __flattenImages__ is now deprecated and the recommended way is `mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN)` http://php.net/manual/en/imagick.mergeimagelayers.php – tbleckert Mar 10 '16 at 10:04