php 如何使用 Imagick 合并和遮罩图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8699228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:18:23  来源:igfitidea点击:

How to use Imagick to merge and mask images?

phpimagemagickimagick

提问by Dennis Haarbrink

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

编辑:
我使用 Imagick 的 API 版本,而不是命令行版本

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

在此处输入图片说明
这就是最终结果的样子

回答by user703016

So, finally, this should do what you need:

所以,最后,这应该做你需要的:

Original image:

原图:

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

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

Opacity mask:

不透明蒙版:

enter image description here

在此处输入图片说明

Overlay:

覆盖:

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

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).

我希望是现在!注意:在您的示例中,您似乎缩小了基本图像的比例,而我没有这样做(我的目标只是展示遮罩是如何完成的)。

回答by Justin T.

Did you try this solution here as described by : https://stackoverflow.com/a/2351173/1093649?

您是否按照以下所述尝试使用此解决方案:https: //stackoverflow.com/a/2351173/1093649

Run this in your server (with the right image names!), and let us know, thanks.

在您的服务器中运行它(使用正确的图像名称!),然后让我们知道,谢谢。

nb : credits go to jspash

注意:学分转到jspash

回答by Viruzzo

http://www.imagemagick.org/Usage/compose/#dstinshould do the trick, but you need to use images with alpha channels (that shouldn't be a problem).

http://www.imagemagick.org/Usage/compose/#dstin应该可以解决问题,但您需要使用带有 alpha 通道的图像(这应该不是问题)。

Edit: in PHP you have to pass it (imagick::COMPOSITE_DSTIN) as a parameter in compositeimage. Other filters in Composite Operator Constantsmay also be useful to you.

编辑:在 PHP 中,您必须将它 ( imagick::COMPOSITE_DSTIN) 作为参数传递给Compositeimage复合运算符常量中的其他过滤器也可能对您有用。

回答by anthony

This type of masking is exampled using a number of different techniques in ImageMagick Examples, Thumbnails, Mask and Paint http://www.imagemagick.org/Usage/thumbnails/#mask_paint

在 ImageMagick 示例、缩略图、蒙版和绘画http://www.imagemagick.org/Usage/thumbnails/#mask_paint 中使用了许多不同的技术来举例说明这种类型的蒙版

Be warned however that the masking and the edges of the overlay image SHOULD NOT MATCH this is important or you may get problems with edge aliasing effects that is best avoided.

但是请注意,覆盖图像的遮罩和边缘不应匹配,这一点很重要,否则您可能会遇到最好避免的边缘混叠效果问题。

Extracting an alpha mask of the ring, can be done using morphology operators to thin it down to a centerline can be used to generate a mask for any random 'ring' shape. ImageMagick Examples, Skeletons by Thinning, and Pruning http://www.imagemagick.org/Usage/morphology/#thinning_skeleton

提取环的 alpha 掩码,可以使用形态学算子将其细化到中心线,可用于为任何随机“环”形状生成掩码。ImageMagick 示例、通过细化和修剪的骨架 http://www.imagemagick.org/Usage/morphology/#thinning_skeleton

Anthony Thyssen Web Master for ImageMagick Examples, and Developer for ImageMagick

Anthony Thyssen ImageMagick 示例的 Web 管理员和 ImageMagick 的开发人员

PS: nice photo from Elfling

PS:来自 Elfling 的漂亮照片