Merging two images in PHP specially with Imagic or Imagemagick commands!

Hi folks,

Can anyone suggest a link/page for merging two images into one using PHP’s IMagick library or directly imagemagick commands? Actually what I want is; I have one image as background and another small iconic image of certain size (say 80x80px) to be placed in a certain specific area of the background image. I have a workaround to place the icon in a specific area of another image. See an example here and tryto customize a http://uno.chivasregal.info/products/customize/2-plastic-fancy-bags. I have stored the Pixel of the icon when it is dragged.

Now in the checkout process, I want to generate the final design image as seen in the web page into another image. I don’t know I explained it enough or not but what I want once again is: place one small image in a specific area of another bigger image and save to a new image.

Please guide me in a right direction. I could not find any clue from Google. Actually I can do it using GD but I don’t want to do it using Imagemagick. Lets say it is the requirement.

Thanking you in advance !

Regards
Raju.

Try:


<?php
$background = '317ffd8c37.jpg';
$top_image = 'boots.png';

exec("convert 317ffd8c37.jpg boots.png -gravity center -geometry +0+50 -composite output.jpg");
?>

-gravity center puts the top image on the center of the base image; tweek the position with -geometry

Thank you so much Rubble ! I will look at it then will revert back if there is anything ! Thanks :slight_smile:

Hi Rubble,

That worked quite good in this way. But why doesn’t it work in windows with full path ?

The following works:


$background = "main.jpg";
$top_image = "tomerge.png";
$output = "output.png";

exec("convert $background $top_image -gravity -geometry +0+0 -composite $output");
header('Content-type: image/png');
echo readfile($output);
die;

But the following does not work (please note the path of the images):


$path = dirname(__FILE__);
$background = "$path/images/main.jpg";
$top_image = "$path/images/tomerge.png";
$output = "$path/images/output.png";

exec("convert $background $top_image -gravity -geometry +0+0 -composite $output");
header('Content-type: image/png');
echo readfile($output);
die;

I am in Windows 8 with Apache and PHP ! Is there any specific way of using paths in Windows? I tried D:\\etc\\path\\ and also D://etc//path but none of them worked.

Thanking you once again for the help !
Regards
Raju

Sorry Rubble, after trying the whole day today and couple of hours yesterday, I figured it out that I had missed something. I had used -gravity but did not pass the value for it. That remained really pain in ass for last two days :frowning:


$path = dirname(__FILE__);
$background = "$path/images/main.jpg";
$top_image = "$path/images/tomerge.png";
$output = "$path/images/output.png";

exec("convert $background $top_image -geometry +0+0 -composite $output");
header('Content-type: image/png');
echo readfile($output);

This works now quite good !

Thanking you once again !

Edit:
By the way, is there a way to enable error message that Imagemagick itself returns and we can see on the screen on such mistakes ?