Reputation: 2897
I've tried to translate it, but doesn't work, doesn't anyone know what the wrong is ?
ImageMagick
convert source.jpg \( -size 640x480 xc:white -size 200x200
xc:black -geometry +200+100 -compose over -composite \)
+geometry -alpha off -compose copy_opacity -composite result.png
PHP code with Imagick I tried, but didn't work:
//Open your image and get its dimensions
$image = new Imagick('source.png');
$height = $image->getImageHeight();
$width = $image->getImageWidth();
//Create a new transparent image of the same size
$mask = new Imagick();
$mask->newImage($width, $height, new ImagickPixel('white'));
//Draw onto the new image the areas you want to be transparent in the original
$draw = new ImagickDraw();
$draw->setFillColor('black');
$draw->rectangle($x, $y, $x + 200, $y + 200);
$mask->drawImage( $draw );
//Composite the images
$image->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0, Imagick::CHANNEL_ALPHA);
$image->setImageFormat('png');
$image->writeImage("~/images/result.png");
Original Question:
How to make specified area of an image transparent with Imagick?
Another trying
$width = 256;
$height = 256;
$x = 50;
$y = 100;
$fooWidth = 100;
$fooHeight = 60;
$image = new Imagick();
$image->newImage($width, $height, new ImagickPixel('yellow'));
//Create a new transparent image of the same size
$mask = new Imagick();
$mask->newImage($width, $height, new ImagickPixel('white'));
$mask->setImageFormat('png');
//Draw onto the new image the areas you want to be transparent in the original
$draw = new ImagickDraw();
$draw->setFillColor('black');
$draw->rectangle($x, $y, $x + $fooWidth, $y + $fooHeight);
$mask->drawImage($draw);
//Composite the images
$image->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$image->setImageFormat('png');
$image->writeImage($path);
COMPOSITE_COPYOPACITY
looks doesn't work:
Upvotes: 0
Views: 434
Reputation: 53089
try turning alpha off at the end when you create the mask. This works fine for me:
convert -size 500x500 xc:yellow \( -size 500x500 xc:white -fill black -draw "rectangle 100,100 300,300" -alpha off \) -compose copy_opacity -composite result.png
See https://www.php.net/manual/en/imagick.setimagematte.php where the example adds
$mask->setImageMatte(false);
After the draw command and before the compositeImage() command
Upvotes: 1
Reputation: 53089
First, you are drawing a rectangle with the same coordinates. You need to make them different to get a rectangle.
$draw->rectangle( 200, 100, 200, 100 );
Should be more like
$draw->rectangle( 100, 100, 200, 200 )
This will draw a rectangle at top left of 100,100 and bottom right of 200,200. So the rectangle will be 100x100 in size.
If you want it to be like my command line example, then do
$draw->rectangle( 200, 100, 400, 300 );
That would be top left of 200,100 and bottom right of 400,300 so 200x200 size.
Second, you have not specified an output format. If you are writing to JPG, it does not support transparency. So be sure to use PNG or TIF for the output.
Upvotes: 1