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

Tuesday, March 11

Code: Merge Gif Images to create one image

The following code will take an array of gif images and merge them into one and save the resulting image to file.

I use it to create a unit images on the fly. As users buy different equipment, the unit's look changes.


function mergeImages($imgPathArray, $dest)
{
$imgArray = array();
for($i=0;$i < sizeOf($imgPathArray);$i++)
{
array_push($imgArray, imageCreateFromGif($imgPathArray[$i]));
}

$src = $imgArray[0];
$background = ImageColorClosest($src, 0, 0, 0);

for($i=1;$i<sizeOf($imgArray);$i++)
{
//ImageColorTransparent($imgNew, $background);

ImageCopyMerge( $src ,
$imgArray[$i],
0, //dest x
0, //dest y
0, //src x
0, //src y
74,//width
100,//height
100 //alpha
);

imagedestroy($imgArray[$i]);
}

return ImageGif($src, $dest);
}





Usage:

$dest = "images/custImage.gif";
$imgPathArray = array("images/builder/blueBase.gif","images/builder/body.gif","images/builder/legs.gif");
array_push($imgPathArray, "images/builder/body.gif");
array_push($imgPathArray, "images/builder/legs.gif");
array_push($imgPathArray, "images/builder/sword.gif");
mergeImages($imgPathArray, $dest);

Notes and Caveats:

All of my gif images were the same size, 74 X 100.
They also have transparent backgrounds.
If your images are different sizes, just change the width and height lines.
Note: Your array must contain at least 2 images otherwise you get an error.

1 comments:

daneel said...

Is it possible to use it in my Html page ?

I don't know php....

Marco

Post a Comment

Followers