Location: PHPKode > scripts > Thumbnail Creation > thumbnail-creation/thumbnail.class.php
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *     Thumbnail Creation class.   Its s simple class where You can create Thobnail image  *
 *     of your current image, it will not create image rather it reduces the size of the   * 
 *	   image                                                                               *
 *                                                                                         *	   
 *	   Name     :   Thumbnail Class                                                        *
 *	   Author   :   Aneesh                                                                 *
 *	   Email    :   hide@address.com                                                     *
 *	   Date     :   Saturday, April 07, 2007                                               *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
class Thumbnail{

	var $thumb_Width    =    125;   //Width Limit For Thumb Nail
	
	var $thumb_Height   =    100;    //Height Limit For Thumb Nail
	
    var $medium_Width   =    292;   // Width Limit For Medium SIze Image (Say for Mouse Over Display)
	
	var $medium_Height  =    154;   // Height Limit For Medium SIze Image (Say for Mouse Over Display)
	
	var $original_Width;    // Original WIdth of the Image, ie the width of the image to be resized
	
	var $original_Height;    // Original Height of the Image, ie the height of the image to be resized
	
	var $New_Height;   // Resized Images' Height
	
	var $New_Width;   // Resized Images' Width
	
	var $reszie_Type;  // What typ of image we need to create ?? Thumb nail or mouse over
	
	var $base_height;    //  Base height For Resize type, Will Update dynamically
	
	var $base_width;    //  Base Width For Resize type, Will Update dynamically
	
	/**
		@param  int OriginalWidth
		@param  int Originalheight
		@param  string ResizeType [Thumb | Hover | Full Size]
		return  NULL
	**/
	function Thumbnail($o_Width,$o_Height,$r_Type='Thumb'){
		$this->original_Width     =    $o_Width;
		$this->original_Height    =    $o_Height;
		$this->reszie_Type        =    $r_Type;
		$this->SetBaseLimit();
	}
	
	/**
		Set Base size or can say as size limits for each type of image
		$param NULL		
	**/
	function SetBaseLimit(){
		switch($this->reszie_Type){
			case 'Thumb':
				// Base Size Limit for Thumb Nail Images
				$this->base_height    =    $this->thumb_Height;
				$this->base_width     =    $this->thumb_Width;
			break;
			
			case 'Hover':
				// Base Size Limit for Mouse Over Images
				$this->base_height    =    $this->medium_Height;
				$this->base_width     =    $this->medium_Width;
			break;
		}
	}
	
	/**
		Finds the Image is to resized or not
		@param NULL
		$return Boolean
	**/
	function isImageToResize(){
		if($this->original_Width > $this->base_width || $this->original_Height > $this->base_height)
		return true;
		else
		return false;		
	}
	
	/**
		Fins the Width Type of Image, ie whther it is a Width > Height Image 
		or Height >Width Image or Height == Width Image
		$return String W  ['Width >Height'] |
		$return String H  ['Width <Height'] |
		$return String WH ['Width==Height'] |
	**/
	
	function GetImageSizeType(){
		if($this->original_Width > $this->original_Height)
		return 'W';
		if($this->original_Width < $this->original_Height)
		return 'H';
		if($this->original_Width == $this->original_Height)
		return 'WH';
	}
	
	/**
		Based on the Imagetype and width resize the original image
	**/
	
	function ResizeImage(){
		if($this->isImageToResize()){
			$ImageSizeType    =   $this->GetImageSizeType();
			switch($ImageSizeType){
				case 'W':
					$this->SetSizeOnWidth();
				break;
				case 'H':
					$this->SetSizeOnHeight();
				break;
				case 'WH':
					$this->SetSizeOnHeight();
				break;
			}
		}else{
			$this->SetToDefault();
		}
	}
	
	/**
		Resize the Image Baesd on width
	**/
	function SetSizeOnWidth(){
		$new_width            =    $this->base_width;
		$reduced_width        =    $this->original_Width - $this->base_width;
		$reduced_percentage   =    ($reduced_width/$this->original_Width)*100;
		$height_to_reduce     =    ($this->original_Height*$reduced_percentage)/100;
		$new_height           =    $this->original_Height-$height_to_reduce;
		$this->New_Height     =    floor($new_height);	
		$this->New_Width      =    $new_width;
	}
	
	/**
		Resize the Image Baesd on Height
	**/
	function SetSizeOnHeight(){
		$new_height            =    $this->base_height;
		$reduced_height        =    $this->original_Height - $this->base_height;
		$reduced_percentage    =    ($reduced_height/$this->original_Height)*100;
		$width_to_reduce       =    ($this->original_Width*$reduced_percentage)/100;
		$new_width             =    $this->original_Width-$width_to_reduce;
		$this->New_Height      =    floor($new_height);	
		$this->New_Width       =    floor($new_width);
	}
	
	/**
		Set the Image size to its default
	**/
	function SetToDefault(){
		$this->New_Height    =    $this->original_Height;
		$this->New_Width     =    $this->original_Width;
	}
}
?>
Return current item: Thumbnail Creation