Skip to content

Instantly share code, notes, and snippets.

@bytespider
Created November 8, 2016 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytespider/6e07a82edd3ce2693a007cd0aa8af93d to your computer and use it in GitHub Desktop.
Save bytespider/6e07a82edd3ce2693a007cd0aa8af93d to your computer and use it in GitHub Desktop.
Setting the background colour of a transparent PNG when converted to JPEG
<?php
$image = new Imagick('kiwi.png'); // https://s13.postimg.org/hakfstg1j/kiwi.png
$image->setImageBackgroundColor(new ImagickPixel('white')); // set the background color of the image
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_BACKGROUND); // ALPHACHANNEL_BACKGROUND is undocumented and has a value of 2
// save a JPEG
$image->setImageFormat('jpeg');
$image->setImageCompression(Imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(80);
$image->writeImage('kiwi.jpg');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment