joepie91's Ramblings

home RSS

How to convert SVG files to PNG using ImageMagick in PHP, retaining the alpha channel

23 Nov 2012

Right, I've been frustrating myself over this one for probably almost an hour now, and I finally found the solution buried somewhere on a forum. Turns out that the trick is setting a transparent background first.

So, in order to convert SVG to PNG with the PHP IMagick extension, without losing your alpha channel, do the following:

$image = new IMagick();  
$image->setBackgroundColor(new ImagickPixel('transparent'));  
$image->readImageBlob($svg_data_as_string);  
$image->setImageFormat("png32");