Php imagick how to make area transparent

I want to make the region transparent in an Imagick object with a specific width, height and top position.

For example, I need a transparent area with 30px x 30px from the 15th px to the top, but I cannot find a way to do this.

$canvas1 = new Imagick(); $canvas1->newImage(30,60,'black','png'); 

Please, help.

+1
source share
4 answers

You can set the opacity as follows

 $image->setImageOpacity(0.0); 

If you set the value to 0.0, the image you packed will become transparent.

for more information you can set Opacity in Imagick

if you want it for a specific part of the area, you need to change the approach using the functions of the GD library by doing some actions like this

  $img = imagecreatefrompng($imgPath); // load the image list($width,$height) = getimagesize($imgPath); // get its size $c = imagecolortransparent($img,imagecolorallocate($img,255,1,254)); // create transparent color, (255,1,254) is a color that won't likely occur in your image $border = 10; imagefilledrectangle($img, $border, $border, $width-$border, $height-$border, $c); // draw transparent box imagepng($img,'after.png'); // save 

I could see a similar requirement posted on another forum here

0
source

For now, you can fill the fill with transparent ink (not transparent ink) as follows:

 $im->floodFillPaintImage('#FF000000', 10, '#FFFFFF', 0, 0, false); 

in this post , Anthony, apparently some important figure in the ImageMagick universe, says you can't draw with transparency.

So, you need to create a punch image and then use it to break through the transparent areas in your actual image. To create a punch, I draw a rectangle opaque on a transparent face, and then invert the entire image:

 $punch = new Imagick(); $punch->newImage(100,100, 'transparent'); $drawing = new ImagickDraw(); $drawing->setFillColor(new ImagickPixel('black')); $drawing->rectangle(15, 15, 45, 45); $punch->drawImage($drawing); $punch->negateImage(true, Imagick::CHANNEL_ALPHA); 

punch

Here is the actual image before stamping:

 $im = new Imagick(); $im->newImage(100,100, 'red'); $im->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE); // make sure it has // an alpha channel 

image

Now we can copy the alpha channel from our punch image. For some reason unknown to me, the obvious way doesn't work:

 // Copy over the alpha channel from one image to the other // this does NOT work, the $channel parameter seems to be useless: // $im->compositeImage($punch, Imagick::COMPOSITE_SRC, 0, 0, Imagick::CHANNEL_ALPHA); 

unsuccessful result

However, both of these work:

 // Copy over the alpha channel from one image to the other // $im->compositeImage($punch, Imagick::COMPOSITE_COPYOPACITY, 0, 0); // $im->compositeImage($punch, Imagick::COMPOSITE_DSTIN, 0, 0); 

result

(A blue background is a Windows Photo Viewer window background that displays transparent areas.)

+1
source

This may be a slightly easier way to do this. I started using @AndreKR setup code to get started:

 $im = new Imagick(); $im->newImage(100,100, 'red'); $im->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE); // make sure it has an alpha channel $box=$im->getImageRegion(30,30,15,15); $box->setImageAlphaChannel(Imagick::ALPHACHANNEL_TRANSPARENT); $im->compositeImage($box,Imagick::COMPOSITE_REPLACE,15,15); 
+1
source

Try

 $canvas1->setImageOpacity(0); 
0
source

All Articles