Imagick convert SVG to PNG - colors replaced by black and white

3.3k Views Asked by At

I'am trying to convert an SVG Image which is created by the SVGGraph library ( http://www.goat1000.com/svggraph.php).

The SVG is colored (red, green, yellow, gray, ...) in the browser and everything is fine. But when I convert it, its just black and white.

With this code I convert it:

//new instance of imagick
$im = new Imagick();
//read the svg file/data (its not saved on the filesystem)
$im->readImageBlob($svgFile);

$im->setImageFormat("png24");
$im->writeImage('cylinder.png');
$im->clear();
$im->destroy();

I've tried it with jpeg and png as output format but the result is the same, all colors will be replaced with black

does anyone have an idea how to fix that ?

1

There are 1 best solutions below

0
Manolo On

Try this way:

$im = new Imagick();
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->readImageBlob($svgFile);

$im->setImageFormat("png24");
$im->writeImage('cylinder.png');
$im->clear();
$im->destroy()