Problem converting svg with clippath to png

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
bubu
Posts: 2
Joined: 2013-02-20T02:02:21-07:00
Authentication code: 6789

Problem converting svg with clippath to png

Post by bubu »

Hi, I'm new working with imagemagick and I have a problem converting svg that contains a clippath to png. Any help is appreciated. Is urgent because I spent the last 2 days searching and testing with no result yet.

This is the svg:

Code: Select all

<svg version="1.1" width="340" height="470">

<clipPath id="imgMask" clipPathUnits="userSpaceOnUse"><path d="m276.5,1.5l-138,0c-14.35899,0 -27,16.64101 -27,31l0,336c0,13.39001 12.73,24.40997 26.091,25.83899c0.259,-1.08002 1.25,-1.83899 5.039,-1.83899l129.73999,0c3.80099,0 4.785,0.76398 5.04001,1.84998c13.11398,-1.323 24.09,-11.46997 24.09,-24.84998l0,-337c0,-14.35899 -10.64099,-31 -25,-31zm-106,50l-29,0c-11.59801,0 -21,-9.40199 -21,-21s9.40199,-21 21,-21l29,0c11.59799,0 21,9.40199 21,21s-9.40201,21 -21,21z" fill="red" stroke="#34ACE4" stroke-width="2" id="outline"></path></clipPath>

<g id="canvasPreview" clip-path="url(#imgMask)" fill-rule="evenodd">
	<image x="28.5" y="-26.5" width="343" height="441" id="userImagePrev" center="true" transform="matrix(1 0 0 1 0 0)" xlink:href="doina.jpg"></image>
</g>

<defs><filter id="blackWhitePicture"><feComposite result="litPaint" operator="arithmetic" in="SourceGraphic" in2="SourceGraphic" k1="0" k2="1" k3="0" k4="0"></feComposite><feColorMatrix result="grayscale" type="saturate" values="0"></feColorMatrix></filter></defs>
</svg>
This is what I try to do:

Code: Select all

$im = new Imagick();
	   //$im->setBackgroundColor(new ImagickPixel('transparent'));
	   $im->readImageBlob($svg);
	   $im->clipPathImage("#1", true);
	   $im->setImageFormat("png32");
	   header('Content-type: image/png');
	   echo $im;
I get this error:

Code: Select all

( ! ) Fatal error: Uncaught exception 'ImagickException' with message 'no clip path defined `/tmp/magick-XXyrLIE6': /tmp/magick-XXyrLIE6.cache @ error/image.c/ClipImagePath/739' in /var/www/casewebsite/catalog/controller/product/custom.php on line 240
( ! ) ImagickException: no clip path defined `/tmp/magick-XXyrLIE6': /tmp/magick-XXyrLIE6.cache @ error/image.c/ClipImagePath/739 in /var/www/casewebsite/catalog/controller/product/custom.php on line 240
Also from the command line

Code: Select all

convert test.svg  +clip -alpha transparent result.png
the result is missing the clippath the image is not clipped.

Thank you!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Problem converting svg with clippath to png

Post by magick »

ImageMagick extracts the clippath from the image Photoshop metadata (8BIM), if it exists. For SVG, we utilize a delegate program or library. Unless the delegate program or library extracts the clippath and encapsulates it as Photoshop metadata, ImageMagick will not be able to extract the clip path. ImageMagick supports these delegate programs and libraries to render SVG: inkscape, rsvg, or the internal SVG renderer.
bubu
Posts: 2
Joined: 2013-02-20T02:02:21-07:00
Authentication code: 6789

Re: Problem converting svg with clippath to png

Post by bubu »

Thank you for your reply. I managed to solve my problem using another image as a mask. So this topic can be closed.

Code: Select all

           $mask = new Imagick('mask.png');
	   $im = new Imagick();
	   //$im->setBackgroundColor(new ImagickPixel('transparent'));
	   $im->readImageBlob($svg);
	   $im->setImageFormat("png32");
	   $im->compositeImage( $mask, imagick::COMPOSITE_DEFAULT, 0, 0 );

	   header('Content-type: image/png');
	   echo $im;
	   
Post Reply