imagick console commands into PHP Imagick

2019-08-04 07:52发布

问题:

Can anyone explain me how to convert imagick cli command that are working fine into PHP code using Imagick pecl? I am not familiar enough with syntax of convert command and it is complex for me to do it quickly.

You may not read this heap of text bellow, just help to convert cli command to PHP. :)

I have one partially transparent figure located on not transparent white background(original image). With filling that transparent figure some background color/image I'll have many colored figures on white background.

What i need now(and where imagick comes in): I need remove that white background on result image so the only colored figure stays there with some blured edges on transparent backround.

What i am doing:

  1. I fill original image with black color to get more contrast black figure on white background. Next, remove white background with command:

    convert ./black.png -fuzz 70% -fill none -floodfill +0+0 white -channel A -blur 0x1 ./mask.png

So i have some transparent mask to use in future.

  1. Apply mask.png to some colored image(green.php) that was got from original too:

    convert ./green.png -alpha Off ./mask.png -compose CopyOpacity -composite PNG32:result.png

That commands have to be translated into PHP. Can anyone advice me?

回答1:

I am ready to answer my question.

Prepare original image to use it as mask. Make partially transparent figure in black:

$blank = new \Imagick('./original.png');
$blank->setimagebackgroundcolor('#000000');

Make mask from blank in black color. Remove not transparent white background and make it fully transparent:

$mask = $blank->flattenImages();
$mask->floodfillpaintimage('none', 30000, '#FFFFFF', 1, 1, false, \Imagick::CHANNEL_ALPHA);
$mask->blurImage(0, 1, \Imagick::CHANNEL_ALL);

So we have a mask image that consists only of figure we need(in black). Now we want to have a green figure, take it:

$greenBlank = new \Imagick('./original.png');
$greenBlank->setimagebackgroundcolor('#00FF00');
$green = $greenBlank->flattenImages();

Now apply prepared mask to our $green.

$green->compositeImage($mask, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);

That is all. We have got a green figure on transparent background.



标签: php imagick