Imagick - 创建两个不同级别的透明度

Imagick - create two different levels of transparency

我有一张图片用于测试。这是:

我已经能够用 PHP Imagick 做我想做的一切,除了创建一个具有多级透明度的 png。问题可能是我对透明度如何存储在 png 中缺乏理解。

假设我正在尝试创建一个透明的三角形区域,但随后创建了一个具有第二级透明度的多边形区域,该区域是半透明的。

我试过在修改前后使用 setOpacity(),但没有成功。

我还创建了两个单独的 ImagickDraw() 对象,并为它们提供了不同的 fillColor,但不幸的是。这是我最后一次尝试的示例:

$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); 

任何能给我指明正确方向的东西都会有很大的帮助...谢谢!

不太确定我是否理解这个问题,但我相信你想创建类似...

  1. 绘制两个不同透明度值的独立形状。
  2. 将它们合成在一起。
  3. 将生成的图像应用为图像蒙版。

我建议将代码重写为...

$img = new Imagick('rec.png');
$height = $img->getImageHeight();
$width = $img->getImageWidth();

// Create first mask from original (and possible preserve original transparancies).
$mask = clone($img);
// We can "extract" the alpha channel to create a full white image.
$mask->setImageAlphaChannel(Imagick::ALPHACHANNEL_EXTRACT);
// Create second mask from frist.
$mask2 = clone($mask);

//Draw onto the new image the areas you want to be transparent in the original
$draw = new ImagickDraw();
$draw->setFillColor('gray90'); //<= Simplify with common color names
$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('gray50'); //<= Something diffrent for visiblity.
$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 );
$mask2->drawImage( $tdraw );
// We can merge the values by multiplication. Might be worth exploring "SCREEN" & "BLEND" options
$mask->compositeImage($mask2, Imagick::COMPOSITE_MULTIPLY, 0, 0);
// Copy the values to alpha/opacity channel
$mask->setImageAlphaChannel(Imagick::ALPHACHANNEL_COPY);
// Copy the opacity from the mask to the original image.
$img->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0); 
$img->writeImage('output.png');