dsndm82062 2014-08-12 09:32
浏览 230
已采纳

Imagick切出部分图像并使其成为100%透明的alpha

I have 2 images $main and $cutout_holes. $main is just any image while $cutout_holes is a black and white image that I want to use as a mask, cutting some holes in $main. I want all white (#ffffff) pixels of $cutout_holes to cut out fully transparent holes in $main. The below is what I have tried so far but it does not work at all.

Any suggestions are welcome

<?
$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;
?>
  • 写回答

1条回答 默认 最新

  • donglei1973 2014-08-13 01:47
    关注

    I believe the composite option(s) your thinking of are Screen & Multiply, but I also don't believe they'll give you the results your expecting. Generally in ImageMagick's documentation, masks are considered alpha-channel values/mattes (i.e. black = 0.0 = opaque, and white = 1.0 = transparent.) Simply flip, or negate, your $coutout_holes image, and apply it as the alpha channel.

    <?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
    $coutout_holes->setImageMatte(FALSE);
    
    // Apply holes mask as the new alpha channel.
    $main->compositeImage($cutout_holes, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
    

    Holes cut

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿