0

I have two PNG files that I need to lay one on top of the other. The first image, the red one, is the background, and the second, the green image, needs to be laid over top of the red background image.

How would I go about creating this single image on the fly within a PHP environment?

enter image description here

SAL
  • 834
  • 1
  • 8
  • 16

2 Answers2

1

The simplest way is probably by using the GD image library functions.

You'll have to create two image resources (one for each image) using the imagecreatefrompng function. Then copy one into the other using the imagecopy function, which lets you specify to- and from- coordinates. (Or, use imagecopyresampled if you also need to resize the overlayed image.) Finally, save the primary image resource using the imagepng function. That last one can either save to disk or output directly in the browser.

The PHP manual for these functions has plenty of good examples.

soapergem
  • 9,263
  • 18
  • 96
  • 152
1

Just use Imagick::compositeImage in PHP.

 // php
 $background = new Imagick('red.png');
 $foreground = new Imagick('green.png');
 $background->compositeImage($foreground,Imagick::COMPOSITE_ATOP,50,50);
 $background->writeImage('output.png');

Our execute Imagemagick commands directly

 # CLI
 convert red.png green.png -gravity south -geometry +0+30 -composite output.png
emcconville
  • 23,800
  • 4
  • 50
  • 66