How to edit and draw SVGs from XML SVG string?

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
jordsta95
Posts: 2
Joined: 2018-10-06T12:08:44-07:00
Authentication code: 1152

How to edit and draw SVGs from XML SVG string?

Post by jordsta95 »

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:

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" />';
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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to edit and draw SVGs from XML SVG string?

Post by fmw42 »

I am not sure I understand fully. But Imagemagick is a raster processor. It has to rasterize your SVG file first before it can do anything else. So I do not think you can modify colors via vector processing. You would have change colors after it is rasterized. Once it is rasterized, you can then manipulate its positioning using -set page for example. You should then be able to combine different raster images and this rasterized SVG file into some layered output format, such as PSD (or TIFF multi-paged format) or flatten all your layers and save to PNG or JPG.

I do not believe that you can "draw" SVG file. You must read them as a normal file and IM will rasterize it.

If you need to change the SVG color, then you would have to use some text editor to modify the vector XML/SVG file; otherwise, you would have to do that after rasterizing using -fuzz XX% -fill newcolor -opaque oldcolor.

Sorry I know only a little about Imagick.
jordsta95
Posts: 2
Joined: 2018-10-06T12:08:44-07:00
Authentication code: 1152

Re: How to edit and draw SVGs from XML SVG string?

Post by jordsta95 »

Alright, that's not a problem. I can pass a variable to the SVG, and set the fill colour on it that way, no biggie there. If there was a way imagick could have done it, that would have been cool. Although, would it keep the transparent background when rasterizing?

Then I'd imagine I just use $draw->scale() and $draw->translate() to resize/move the SVG, again, not a problem.

The only issue now is making it so ImagickDraw can use the SVG, and then draw it to the Imagick created image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to edit and draw SVGs from XML SVG string?

Post by fmw42 »

Although, would it keep the transparent background when rasterizing?
It should if the SVG has specified a transparent background. If that does not work in command line, I would try "-background none" before reading in the SVG file. You should be able to do that in Imagick.

Perhaps one of the other IM users who know more about SVG files can add to the discussion or correct my statements.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to edit and draw SVGs from XML SVG string?

Post by snibgo »

As Fred says.

According to https://secure.php.net/manual/en/imagic ... aphics.php, setVectorGraphics is used to restore stuff that has been saved into a string with getVectorGraphics. And drawImage() is like a CLI "-draw" operation, drawing vector objects like "circle" that are given directly to IM, not via SVG.

That isn't how to read SVG files. SVG files are read in exactly the same way as any other input image format like PNG or JPG. This creates an in-memory copy of a raster version of the SVG. And then you can process it like any other raster image.
snibgo's IM pages: im.snibgo.com
Post Reply