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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab