【问题标题】:Imagick - create two different levels of transparencyimagick - 创建两个不同级别的透明度
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-01-03
    • 2011-10-08
    • 1970-01-01
    • 2011-11-19
    • 2018-03-30
    • 2013-09-20
    • 2014-08-12
    • 1970-01-01
    • 2011-04-07
    相关资源
    最近更新 更多