Convert PNG to JPG and set transparent background to white with ImageMagick and PHP

21,467

Solution 1

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();

Solution 2

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();
Share:
21,467
John
Author by

John

Updated on October 31, 2020

Comments

  • John
    John over 3 years

    How can I use ImageMagick (with the php extension) to set the transparent background to white when converting an image from PNG to JPEG?

  • CappY
    CappY over 13 years
    I believe you should use $sizes = $image->getImageGeometry(); $width = $sizes['width']; $height = $sizes['height']; instead of $image->getWidth(), $image->getHeight()
  • Orbling
    Orbling over 13 years
    @CappY: That works too, any reason for the function over the other two, except perhaps a few microseconds performance boost?
  • CappY
    CappY over 13 years
    @Orbling : Fatal error: Call to undefined method Imagick::getWidth() Dunno if it's works for you.
  • Orbling
    Orbling over 13 years
    @CappY: How bizarre... it's in the doc: php.net/manual/en/function.imagick-getimagewidth.php
  • CappY
    CappY over 13 years
    It's $image->getImageWidth(); and $image->getImageHeight(); I didn't saw these functions. :)
  • Green
    Green almost 12 years
    @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?
  • Orbling
    Orbling almost 12 years
    @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.
  • tbleckert
    tbleckert over 8 years
    Old answer but flattenImages is now deprecated and the recommended way is mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN) php.net/manual/en/imagick.mergeimagelayers.php