Creating an 'erase' brush

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Post Reply
monsters

Creating an 'erase' brush

Post by monsters »

Hi there,

I'm 'drawing' on an image, like thus

Code: Select all

	DrawSetStrokeColor($drawing,$pixel);
DrawSetStrokeWidth($drawing,$thickness);
DrawSetStrokeAlpha($drawing,$opacity);
DrawSetStrokeLineCap($drawing, MW_RoundCap);
DrawSetStrokeLineJoin($drawing, MW_RoundJoin);
DrawSetFillOpacity($drawing,1);

DrawLine($drawing, 0, 0, 200, 200);
			

MagickDrawImage($source,$drawing);
what i'd like to do is 'erase' part of what i've drawn, i tried this

Code: Select all

DrawLine($drawing, 0, 0, 200, 200);
//set the stroke to transparent
DrawSetStrokeAlpha($drawing,0);
DrawLine($drawing, 0, 200, 200, 0);
but that didn't work of course.. i really just need to be able to set some pixels on my 'drawing' as transparent before applying them back to the main image, .. any suggestions?

I could create a new image with my 'erase' pen, and then apply that as a transparency mask, but i'm having some trouble finding out how to do that as well.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

As you discovered you can not 'draw' transparency.

However what you can do is create a second 'transparent' canvas and draw what you want to erase on it. The color of the 'shape' does not matter, as long as it is not transparent where you want things erased. Think of the resulting image as a shaped 'hole punch'.

This image can then be alpha composites over the destination image using 'DstOut', which will then erase that shape from the original image.

See the 'cresent moon' example, in the command line IM examples.
http://www.cit.gu.edu.au/~anthony/graph ... se/#dstout

Which draws one circle, than a second 'hole punch' circle, to erase from the first.

The PHP version of this should be straight forward.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply