Page 1 of 1

Convertation SVG to PNG with transparency

Posted: 2013-01-19T14:31:36-07:00
by neyronius
Below source code that converts SVG image to png

//Wrong result
$mw = NewMagickWand();
$transparentColor = NewPixelWand();
PixelSetColor($transparentColor, 'transparent');
MagickReadImage($mw, 'from.svg');
MagickSetImageBackgroundColor($mw, $transparentColor);
MagickSetImageFormat($mw, 'png32');
MagickWriteImage($mw, 'to.png'); //Image with white color instead of transparent

//Works OK
$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readimage('from.svg');
$im->setImageFormat("png32");
$im->writeimage('to.png'); //Image with transparent background

I think the error in ImageMagick extension. Do you have any ideas how to convert SVG to PNG with transparency using MagickWand?

Re: Convertation SVG to PNG with transparency

Posted: 2013-01-19T17:06:28-07:00
by el_supremo
Those two do not look equivalent. In the one that works, you set background colour to transparent and then read the image. In the one that doesn't work, you read the image first and then use MagickSetImageBackgroundColor (which is not the same as MagickSetBackgroundColor).
Try this:

Code: Select all

//Wrong result
$mw = NewMagickWand();
$transparentColor = NewPixelWand();
PixelSetColor($transparentColor, 'transparent');
MagickSetBackgroundColor($mw, $transparentColor);
MagickReadImage($mw, 'from.svg');
MagickSetImageFormat($mw, 'png32');
MagickWriteImage($mw, 'to.png'); //Image with white color instead of transparent
Pete

Re: Convertation SVG to PNG with transparency

Posted: 2013-01-19T23:45:33-07:00
by neyronius
Thank you. It works!

Re: Convertation SVG to PNG with transparency

Posted: 2013-01-20T11:13:43-07:00
by neyronius
But what about transparency after resizing?

Code: Select all

$mw = NewMagickWand();
$transparentColor = NewPixelWand();
PixelSetColor($transparentColor, 'transparent');
MagickSetBackgroundColor($mw, $transparentColor);
MagickReadImage($mw, 'from.svg');

MagickResizeImage($mw, 300, 300, MW_LanczosFilter, 0); //Key point

MagickSetImageFormat($mw, 'png32');
MagickWriteImage($mw, 'to.png'); //Black image if we use resize. Transparent else.