Using a transparent PNG as a clip mask

☆樱花仙子☆ 提交于 2019-11-27 16:26:02

问题


Is it possible to take this image:

And apply this mask:

And turn it into this:

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


回答1:


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;



回答2:


I think you are looking for imagealphablending. I use it for watermarks, and I believe it will do the effect you are looking for.




回答3:


Great work with (ImageMagick) NOT GD .. I see the tags of this question is GD!!

Here is a GD version at this link: PHP GD Use one image to mask another image, including transparency



来源:https://stackoverflow.com/questions/1741488/using-a-transparent-png-as-a-clip-mask

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!