fileName = $path."/".$filename; ### 80 cropped to 65### if($size80 > 0 ){ if (($image_info[0]>65) && ($image_info[1]>65)) { $thumb->fileName = $path."/".$filename; $thumb->quality = 100; $thumb->init(); $thumb->percent = 00; //Percent has to be 0 for maxWidth and maxHeight to work; if ($image_info[0] > $image_info[1]) { $thumb->maxWidth = 00; $thumb->maxHeight = 65; } else { $thumb->maxWidth = 65; $thumb->maxHeight = 0; } $thumb->resize(); $thumb->cropSize = 65; $thumb->cropX = ($thumb->getCurrentWidth()/2)-($thumb->cropSize/2); $thumb->cropY = ($thumb->getCurrentHeight()/2)-($thumb->cropSize/2); $thumb->crop(); $thumb->save($path."/thumbnail80/".$filename); //////////////////////////thumb nail for profile picture///////// } else { copy($path."/".$filename, $path."/thumbnail80/".$filename); // upload the file to the server chmod("$add",0777); } } ### 100 cropped to 85### if($size100 > 0 ){ if (($image_info[0]>85) && ($image_info[1]>85)) { $thumb->quality = 100; $thumb->init(); $thumb->percent = 00; if ($image_info[0] > $image_info[1]) { $thumb->maxWidth = 00; $thumb->maxHeight = 85; } else { $thumb->maxWidth = 85; $thumb->maxHeight = 0; } $thumb->resize(); $thumb->cropSize = 85; $thumb->cropX = ($thumb->getCurrentWidth()/2)-($thumb->cropSize/2); $thumb->cropY = ($thumb->getCurrentHeight()/2)-($thumb->cropSize/2); $thumb->crop(); $thumb->save($path."/thumbnail100/".$filename); } else { copy($path."/".$filename, $path."/thumbnail100/".$filename); // upload the file to the server chmod("$add",0777); } } #### 200 ### if($size200 > 0 ){ if (($image_info[0]>200) && ($image_info[1]>200)) { $thumb->fileName =$path."/".$filename; $thumb->quality = 100; $thumb->init(); $thumb->percent = 0; //Percent has to be 0 for maxWidth and maxHeight to work; if ($image_info[0] > $image_info[1]) { $thumb->maxWidth = 00; $thumb->maxHeight = 200; } else { $thumb->maxWidth = 200; $thumb->maxHeight = 0; } $thumb->resize(); $thumb->cropSize = 200; $thumb->cropX = ($thumb->getCurrentWidth()/2)-($thumb->cropSize/2); $thumb->cropY = ($thumb->getCurrentHeight()/2)-($thumb->cropSize/2); $thumb->crop(); $thumb->save($path."/thumbnail200/".$filename); //////////////////////////thumb nail for profile picture///////// } else { copy($path."/".$filename, $path."/thumbnail200/".$filename); // upload the file to the server chmod("$add",0777); } } #### 400 #### if($size400 > 0 ){ if (($image_info[0]>320) && ($image_info[1]>320)) { $thumb->fileName = $path."/".$filename; $thumb->quality = 100; $thumb->init(); $thumb->percent = 0; //Percent has to be 0 for maxWidth and maxHeight to work; if ($image_info[0] > $image_info[1]) { $thumb->maxWidth = 00; $thumb->maxHeight = 320; } else { $thumb->maxWidth = 320; $thumb->maxHeight = 0; } $thumb->resize(); $thumb->cropSize = 320; $thumb->cropX = ($thumb->getCurrentWidth()/2)-($thumb->cropSize/2); $thumb->cropY = ($thumb->getCurrentHeight()/2)-($thumb->cropSize/2); $thumb->crop(); $thumb->save($path."/thumbnail400/".$filename); //////////////////////////thumb nail for profile picture///////// } else { copy($path."/".$filename, $path."/thumbnail400/".$filename); // upload the file to the server chmod("$add",0777); } } $myfilename = "http://www.friendseat.com/".$path."/".$filename; } return $myfilename; } ?>quality = 100; $thumb->fileName = "/path/to/file.jpg"; //IMPORTANT - must run init() function before any manipulation is performed $thumb->init(); //shrink image by 50% $thumb->percent = 50; $thumb->resize(); //crop image to 350x350 from center of image $thumb->cropSize = 350; $thumb->crop(); //resize image to no more than 125px wide $thumb->percent = 0; $thumb->maxWidth = 125; $thumb->resize(); //save image as 'filename.jpg' $thumb->save('/path/to/save/filename.jpg'); You can also use this class to dynamically generate thumbnails and display them After you have made your manipulations you could display the result by: echo ''; */ class Thumbnail { var $errmsg; //error message to parse var $error; //flag for whether there is an error var $format; //file format of image var $currentDimensions; //current dimensions of working image var $newDimensions; //new dimensions after manipulation var $newImage; //final image to be displayed/saved var $oldImage; //original image to be manipulated var $workingImage; //working image being manipulated var $fileName; //filename of image, can include directory var $maxWidth; //maximum width of the image var $maxHeight; //maximum height of the image var $percent; //percentage of the original image size var $quality; //image quality of jpeg images var $cropSize; //size in px to crop from the center of the image (crop is always square) var $cropX; var $cropY; function Thumbnail() { //class constructor $this->errmsg = ''; $this->error = false; $this->format = ''; $this->currentDimensions = array(); $this->newDimensions = array(); $this->fileName = ''; $this->maxWidth = 0; $this->maxHeight = 0; $this->percent = 0; $this->quality = 100; $this->cropSize = 0; $this->cropX = 0; $this->cropY = 0; } function destruct() { if($this->newImage) @ImageDestroy($this->newImage); if($this->oldImage) @ImageDestroy($this->oldImage); if($this->workingImage) @ImageDestroy($this->workingImage); } function init() { //initialize function that does basic error checking and determines file type //check to see if file exists if(!file_exists($this->fileName)) { $this->errmsg = "File not found"; $this->error = true; } //check to see if file is readable elseif(!is_readable($this->fileName)) { $this->errmsg = "File is not readable"; $this->error = true; } //determine file extension if($this->error == false) { //check if gif if(stristr(strtolower($this->fileName),'.gif')) $this->format = 'GIF'; //check if jpeg elseif(stristr(strtolower($this->fileName),'.jpg') || stristr(strtolower($this->fileName),'.jpeg')) $this->format = "JPG"; //check if png elseif(stristr(strtolower($this->fileName),'.png')) $this->format = 'PNG'; //unknown file format else { $this->errmsg = "Unknown file format"; $this->error = true; } } //catch user forgetting to specify size paramenters if($this->maxWidth == 0 && $this->maxHeight == 0 && $this->percent == 0) $this->percent = 100; if($this->error == false) { switch($this->format) { case "GIF": $this->oldImage = ImageCreateFromGif($this->fileName); break; case "JPG": $this->oldImage = ImageCreateFromJpeg($this->fileName); break; case "PNG": $this->oldImage = ImageCreateFromPng($this->fileName); break; } $size = GetImageSize($this->fileName); $this->currentDimensions = array("width"=>$size[0],"height"=>$size[1]); $this->newImage = $this->oldImage; } if($this->error) { $this->showErrorImage(); break; } } function getCurrentWidth() { return $this->currentDimensions['width']; } function getCurrentHeight() { return $this->currentDimensions['height']; } ############################################# #---- Image Size Calculation Functions -----# ############################################# function calcWidth($width,$height) { $newWP = (100 * $this->maxWidth) / $width; $newHeight = ($height * $newWP) / 100; return array("newWidth"=>intval($this->maxWidth),"newHeight"=>intval($newHeight)); } function calcHeight($width,$height) { $newHP = (100 * $this->maxHeight) / $height; $newWidth = ($width * $newHP) / 100; return array("newWidth"=>intval($newWidth),"newHeight"=>intval($this->maxHeight)); } function calcPercent($width,$height) { $newWidth = ($width * $this->percent) / 100; $newHeight = ($height * $this->percent) / 100; return array("newWidth"=>intval($newWidth),"newHeight"=>intval($newHeight)); } function calcImageSize($width,$height) { $newSize = array("newWidth"=>$width,"newHeight"=>$height); if($this->maxWidth > 0 && $width > $this->maxWidth) { $newSize = $this->calcWidth($width,$height); if($this->maxHeight > 0 && $newSize['newHeight'] > $this->maxHeight) $newSize = $this->calcHeight($newSize['newWidth'],$newSize['newHeight']); $this->newDimensions = $newSize; } if($this->maxHeight > 0 && $height > $this->maxHeight) { $newSize = $this->calcHeight($width,$height); if($this->maxWidth > 0 && $newSize['newWidth'] > $this->maxWidth) { $newSize = $this->calcWidth($newSize['newWidth'],$newSize['newHeight']); } $this->newDimensions = $newSize; } if($this->percent > 0) { $newSize = $this->calcPercent($width,$height); $this->newDimensions = $newSize; } } ####################################### #----- Show Error Image Function -----# ####################################### function showErrorImage() { //output an error image to the user header("Content-type: image/png"); $errImg = ImageCreate(220,25); $bgColor = ImageColorAllocate($errImg,0,0,0); $fgColor1 = ImageColorAllocate($errImg,255,255,255); $fgColor2 = ImageColorAllocate($errImg,255,0,0); ImageString($errImg,3,6,6,"ERROR:",$fgColor2); ImageString($errImg,3,55,6,$this->errmsg,$fgColor1); ImagePng($errImg); ImageDestroy($errImg); } ########################################## #----- Image Manipulation Functions -----# ########################################## function resize($name = '') { $this->calcImageSize($this->currentDimensions['width'],$this->currentDimensions['height']); if(function_exists("ImageCreateTrueColor")) { $this->workingImage = ImageCreateTrueColor($this->newDimensions['newWidth'],$this->newDimensions['newHeight']) or dir("cannot") ; } else { $this->workingImage = ImageCreate($this->newDimensions['newWidth'],$this->newDimensions['newHeight']); } ImageCopyResampled( $this->workingImage, $this->oldImage, 0, 0, 0, 0, $this->newDimensions['newWidth'], $this->newDimensions['newHeight'], $this->currentDimensions['width'], $this->currentDimensions['height'] ); $this->oldImage = $this->workingImage; $this->newImage = $this->workingImage; $this->currentDimensions['width'] = $this->newDimensions['newWidth']; $this->currentDimensions['height'] = $this->newDimensions['newHeight']; } function crop() { $cropX = intval(( $this->cropSize - $this->currentDimensions['width'] ) / 2); $cropY = intval(( $this->cropSize - $this->currentDimensions['height']) / 2); if(function_exists("ImageCreateTrueColor")) { $this->workingImage = ImageCreateTrueColor($this->cropSize,$this->cropSize); } else { $this->workingImage = ImageCreate($this->cropSize,$this->cropSize); } ImageCopy( $this->workingImage, $this->oldImage, 0, 0, $this->cropX, $this->cropY, $this->cropSize, $this->cropSize ); $this->oldImage = $this->workingImage; $this->newImage = $this->workingImage; $this->currentDimensions['width'] = $this->cropSize; $this->currentDimensions['height'] = $this->cropSize; } ########################################## #----- Image Display/Save Functions -----# ########################################## function save($name) { $this->show($name); } function show($name='') { switch($this->format) { case "GIF": if($name != '') { imagegif($this->newImage,$name); } else { header("Content-type: image/gif"); imagegif($this->newImage); } break; case "JPG": if($name != '') { ImageJpeg($this->newImage,$name,$this->quality); } else { header("Content-type: image/jpeg"); ImageJpeg($this->newImage,'',$this->quality); } break; case "PNG": if($name != '') { ImagePng($this->newImage,$name); } else { header("Content-type: image/png"); ImagePng($this->newImage,$name); } break; } } } ?>