Zend - The PHP Company




Resizing

Add Code


A SafCom Function for Image Resizing  

Type: application
Added by: safras
Entered: 26/02/2003
Last modified: 03/12/2002
Rating: - (fewer than 3 votes)
Views: 10601
This is a very simple and easy way to resize your Image, Just passed the value for SafComMakeThumbnails() it will do your work


<?
$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 "/usr/home/www/images/visitorcards";
$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 SafComMakeThumbnails($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/cardwww/www/images/visitorcards
$filepatht $filepatht//target :: /usr/home/cardwww/www/images/visitorcards
$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,$filewidth,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,$filewidth,imagesx($src_img),imagesy($src_img));
imagepng($dst_img"$filepatht/$smallstart$filename");
}
}

?>


Usage Example




Rate This Script





Search



This Category All Categories