Using SVG path definitions as clip mask in ImagickDraw

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
tsami
Posts: 2
Joined: 2015-03-18T09:29:46-07:00
Authentication code: 6789

Using SVG path definitions as clip mask in ImagickDraw

Post by tsami »

Hello, new to IM, so apologies if this question has already been answered (although I did not find it after searching).

I have been given a series of .svg files, each containing an outline which we need to use as a clipping mask. I am wondering if there is some way to use the path definition ("d" attribute in the svg tag) as an argument to some method in ImagickDraw to draw the path.

For example, here is an .svg file that I'm working with:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 width="792px" height="612px" viewBox="0 0 792 612" enable-background="new 0 0 792 612" xml:space="preserve">
<path fill-rule="evenodd" clip-rule="evenodd" fill="none" stroke="#009FE3" stroke-miterlimit="10" d="M338.196,350.536
	c0-3.727,3.018-6.744,6.743-6.744h94.513c3.726,0,6.744,3.018,6.744,6.744v35.111c0,3.727-3.019,6.744-6.744,6.744h-94.513
	c-3.726,0-6.743-3.018-6.743-6.744V350.536z"/>
</svg>
I'd like to be able to take the "d" attribute, and draw it into a clipping mask (I don't care about the stroke color or any other information besides the path). Open to other suggestions as well, if I'm approaching this all wrong. Thanks in advance for any help you could provide.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using SVG path definitions as clip mask in ImagickDraw

Post by snibgo »

[Mod note: as your queston is about IMagick, rather than the development of ImageMagick, I'm moving it to the IMagick forum.]
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using SVG path definitions as clip mask in ImagickDraw

Post by snibgo »

I know nothing about IMagick.

With ImageMagick, you can draw with the contents of the "d" attribute. For example (Windows BAT syntax):

Code: Select all

%IM%convert -size 600x600 xc: ^
  -draw "path 'M338.196,350.536c0-3.727,3.018-6.744,6.743-6.744h94.513c3.726,0,6.744,3.018,6.744,6.744v35.111c0,3.727-3.019,6.744-6.744,6.744h-94.513c-3.726,0-6.743-3.018-6.743-6.744V350.536z'" ^
  p.png
This gives a black round-corner rectangle on a white background. This can then be used to mask out an image, for example by making that part of the image transparent, then flattening against some colour.

As an alternative, you can directly render the SVG file:

Code: Select all

convert clippath.svg s.png
However, this SVG makes a blue outline. It would work better if it was filled.

I suppose IMagick can work similarly.
snibgo's IM pages: im.snibgo.com
tsami
Posts: 2
Joined: 2015-03-18T09:29:46-07:00
Authentication code: 6789

Re: Using SVG path definitions as clip mask in ImagickDraw

Post by tsami »

sorry for posting to the incorrect board, and thanks for your answer...

I have come across similar examples using

Code: Select all

-draw "path ..."
elsewhere... but the ImagickDraw class in php doesn't seem to have an analogous "path" method. Unless I'm mistaken, it seems the only way to do this is by breaking the complete path into a series of individual drawing instructions, although it looks like I may be able to streamline this by converting my files to .mvg

Going to continue experimenting and post my results here... it would be really nice if ImagickDraw had a path method, I feel like there would be many common use cases for it.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using SVG path definitions as clip mask in ImagickDraw

Post by snibgo »

IMagick isn't created or supported by ImageMagick developers. As far as I know, IMagick developers don't read this forum. So if you want a new feature in IMagick, I'm afraid this isn't the best place. (Don't ask me where might be!)
snibgo's IM pages: im.snibgo.com
Post Reply