我有一张用于测试目的的图片。这是:
我已经能够用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);
指向正确方向的任何事情都将是一个巨大的帮助......谢谢!
答案 0 :(得分:2)
我不确定我是否理解这个问题,但我相信你想创造类似......
的东西我建议将代码重写为......
$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');