Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

 
Make Image Thumbnails Using PHP GD

This programmer is a very simple and use full code for you, If you want you can use this as a function. Any way its up to you

Script Name : SafMakeThumbnails() Version : 1.1
Developer : Safras Ahamed Date 03/13/2003
Used Functions : imagecreatefromjpeg(), imagecreate(), imagecopyresized(), imagejpeg(), imagesx(), imagesy(),  imagepng()
 

<?
$new_w
= 126; //resize image width to 126 pix
$new_h = 88; //resize image height to 88 pix
//here is the directory path that the original image (big image) stored
$cfg_fullsizepics_path = "$yourpath";
$entry = "$filename"; //If you know there file name so you give here
$VTYPE = "$VTYPE"; // file type, So we can detect jpg or gif

//Ok here we go, Now we can start making thumbnails...
if ($VTYPE =="image/pjpeg") { // if the file type is image/pjpeg (jpg). for jpg files MIME type is image/pjpeg
$src_img = imagecreatefromjpeg($cfg_fullsizepics_path."/".$entry); //we are loading the big image to imagecreatefromjpeg

$dst_img = imagecreate($new_w,$new_h); //making image resize to 120 x 80pix
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));//
imagejpeg($dst_img, "$cfg_fullsizepics_path/s_$entry");//this is where we store the file. here I am storing file in same directory but but I am saving all thumbnails with s_ so I know if the file is start from s_ its a thumbnail

}else{ // if the image is not JPG, Then I know its gif I mean its call png, the followings i am not going to explain cas its same as JPG but only functions are changed as imagecreatefrompng
$src_img = imagecreatefrompng($cfg_fullsizepics_path."/".$entry);
$dst_img = imagecreate($new_w,$new_h);
imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
imagepng($dst_img, "$cfg_fullsizepics_path/s_$entry");
}
//====== FULL CODE AS A FUNCTION =======
//This is tested and working, but you need GD library
function SafMakeThumbnails($filename,$filetype,$filepaths,$filepatht,$filewidth,$filehight,$smallstart) {
$filename = $filename; // file name on source path
$filetype = $filetype; // file type (gif or jpg in mime format like image/gif or image/pjpeg)
$filepaths = $filepaths; //source :: /usr/home/httpd/www/bla-bla/image
$filepatht = $filepatht; //target :: /usr/home/httpd/www/bla-bla/image
$filewidth = $filewidth; // resize image width
$filehight = $filehight; // resize image hight
$smallstart = $smallstart; // Small Image start with

if ($filetype =="image/pjpeg") {
$src_img = imagecreatefromjpeg($filepaths."/".$filename);
$dst_img = imagecreate($filewidth,$filewidth);
imagecopyresized($dst_img,$src_img,0,0,0,0,$filewidth,$filehight,imagesx($src_img),imagesy($src_img));
imagejpeg($dst_img, "$filepatht/$smallstart$filename");
}else{
$src_img = imagecreatefrompng($filepaths."/".$filename);
$dst_img = imagecreate($filewidth,$filewidth);
imagecopyresized($dst_img,$src_img,0,0,0,0,$filewidth,$filehight,imagesx($src_img),imagesy($src_img));
imagepng($dst_img, "$filepatht/$smallstart$filename");
}
}

?>

 
Web Master : Safras Ahamed, Email : safras@ArabChat.Org
Copyright 2003 ArabChat Network