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?
Convertation SVG to PNG with transparency
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07:00
Re: Convertation SVG to PNG with transparency
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:
Pete
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
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.
See my message in this topic for a link to a zip of all the files.
Re: Convertation SVG to PNG with transparency
Thank you. It works!
Re: Convertation SVG to PNG with transparency
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.