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

问题描述 投票:0回答:1

我有一个用于测试目的的图像。这里是:

test image

我已经能够用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); 

任何指向我正确方向的东西都将是一个巨大的帮助......谢谢!

php imagemagick imagick alpha-transparency
1个回答
2
投票

不确定我是否理解这个问题,但我相信你想创造类似的东西......

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

two different levels of transparency

© www.soinside.com 2019 - 2024. All rights reserved.