2017-10-27 08:43
浏览 341


I want to make a part of an image transparent, I tried the code below, even tried many constants as COMPOSITE_DSTOUT, but all didn't work, does anyone know how to?

$fooImage->newImage(256, 256, new ImagickPixel('transparent'));
$Image->compositeImage($fooImage, Imagick::COMPOSITE_DSTOUT, $offsetX, offsetY);

I tested the code below, just got yellow with black, not transparent:

$width = 256;
$height = 256;

$image = new Imagick();
$image->newImage($width, $height, new ImagickPixel('yellow'));

$x = 50;
$y = 100;
$fooWidth = 100;
$fooHeight = 60;

//Create a new transparent image of the same size
$mask = new Imagick();
$mask->newImage($width, $height, new ImagickPixel('none'));

//Draw onto the new image the areas you want to be transparent in the original
$draw = new ImagickDraw();
$draw->rectangle($x, $y, $x + $fooWidth, $y + $fooHeight);

//Composite the images
$image->compositeImage($mask, Imagick::COMPOSITE_DSTOUT, 0, 0,


Got black inside yellow, not transparent in yellow

result image

图片转代码服务由CSDN问答提供 功能建议

我想让图片的一部分透明,我尝试了下面的代码,甚至尝试了许多常量作为COMPOSITE_DSTOUT, 但都没有用,有没有人知道怎么做?

  $ fooImage-> newImage(256,256,new ImagickPixel('transparent')); 
 $  Image-> compositeImage($ fooImage,Imagick :: COMPOSITE_DSTOUT,$ offsetX,offsetY); 

我测试了下面的代码,只是变黄了 黑色,不透明:

  $ width = 256; 
 $ height = 256; 
 $ image = new Imagick(); 
 $  image-> newImage($ width,$ height,new ImagickPixel('yellow')); 
 $ x = 50; 
 $ y = 100; 
 $ fooWidth = 100; 
 $ fooHeight = 60  ; 
 $ mask = new Imagick(); 
 $ mask-> newImage($ width,$ height,new ImagickPixel('none'));  
 $ mask-> setImageFormat('png'); 
 $ draw = new ImagickDraw(); 
 $ draw-  > setFillColo  r('black'); 
 $ draw-> rectangle($ x,$ y,$ x + $ fooWidth,$ y + $ fooHeight); 
 $ mask-> drawImage($ draw); 
 $ image-> compositeImage($ mask,Imagick :: COMPOSITE_DSTOUT,0,0,
 Imagick :: CHANNEL_ALPHA); 
 $ image-> setImageFormat('png'  ); 
 $ image-> writeImage($ path); 


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhou7656 2017-10-27 17:19

    You need to make a black and white mask image the size of your input (white where you want it to be opaque and black where you want it to be transparent). Then use the equivalent of -compose copyopacity -composite to put the mask into the alpha channel of the image. Sorry, I do not code Imagick.

    Here is an example using ImageMagick command line syntax:


    enter image description here

    convert logo.jpg \( -size 640x480 xc:white -size 200x200 xc:black -geometry +200+100 -compose over -composite \) +geometry -alpha off -compose copy_opacity -composite result.png

    enter image description here

    You can see it is transparent by compositing it over another image (in this case a checkerboard).

    convert ( -size 640x480 pattern:checkerboard ) result.png -compose over -composite result2.jpg

    enter image description here

    点赞 打赏 评论
  • doudou_3636 2017-10-27 12:02

    Do you try \Imagick::COMPOSITE_COPYOPACITY ?

    Because that's probably the right one.

    点赞 打赏 评论

相关推荐 更多相似问题