PHP ImageMagick - Composite Images



The combination of two or more images to create a new one is called composite photography. And the combined photo is called a composite image. Combining visual elements from separate sources into a single image is often done to create the illusion that all those elements are parts of the same image.

Doing this manually becomes a very complex task and it takes hours. To make this process easy and fast, Imagemagick has provided a method named ‘compositeImage()’ which takes two images as input and provides the combined image as output.

Syntax

public Imagick::compositeImage(Imagick $composite_object, int $composite, int $x, int $y, int $channel = Imagick::CHANNEL_DEFAULT): bool

The parameters of this method are composite_object, x, y, and channel. ‘Composite_object’ is an Imagick object which holds the composite image.

‘x’ is the column offset of the composited image and ‘y’ is the row offset of the composited image. ‘Channel’ provides any channel constant that is valid for your channel mode.

Example

This example is a PHP code snippet which implements ‘compositeImage()’ function. Firstly, two imagick objects are created and two images are taken as inputs. Both the images are composited with the help of ‘compositeImage()’ function, and the output image is in the format ‘compositeImage.png’.

<?php
   $image1=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image1.jpg");
   $image2=new Imagick($_SERVER['DOCUMENT_ROOT']."/test/image2.jpg");
   $image1->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
   $image1->setImageArtifact('compose:args', "1,0,-0.5,0.5");
   $image1->compositeImage($image2, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
   $image1->writeImage($_SERVER['DOCUMENT_ROOT']."/test/compositeImage.png");
?>

Assume that the following is the input image (image1.jpg) in the program −

Composite Images

Assume that the following is the input image (image2.jpg) in the program −

Composite Images

Output

Composite Images
Advertisements