本文介绍了使用IMagick / ImageMagick使图像的一部分透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望使用IMagick使图像的一部分(或实际上是几个部分)透明,这样我就可以将它用作不同图像上的遮罩。我无法用简单的方式想出任何方式。
I wish to make part (or in fact, several parts) of an image transparent using IMagick, such that I can use it as a mask over a different image. I can't figure out any way of doing this in an simple fashion.
所以说我的起始图像如下所示,其中X是任何颜色:
So say my starting image is represented as below, where X is any color:
XXXXXXXXXXXXX
XXXXXXXXXXXXX
XXXXXXXXXXXXX
XXXXXXXXXXXXX
XXXXXXXXXXXXX
然后我希望能够使某些矩形区域透明(所以它最终有点像打卡):
Then I want to be able to make certain rectangular regions transparent (so it ends up a bit like a punch-card):
XXXXXXXXXXXXX
X XXXXXXXXXX
X XXXX XXXX
XXXXXXX XXXX
XXXXXXXXXXXXX
有谁知道这样做的好方法?谢谢。
Does anyone know of a good way of doing this? Thanks.
推荐答案
想出来。
//Open your image and get its dimensions
$image = new Imagick('image.png');
$height = $image->getImageHeight();
$width = $image->getImageWidth();
//Create a new transparent image of the same size
$mask = new Imagick();
$mask->newImage($width, $height, new ImagickPixel('none'));
$mask->setImageFormat('png');
//Draw onto the new image the areas you want to be transparent in the original
$draw = new ImagickDraw();
$draw->setFillColor('black');
$draw->rectangle( 10,10,100,100 );
$mask->drawImage( $draw );
//Composite the images using Imagick::COMPOSITE_DSTOUT
$image->compositeImage($mask, Imagick::COMPOSITE_DSTOUT, 0, 0, Imagick::CHANNEL_ALPHA);
这篇关于使用IMagick / ImageMagick使图像的一部分透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!