CopyOpacity equivalent for php IMagick?

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
Cadrach

CopyOpacity equivalent for php IMagick?

Post by Cadrach »

Hi there,

I am new to ImageMagick but already fell in love with it ;)

However I am currently stuck with a small issue:
I have found many command line examples of a mask used to specify the opacity zones of an image (like in this jigsaw example: http://www.imagemagick.org/Usage/advanced/)

I would like to do the same using the php library, but I am stuck at the moment with:

Code: Select all

$img = new Imagick('myImage.png'); //Full image
$mask = new Imagick('mask.png'); //Black&White mask
$img->compositeImage($mask,Imagick::COMPOSITE_MULTIPLY,0,0,Imagick::CHANNEL_ALPHA);
This does not work as I would like since the black zones of the mask render black on the final image (instead of fully transparent). I have played with the COMPOSITE and CHANNEL constants to no avail.

Is there a way to do it?
Last edited by Cadrach on 2008-07-26T11:07:18-07:00, edited 1 time in total.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: CopyOpacity equivalent for php IMagick?

Post by el_supremo »

The copyopacity composite operator does not seem to be defined in Imagick. If you can use MagickWand for PHP, it is defined there.

Pete
flemer

Re: CopyOpacity equivalent for php IMagick?

Post by flemer »

Cadrach,

I have upgraded ImageMagick to version 6.4.2, because I found that channel parameter is ignored in ImageMagick below 6.2.8. But I am still having the same issue.

I am trying to add a gradient mask to an image.

Alternatively, I have used a png image which has the gradient transparency (mask.png) and composited the image atop. At this manner the transparency of the fist image is inerited by the second.

Code: Select all

$canvas = new Imagick();
$canvas->newImage(760, 480, "transparent");
$canvas->setFormat("PNG");

$mask = new Imagick('mask.png');
$canvas->compositeImage($mask, imagick::COMPOSITE_COPY, 0, 0);

$image = new Imagick('imagem.png');
$canvas->compositeImage($image, imagick::COMPOSITE_ATOP, 0, 0);

$canvas->writeImage("final.png");
Does anybody knows a better way to do it?
Attachments
mask.png
mask.png (2.71 KiB) Viewed 20771 times
imagem.jpg
imagem.jpg (24.91 KiB) Viewed 20757 times
Cadrach

Re: CopyOpacity equivalent for php IMagick?

Post by Cadrach »

Thanks, I will try that and let you now, basically what I was trying to do was:

mask+image = final

(btw I was not able to do it using MagicDraw, DrawComposite only allows to compose a Draw with an image it seems)
Attachments
Greyscale Mask
Greyscale Mask
wall.png (849 Bytes) Viewed 20703 times
Final image
Final image
final.png (16.4 KiB) Viewed 20703 times
test.jpg
test.jpg (3.68 KiB) Viewed 20704 times
Last edited by Cadrach on 2008-07-30T01:34:13-07:00, edited 1 time in total.
Cadrach

Re: CopyOpacity equivalent for php IMagick?

Post by Cadrach »

I used your code and your image files and the resulting image does not have any transparency. I checked my Imagick version using getVersion() and got 6.3.3. Where did you get the 6.4.2? I installed the latest PECL dll for php 5.2.5, and the 6.4.2 ImageMagick software, Q16 (I am under Windows XP)
flemer

Re: CopyOpacity equivalent for php IMagick?

Post by flemer »

Cadrach,

Try the following code using the png image (mask.png) attached to this message. Note that mask.png must contain transparent pixels instead of black and white colors. The color of the pixels does not matter.

Code: Select all

$canvas = new Imagick();
$canvas->newImage(71, 123, "transparent");
$canvas->setFormat("PNG");

$mask = new Imagick('wall.png');
$canvas->compositeImage($mask, imagick::COMPOSITE_COPY, 0, 0);

$image = new Imagick('test.jpg');
$canvas->compositeImage($image, imagick::COMPOSITE_ATOP, 0, 0);

$canvas->writeImage("final.png");
Latest ImageMagick Release:
http://sourceforge.net/project/showfile ... p_id=24099
Attachments
wall.png
wall.png (27.82 KiB) Viewed 20724 times
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: CopyOpacity equivalent for php IMagick?

Post by mkoppanen »

Imagick composite constants:

IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DEFAULT", OverCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_UNDEFINED", UndefinedCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_NO", NoCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_ADD", AddCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_ATOP", AtopCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_BLEND", BlendCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_BUMPMAP", BumpmapCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_CLEAR", ClearCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COLORBURN", ColorBurnCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COLORDODGE", ColorDodgeCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COLORIZE", ColorizeCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COPYBLACK", CopyBlackCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COPYBLUE", CopyBlueCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COPY", CopyCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COPYCYAN", CopyCyanCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COPYGREEN", CopyGreenCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COPYMAGENTA", CopyMagentaCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COPYOPACITY", CopyOpacityCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COPYRED", CopyRedCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_COPYYELLOW", CopyYellowCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DARKEN", DarkenCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DSTATOP", DstAtopCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DST", DstCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DSTIN", DstInCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DSTOUT", DstOutCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DSTOVER", DstOverCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DIFFERENCE", DifferenceCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DISPLACE", DisplaceCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_DISSOLVE", DissolveCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_EXCLUSION", ExclusionCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_HARDLIGHT", HardLightCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_HUE", HueCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_IN", InCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_LIGHTEN", LightenCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_LUMINIZE", LuminizeCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_MINUS", MinusCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_MODULATE", ModulateCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_MULTIPLY", MultiplyCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_OUT", OutCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_OVER", OverCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_OVERLAY", OverlayCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_PLUS", PlusCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_REPLACE", ReplaceCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_SATURATE", SaturateCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_SCREEN", ScreenCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_SOFTLIGHT", SoftLightCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_SRCATOP", SrcAtopCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_SRC", SrcCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_SRCIN", SrcInCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_SRCOUT", SrcOutCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_SRCOVER", SrcOverCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_SUBTRACT", SubtractCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_THRESHOLD", ThresholdCompositeOp );
IMAGICK_REGISTER_CONST_LONG( "COMPOSITE_XOR", XorCompositeOp );

Some documented better than the others..
Mikko Koppanen
My blog: http://valokuva.org
flemer

Re: CopyOpacity equivalent for php IMagick?

Post by flemer »

Koppanen, thank you very much.

The best way I found to add gradient transparency to an image:

Code: Select all

$mask = new Imagick('mask.png'); // gray scale mask (without transparency)
$mask->setImageMatte(false); // any alpha channel must be disabled

$image = new Imagick("imagem.jpg");
$image->setFormat("PNG");

/* When the overlay (source) image has no matte (alpha or matte) channel,
then COPYOPACITY operator will treat it as a simple grey-scale image mask. */
$image->compositeImage($mask, imagick::COMPOSITE_COPYOPACITY, 0, 0);
$image->writeImage("final.png");
Cadrach

Re: CopyOpacity equivalent for php IMagick?

Post by Cadrach »

Yes, imagick::COMPOSITE_COPYOPACITY worked perfectly for me, thanks a lot!
Post Reply