Preserving Alpha with MagickCompositeImage

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
PeterUK
Posts: 7
Joined: 2013-04-10T06:54:21-07:00
Authentication code: 6789

Preserving Alpha with MagickCompositeImage

Post by PeterUK »

I'm using MagickCompositeImage to read in a "border" image which has some partial transparency in it, but when I composite it onto my background, the partial transparency is turned to black, but the fully transparent parts are still transparent.

Any tips on what I'm doing wrong?

Code: Select all

// Read in images
$image_body = NewMagickWand();
$image_header = NewMagickWand();
$border_img = NewMagickWand();
MagickReadImage($image_body, "body.png");
MagickReadImage($image_header, "header.png");
MagickReadImage($border_img, "border.png");

// Create main canvas
$main_image = NewMagickWand();
MagickSetFormat($main_image, 'PNG24');
$transparent = NewPixelWand();
PixelSetColor($transparent, 'none');
MagickNewImage($main_image, 350, 150, $transparent);
MagickCompositeImage($main_image, $image_body, MW_OverCompositeOp, 2, 26);
MagickCompositeImage($main_image, $image_header, MW_OverCompositeOp, 2, 2);
MagickCompositeImage($main_image, $border_img, MW_OverCompositeOp, 0, 0);

header("Content-Type: image/png");
MagickEchoImageBlob($main_image);
I tried using MW_CopyOpacityCompositeOp instead of MW_OverCompositeOp when copying the border but then it just seems to copy nothing at all.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Preserving Alpha with MagickCompositeImage

Post by el_supremo »

Try enabling the alpha channel in the main image after creating it:

Code: Select all

MagickSetImageAlphaChannel($main_image,MW_SetAlphaChannel);
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
PeterUK
Posts: 7
Joined: 2013-04-10T06:54:21-07:00
Authentication code: 6789

Re: Preserving Alpha with MagickCompositeImage

Post by PeterUK »

Thanks, I already tried that and I tried setting it to the border image as well.

I also tried this:

Code: Select all

MagickSetImageAlphaChannel($main_image, MW_ActivateAlphaChannel);
MagickSetImageAlphaChannel($border_img, MW_ActivateAlphaChannel);
For both and that didn't work either.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Preserving Alpha with MagickCompositeImage

Post by anthony »

You are writing to a PNG24 format... that format has no transparency. so any transparency left after the
compositions will be lost (making the hidden fully-transparent color visible).

Try using PNG32

PS; I would move the 'SetFormat' to just before you write as that is where it is needed. Yes it can be set before but that disconnects the relevant setting from the operator.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
PeterUK
Posts: 7
Joined: 2013-04-10T06:54:21-07:00
Authentication code: 6789

Re: Preserving Alpha with MagickCompositeImage

Post by PeterUK »

anthony wrote:You are writing to a PNG24 format... that format has no transparency. so any transparency left after the
compositions will be lost (making the hidden fully-transparent color visible).

Try using PNG32

PS; I would move the 'SetFormat' to just before you write as that is where it is needed. Yes it can be set before but that disconnects the relevant setting from the operator.
Thank you, that sorted it out!
ralphysunsmith
Posts: 1
Joined: 2015-03-11T03:48:00-07:00
Authentication code: 6789

Re: Preserving Alpha with MagickCompositeImage

Post by ralphysunsmith »

Thank you for giving this suggestion, I have encountered this same format before and I was totally lost how to figure it out, It has given me an idea on how to work on it.
Post Reply