Tarek Sami
Tarek Sami

Reputation: 23

Issue with setVectorGraphics method in Imagick?

I'm working on a task which requires parsing SVG path data for use as a clip mask in Imagick... right now, I'm getting errors trying to use the setVectorGraphics method- the following code produces an error message like:

Fatal error: Uncaught exception 'ImagickException' with message 'Unable to draw image'

$draw = new \ImagickDraw();
$draw->setFillColor("red");
$draw->circle(20, 20, 50, 50);
$draw->setFillColor("blue");
$draw->circle(50, 70, 50, 50);
$draw->rectangle(50, 120, 80, 150);
//Get the drawing as a string
$SVG = $draw->getVectorGraphics();

$draw2 = new \ImagickDraw();
$draw2->setVectorGraphics($SVG);
$imagick = new \Imagick();
$imagick->newImage(200, 200, 'white');
$imagick->setImageFormat("png");
$imagick->drawImage($draw2);
header("Content-Type: image/png");
echo $imagick->getImageBlob();

(code comes from this demo)

I'm pretty sure the usage of Imagick and ImagickDraw here is correct... is this a bug in the Imagick library? If so, is there some way to work around the issue?

Thanks!

Upvotes: 2

Views: 315

Answers (1)

emcconville
emcconville

Reputation: 24419

I'm pretty sure the usage of Imagick and ImagickDraw here is correct

Correct. PHP's implementation of ImageMagick's C-API, in this instance, is complete.

... is this a bug in the Imagick library?

Yes. This feature has a history of issues, and behavior has changed across ImageMagick version. Ensure you have the latest version, author a test case, and submit a bug report.

If so, is there some way to work around the issue?

The ImagickDraw::getVectorGraphics returns a string of xml nodes, but not a complete XML document. Your calling code needs to generate the missing parent node.

$SVG = $draw->getVectorGraphics();
// ....
$draw2->setVectorGraphics('<root>' . $SVG . '</root>');

Upvotes: 1

Related Questions