Onur Kucukkece
Onur Kucukkece

Reputation: 1768

php imagick, how to make an area transparent

I want to make an area transparent within an Imagick object with a specific width, height and a top position.

For example I need a transparent area with 30px x 30px from the 15th px to the top but I can't find a way to do it.

$canvas1 = new Imagick();

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

Please help.

Upvotes: 3

Views: 3829

Answers (4)

Mark Setchell
Mark Setchell

Reputation: 207465

This may be a slightly simpler way of doing it. I recycled @AndreKR's 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);

Upvotes: 3

AndreKR
AndreKR

Reputation: 33678

While you can flood fill with transparency ink (not transparent ink) like this:

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

in this post, Anthony, apparently some important figure in the ImageMagick universe, says that you cannot draw with transparency.

So it seems you have to create a punch image and then use it to punch the transparent areas out in your actual image. To create the punch here I draw the rectangle opaque on a transparent brackground and then invert the whole 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's the actual image before the punching:

$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 over the alpha channel from our punch image. For a reason unknown to me the obvious way does not 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, these both 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

(The light blue is the background of the Windows photo viewer, indicating transparent areas.)

Upvotes: 2

Aravind.HU
Aravind.HU

Reputation: 9472

You can set the opacity as follows

$image->setImageOpacity(0.0);

If you set it to 0.0 the image what you have crated will become transparent

for more information you can Set opacity in Imagick

if you want it for a particular area part then you need to change the approach by using GD library functions by doing some what 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 which is posted in another forum here

Upvotes: 0

Wearybands
Wearybands

Reputation: 2455

Try

$canvas1->setImageOpacity(0);

Upvotes: 0

Related Questions