Last Good Quote: Son's are the seasoning on our lives. - Someone on Facebook

Friday, March 20

Code: Merge Gifs with PHP

For those who need it, and for my bad memory. The following code will take two image files, lay them over one another, and print some text on the top.

An easy way to create dynamic images.

//
//base image (is under everything)
//
$image = imagecreatefromgif("background.gif");
$width = imagesx($image) ;
$height = imagesy($image) ;

//
//create the final image to be output
//
$image_final = imagecreatetruecolor($width, $height);

//
//create a few colors, for drawing
//
$black = imagecolorallocate($image_final,0,0,0);
$white = imagecolorallocate($image_final, 255,255,255);


//Get destination coordinates
$dst_x = 75;
$dst_y = 75;


//fill image with background
imagefilledrectangle($image_final, 0, 0, $width , $height , $black);


//copy background to output image
imagecopyresampled($image_final, $image, 0, 0 , 0, 0, $width, $height, $width, $height);


//
// Add another image over the top
//
$overlay = imagecreatefromgif("unit.gif");
$width_o = imagesx($overlay);
$height_o = imagesy($overlay);
imagecopy($image_final, $overlay, $dst_w,$dst_h,0,0,$width_o,$height_o);

//
// Write some stuff on the image
//
imagestring($image_final, 3, 15, 32, "Hi there", $black);
imagestring($image_final, 3, 200, 32, "Holy Moly", $black);


//
// Send the image to the browser
//
header("Content-type: image/gif");
imagegif($image_final);

0 comments:

Post a Comment

Followers