Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

php - How to use Imagick to merge and mask images?

I know very little of image processing and even less of the terminology used, so please bear with me.

Basically, I want to merge two images together where one of them will act as a mask. That image looks something like this:
Example
Where the blue and yellow background are both transparent in reality.

This image is being used as a mask for regular photo's. Parts of the photo that 'stick out' of the circle need to be 'cropped' (be made invisible) while the inside remains visible.
So everything that comes in the blue area is invisible, everything in the yellow area is visible.

I honestly have no clue how to go about it so any help would be greatly appreciated!

Edit:
I use the API version of Imagick, not the commandline version

Edit:
To get a feel of what I want to achieve, here is an example.

The input images are thus:
enter image description here
This is the mask image, always the same

enter image description here
This is an example of a picture, dynamic

enter image description here
This is what the end result should look like

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, finally, this should do what you need:

Original image:

http://i.stack.imgur.com/b7seR.png

Opacity mask:

enter image description here

Overlay:

http://i.stack.imgur.com/3ulkM.png

Output:

enter image description here

The code:

<?php
$base = new Imagick('U0R4F.png');
$mask = new Imagick('mask.png');
$over = new Imagick('3ulkM.png');

// Setting same size for all images
$base->resizeImage(274, 275, Imagick::FILTER_LANCZOS, 1);

// Copy opacity mask
$base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);

// Add overlay
$base->compositeImage($over, Imagick::COMPOSITE_DEFAULT, 0, 0);

$base->writeImage('output.png');
header("Content-Type: image/png");

echo $base;
?>

I hope it's right now! Note: In your example it looks like you downscaled the base image, which I didn't (my goal is just to show how the masking is done).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...