【问题标题】:Imagick cut out parts of image and make them 100% transparent alphaImagick 切出部分图像并使它们 100% 透明 alpha
【发布时间】:2014-10-05 07:39:59
【问题描述】:

我有 2 张图片 $main 和 $cutout_holes。 $main 只是任何图像,而 $cutout_holes 是我想用作蒙版的黑白图像,在 $main 中切割一些孔。我希望 $cutout_holes 的所有白色 (#ffffff) 像素在 $main 中切出完全透明的孔。以下是我到目前为止尝试过的,但它根本不起作用。

欢迎提出建议

<?
$image = new Imagick();
$image->newImage(100, 100, new ImagickPixel("#222222"));

$image->compositeImage($main, Imagick::COMPOSITE_DEFAULT, 0, 0);
$image->compositeImage($cutout_holes, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);

header('Content-type: image/png');
$image->setImageFormat('png');
echo $image;
?>

【问题讨论】:

    标签: php image imagemagick imagick


    【解决方案1】:

    我相信您想到的复合选项是 Screen & Multiply,但我也不相信它们会给您带来您期望的结果。通常在ImageMagick's documentation 中,蒙版被视为 alpha 通道值/遮罩(即黑色 = 0.0 = 不透明,白色 = 1.0 = 透明。)只需翻转或否定您的$coutout_holes 图像,并将其应用为 alpha 通道.

    <?php
    $main = new Imagick('any.png');
    $cutout_holes = new Imagick('mask.png');
    
    // If original mask wasn't already negated, do it here.
    $cutout_holes->negateImage(FALSE);
    
    // Null any previous alpha states. Same as -alpha off
    $main->setImageAlphaChannel(Imagick::ALPHACHANNEL_DEACTIVATE);
    // (and/or) Drop matte state of mask. Same as +matte
    $cutout_holes->setImageMatte(FALSE);
    
    // Apply holes mask as the new alpha channel.
    $main->compositeImage($cutout_holes, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
    

    【讨论】:

    • 这几乎奏效了。我不得不用 $cutout_holes->setImageMatte(false); 替换 setImageAlphaChannel然后它就起作用了 :) 谢谢!
    • 哦,是的,您还需要准备口罩。我会更新我的答案。
    猜你喜欢
    • 2014-01-25
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2016-01-05
    • 2018-03-30
    • 2011-04-25
    • 2012-10-12
    • 2015-08-19
    相关资源
    最近更新 更多