How to edit and draw SVGs from XML SVG string?
Posted: 2018-10-06T12:23:03-07:00
Hi,
I have the XML for an SVG, but I'm struggling to understand how I can manipulate it so I can change its colour/add it as a layer to a greater Imagick object.
I can do readImageBlob and then echo out the results, and see my SVG just fine. But when I use http://php.net/manual/en/imagickdraw.se ... aphics.php (which I'm only assuming does what I am expecting) I get an error stating that imagick cannot draw this layer.
Here is the code which I have:
Ideally, what I would like is to be able to add $svg as a layer, with a transparent background (there will potentially be layers above and below $svg), on top of $im - and, if possible, alter the size/colour/position of the SVG before it is written to $im.
As mentioned earlier, I doubt setVectorGraphic is the correct function to allow the SVG to pass to drawImage (seeing as no matter what I do, it fails), so if there is a better way to do this, which will allow me to manipulate it better, then I am all ears!
I have the XML for an SVG, but I'm struggling to understand how I can manipulate it so I can change its colour/add it as a layer to a greater Imagick object.
I can do readImageBlob and then echo out the results, and see my SVG just fine. But when I use http://php.net/manual/en/imagickdraw.se ... aphics.php (which I'm only assuming does what I am expecting) I get an error stating that imagick cannot draw this layer.
Here is the code which I have:
Code: Select all
$height = 520;
$width = 820;
$backgroundColour = "blue";
$im = new Imagick();
$im->newImage( $width, $height, new ImagickPixel( $backgroundColour ) );
$svg = $this->svgXML;
$draw = new ImagickDraw();
$draw->setVectorGraphics($svg);
$im->drawImage( $draw );
$im->setImageFormat( "png" );
echo '<img src="data:image/png;base64,'.base64_encode($im->getImageBlob()).'" alt="" class="renderedImage" />';
As mentioned earlier, I doubt setVectorGraphic is the correct function to allow the SVG to pass to drawImage (seeing as no matter what I do, it fails), so if there is a better way to do this, which will allow me to manipulate it better, then I am all ears!