Imagick - Composite Image Mask via PHP

I am trying to recreate a script that uses ImageMagick's "convert" command to create an image. But I want to do the same in PHP using Imagick:

convert ./a.png ./b.png ./c.png  -virtual-pixel transparent -channel rgba -alpha on -background transparent -define compose:args=300x53.033 -compose displace -composite ./final.png

      

a.png

Original

b.png

Overlay

c.png

Mask

Result:

Result obtained though command line

Where a.png

is the base image, b.png

is the overlay, and c.png

is the "gray" mask. To achieve this result, I have to perform all the manipulations supported by the extension, write the image to disk, and then execute this command through the exec function, which is a terrible solution.

The following snippet doesn't work:

$a = new Imagick('../a.png');
$b = new Imagick('../b.png');
$c = new Imagick('../c.png');

$a->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$a->setImageBackgroundColor(new ImagickPixel('none'));
$a->setOption('compose:args', '300x53.033');
$a->compositeImage($c, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
$a->compositeImage($b, Imagick::COMPOSITE_DISPLACE, 0, 0);

      

Result:

Result obtained though PHP

Expected:

Result obtained though command line

I would like to achieve the same result using Imagick PHP only.

+3


source to share