imagemagick - PHP Imagick clipping mask results in dark halo edgesGet the solution ↓↓↓
I have two png layers, both with transparent backgrounds, that I'm trying to combine in Imagick in PHP. One is a shape, another is reflections to be applied on top of that shape. However the reflections were drawn overlapping outside the edges of the shape, so I need to clip them to the shape (remove the parts of the reflections that are not inside the shape) when layering it on top, and preserving the alpha channels.
All the examples I can find online do it like this;
$shape->compositeImage($reflections, \Imagick::COMPOSITE_DSTIN, 0, 0);
It works to apply and clip the reflections, but it results in a dark "halo" around the soft edges of the reflections. I believe what is happening is it's giving the soft alpha edges of the reflections a black background and then deleting the alpha. But I need the "shape" to be the background of the reflections, so the edges of the reflections alpha blend in to the shape behind.
How do I apply the reflections to the shape and also clip it inside the shape, without dark halos? I tried applying it then clipping the entire thing against the original shape. That makes the reflections work nicely, except now the shape itself has a dark halo around the edges. So I think I'm just using masking incorrectly, but I can't figure out what the right way is to do it.
This is the shape image;
This is the reflections image;
This is what I'm currently getting from the above code;
I do not know Imagick well. So here is how to do it in ImageMagick command line. It should be relatively easy to convert the commands to Imagick.
Basically, you separate the alpha channels of the two images and then multiply them together to clip the highlight alpha with the blob alpha. Then you put the result as the new alpha channel back into the highlight image. Then you composite that result as the new highlight image over the blob image.
convert blob.png highlight.png -alpha extract -compose multiply -composite new_highlight_alpha.png convert highlight.png new_highlight_alpha.png -alpha off -compose copy_opacity -composite new_highlight.png convert blob.png new_highlight.png -compose over -composite result.png
So is this result what you want?
Taking the commands from fmw42 I managed to come up with the following PHP code that seems to do the same thing, and works;
//$shape is my first image, $reflection is my second image, as Imagick instances //extract shape alpha channel $shapeAlpha = clone $shape; $shapeAlpha->separateimagechannel(\Imagick::CHANNEL_ALPHA); //extract reflection alpha channel $reflectionAlpha = clone $reflection; $reflectionAlpha->separateimagechannel(\Imagick::CHANNEL_ALPHA); //merge both alpha channels $shapeAlpha->compositeImage($reflectionAlpha, \Imagick::COMPOSITE_MULTIPLY, 0, 0); $combinedAlpha = clone $shapeAlpha; //clip the reflection with the combined alpha channel $reflection->setImageMatte(false); $combinedAlpha->setImageMatte(false); $reflection->compositeImage($combinedAlpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0); //apply the reflection to the shape $shape->compositeImage($reflection, \Imagick::COMPOSITE_OVER, 0, 0); //$shape is now the final composite image as intended, no dark halo
People are also looking for solutions of the problem: use the option --with-all-dependencies (-w) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.