简体   繁体   中英

How to translate ImageMagick command to Imagick in PHP?

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:

在此处输入图片说明

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.

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 http://us3.php.net/manual/en/imagick.setimagematte.php where the example adds

$mask->setImageMatte(false);

After the draw command and before the compositeImage() command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM