Kiran Dash
Kiran Dash

Reputation: 4956

How to overlay an image on top of another image using iMagick

Overview:

I got two images. The background image with 1920x1080p resolution and the overlay image that can be of any resolution less than 1920x1080p.

Background image(1920x1080):

enter image description here

Overlay image:(any size less <= 1920x1080)

enter image description here

Result image(1920x1080):

enter image description here

I am trying to overlay the overlay image exactly at the center of background image. I know how to do it using ImageMagick.

Solution using ImageMagick:

convert -composite -gravity center background.png overlay.png result.png

Expextation:

I am expecting to do the same using iMagick with the help of Imagick::compositeImage and gravity center. So please any one can help me out here.

Upvotes: 10

Views: 9866

Answers (1)

emcconville
emcconville

Reputation: 24419

Imagick wraps the C-API MagickWand which doesn't automatically inherit the -composite -gravity behavior. You will need to do the math yourself.

$background = new Imagick( __DIR__ . DIRECTORY_SEPARATOR . '6pEEG.png');
$window = new Imagick(__DIR__ . DIRECTORY_SEPARATOR . 'mpyY3.png');

$deltaX = $background->getImageWidth()  - $window->getImageWidth();
$deltaY = $background->getImageHeight() - $window->getImageHeight();

$background->compositeImage($window,
                            Imagick::COMPOSITE_ATOP,
                            $deltaX / 2,
                            $deltaY / 2);

How to overlay an image on top of another image using iMagick

Upvotes: 7

Related Questions