View Full Version : GD Overlay

Todd W
12-01-2007, 03:48 PM
I've never worked with the GD Library only ImageMagick, and am getting into using the GD now especially for my video sites where I take the first frame and create a thumbnail. I've already got it doing that, the next step is I want to overlay another image on top of my new thumbnail, and save it. (I want to put the PLAY arrow like YouTube has so people know to click the image to play the video.)

What GD function would I use to overlay an image on another?

Oh, and while we are on it, does anyone have the play arrow in PSD already ;)


12-01-2007, 05:15 PM
Can't help you, but I just wanted to say I like GD over ImageMagick simply because GD usually is already installed in a standard way on most servers, or can easily be turned on, ImageMagick notsomuch.

12-02-2007, 07:12 AM
I put together the example below from the code at http://db.org/demo/2003/02/14/scale-and-overlay/. Hope its what you were looking for.


$overlay = 'overlay.png'; //image to overlay i.e. arrow
$origimg = 'orig.png'; //image having the arrow added

$trans_r = 255;
$trans_g = 255;
$trans_b = 255;

if(!$overlay = imagecreatefrompng($overlay)) {
$error = 'error opening overlay image';

$orig = imagecreatefrompng($origimg);

$orig_x = imagesx($orig);
$orig_y = imagesy($orig);
$overlay_x = imagesx($overlay);
$overlay_y = imagesy($overlay);
$image_x = $orig_x;
$image_y = $orig_y;

$offset_x = 0;
$offset_y = 0;

$image = imagecreatetruecolor($image_x, $image_y);
imagecopyresampled($image, $orig, 0, 0, 0, 0, $image_x, $image_y, $orig_x, $orig_y);

imagecolortransparent($overlay, imagecolorallocate($overlay, $trans_r, $trans_g, $trans_b));
imagecopymerge($image, $overlay, $offset_x, $offset_y, 0, 0, $overlay_x, $overlay_y, 99);

imagepng($image, 'over.png');


Overlayed image:<br />
<img src = "over.png">

Todd W
12-02-2007, 11:20 AM
Thanks, I'll give it a shot.

Side Note: I fail to see how GD is faster than ImageMagick having to do so many lines of code :eek: