使用透明的PNG作为剪贴蒙版 [英] Using a transparent PNG as a clip mask

查看:541
本文介绍了使用透明的PNG作为剪贴蒙版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以拍摄这张照片:

Is it possible to take this image:

并应用此蒙版:

将其变成这样:

使用GD还是Imagick?我知道可以使用形状遮罩图像,但是我不确定如何继续使用预先创建的alphatransparent图像进行处理. :s

Using either GD or Imagick? I know it's possible to mask an image using shapes but I'm not sure how to go on about doing it with a pre-created alphatransparent image. :s

推荐答案

使用Imagick和ImageMagick版本> 6(我不知道它是否适用于旧版本):

Using Imagick and ImageMagick version > 6 (I don't know if it will work on older versions):

// Set image path
$path = '/path/to/your/images/';

// Create new objects from png's
$dude = new Imagick($path . 'dude.png');
$mask = new Imagick($path . 'dudemask.png');

// IMPORTANT! Must activate the opacity channel
// See: http://www.php.net/manual/en/function.imagick-setimagematte.php
$dude->setImageMatte(1); 

// Create composite of two images using DSTIN
// See: http://www.imagemagick.org/Usage/compose/#dstin
$dude->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0);

// Write image to a file.
$dude->writeImage($path . 'newimage.png');

// And/or output image directly to browser
header("Content-Type: image/png");
echo $dude;

这篇关于使用透明的PNG作为剪贴蒙版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆