Prerequisites

  1. Install Imagick in php with command sudo apt-get install php7.4-imagick
  2. Resart the server with sudo service apache2 restart / valet restart

Firstly we have 2 images, the first image is a source image and the second one is mask image. We will crop the original image with as the size of mask image.

$base = new Imagick(‘path-to-source-image');
$mask = new Imagick(‘path-to-mask-image);

Now, we crop the source image as the size of mask image and save it to file.

$base->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 40, 40); //position can be changed by playing with numbers
$base->writeImage('path-to-result-file');
round image

And then, we take result image and image to be merged. We merge them both and save it to specified path.

$src1 = new Imagick('path-to-result-file');

$src2 = new Imagick(‘path-to-merge-image’);
$src1->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_UNDEFINED);
$src1->setImageArtifact('compose:args', "1,0,-0.5,0.5");
//position can be adjusted by playing with numbers
$src1->compositeImage($src2, Imagick::COMPOSITE_DEFAULT,122, 120);
$src1->writeImage("output-image-path");