【发布时间】:2018-03-05 11:02:41
【问题描述】:
我有一张用于测试目的的图片。这里是:
我已经能够用 PHP Imagick 做我想做的所有事情,除了创建一个具有多级透明度的 png。问题可能是我对透明度如何存储在 png 中缺乏了解。
假设我要创建一个透明的三角形区域,然后创建一个具有第二级透明度的多边形区域,一个半透明的区域。
我在修改前后都尝试过使用 setOpacity(),但没有成功。
我还创建了两个独立的 ImagickDraw() 对象,并给它们一个不同的填充颜色,但没有运气。这是我最后一次尝试的示例:
$img = new Imagick('rec.png');
$height = $img->getImageHeight();
$width = $img->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(new ImagickPixel('#999999'));
//$draw->rectangle( 10,10,100,100 );
$points = [
['x' => 400, 'y' => 0],
['x' => 400, 'y' => 200],
['x' => 700, 'y' => 0],
['x' => 400, 'y' => 0],
];
$draw->polygon($points);
$tdraw = new ImagickDraw();
$tdraw->setFillColor('rgb(90, 90, 90)');
$npoints = [
['x' => 0, 'y' => 0],
['x' => 0, 'y' => 200],
['x' => 400, 'y' => 200],
['x' => 700, 'y' => 0],
['x' => 0, 'y' => 0],
];
$tdraw->polygon($npoints);
$mask->drawImage( $draw );
$mask->drawImage( $tdraw );
$mask->negateImage(true, Imagick::CHANNEL_ALPHA);
// Composite the images using Imagick::COMPOSITE_DSTOUT
$img->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
任何为我指明正确方向的事情都会有很大的帮助......谢谢!
【问题讨论】:
-
不在机器附近,但尝试使用
rgba(x,y,z,t)设置填充颜色,其中t是透明度,或使用#ffffff80
标签: php imagemagick imagick alpha-transparency