Image Thumbnails : Php GD Library

GD Library : GD Lib is used to dynamically edit or creating image . You can see example of GD application in most of the sites which is CAPTCHA application .

Captcha is used for verification of any form for Human filling not robot .

Here i am going to explain the code to create dynamic thumb of bigger images which widely used by the developer for creating a gallery short of things .


function Thumbnail($imageDirectory, $imageName, $thumbDirectory,$thumb_w,$thumb_h)
{

     // Getting the image attributes
$size= getimagesize(“$imageDirectory/”.$imageName);
$origHeight = $size[1];
$origWidth = $size[0];

       // Checking image Type jpg , png etc
$type=$size[2];

     // To distinguished the name of thumb to original one
$tsrc=”thumb_”.$imageName;

     // To check the proportionally which is high
if(($origHeight)>=($origWidth))
{
$thumbHeight =$thumb_h;
$thumbWidth = ceil($origWidth *($thumbHeight /$origHeight) );
}
if(($origWidth)>=($origHeight))
{
$thumbWidth =$thumb_w;
$thumbHeight = $origHeight;
$thumbHeight = ceil( $origHeight * ($thumbWidth /$origWidth));
}

    // GD functions names are changed for each type of images  , so we are jumping   accordingly
if($type==1)  // for gif formate // //resize the image//
{
$im=imagecreatefromgif(“$imageDirectory/”.$imageName);
$newimage=imagecreatetruecolor($thumbWidth,$thumbHeight);
imagecopyresampled($newimage,$im,0,0,0,0,$thumbWidth,$thumbHeight,$origWidth,$origHeight);
imagegif($newimage,”$imageDirectory/”.$tsrc);
chmod(“$imageDirectory/$tsrc”,0777);
}
if($type==2)  // for jpeg formate // //resize the image//
{
$im=imagecreatefromjpeg(“$imageDirectory/”.$imageName);
$newimage=imagecreatetruecolor($thumbWidth,$thumbHeight);
imagecopyresampled($newimage,$im,0,0,0,0,$thumbWidth,$thumbHeight,$origWidth,$origHeight);
imagejpeg($newimage,”$imageDirectory/”.$tsrc);
chmod(“$imageDirectory/$tsrc”,0777);
}
if($type==3)  // for png formate // //resize the image//
{
$im=imagecreatefrompng(“$imageDirectory/”.$imageName);
$newimage=imagecreatetruecolor($thumbWidth,$thumbHeight);
imagecopyresampled($newimage,$im,0,0,0,0,$thumbWidth,$thumbHeight,$origWidth,$origHeight);
imagepng($newimage,”$imageDirectory/”.$tsrc);
chmod(“$imageDirectory/$tsrc”,0777);
}
if($type==6)  // for wbmp formate // //resize the image//
{
$im=imagecreatefromwbmp(“$imageDirectory/”.$imageName);
$newimage=imagecreatetruecolor($thumbWidth,$thumbHeight);
imagecopyresampled($newimage,$im,0,0,0,0,$thumbWidth,$thumbHeight,$origWidth,$origHeight);
imagewbmp($newimage,”$imageDirectory/”.$tsrc);
chmod(“$imageDirectory/$tsrc”,0777);
}
}

// Calling of Function will create thumb in same location  and fetching the image from //same directory
Thumbnail(“/”,”abc.jpg”,”/”,200,200);

 

 

Hope this small tutorial will help you to develop some logic .

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>