Convertation SVG to PNG with transparency

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
neyronius
Posts: 3
Joined: 2013-01-19T14:25:21-07:00
Authentication code: 6789

Convertation SVG to PNG with transparency

Post 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?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Convertation SVG to PNG with transparency

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
neyronius
Posts: 3
Joined: 2013-01-19T14:25:21-07:00
Authentication code: 6789

Re: Convertation SVG to PNG with transparency

Post by neyronius »

Thank you. It works!
neyronius
Posts: 3
Joined: 2013-01-19T14:25:21-07:00
Authentication code: 6789

Re: Convertation SVG to PNG with transparency

Post 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.
Post Reply