Friday, April 3, 2015

Make trasparent gif - PHP Snippet

Make trasparent gif - PHP Snippet

This php function will help you to make a transparent gif thumbnail.
// $imageDirectory = the directory of the image with out the trailing slash 
// $thumbDirectory = the directory where you want to save the thumb
// $imageName = the file name of the image example: myimage.gif
// $thumbWidth = the final size of the thumb
// $percent = the scaling size of the image. values = .1 - 1

// This function will Save the image on your server
function transparentGif($imageDirectory,$thumbDirectory, $imageName, $thumbWidth){
$image = imagecreatefromgif("$imageDirectory/$imageName");
$details = getimagesize("$imageDirectory/$imageName");
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$resized = imagecreatetruecolor($thumbWidth, $thumbHeight);
$colorTransparent = imagecolortransparent($image);
imagepalettecopy($image, $resized);
imagefill($resized, 0, 0, $colorTransparent);
imagecolortransparent($resized, $colorTransparent);
imagecopyresized($resized, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]);
imagegif($resized,"$thumbDirectory/$imageName");
}

// This function will only display the transparent gif, and not save it
function transparentGifShow($imageDirectory,$thumbDirectory, $imageName,$percent = 1){
$image = imagecreatefromgif("$imageDirectory/$imageName");
$details = getimagesize("$imageDirectory/$imageName");
$thumbHeight = $details[1] * $percent;
$thumbWidth = $details[0] * $percent;
$resized = imagecreatetruecolor($thumbWidth, $thumbHeight);
$colorTransparent = imagecolortransparent($image);
imagepalettecopy($image, $resized);
imagefill($resized, 0, 0, $colorTransparent);
imagecolortransparent($resized, $colorTransparent);
imagecopyresized($resized, $image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]);
imagegif($resized);
}
Disqus Comments