PHP Image Crop from Resize

Discussion in 'Web Design and Development' started by joro, Aug 22, 2009.

  1. joro macrumors 68020

    joro

    Joined:
    Jun 11, 2009
    Location:
    NOVA
    #1
    Alright, I am having a problem modifying this code to make it work. This script is one that allows me to upload a full size image and from it create a thumbnail version. I want the image to scale proportionally but I want the script to crop from the center for larger images. So, for example, right now I upload a horizontal image and I wind up with an image that is 150 x 99 or flipped if vertical.

    What can I add to this code to make it center crop so that the thumbnail is exactly 150 x 150? :confused:

    Code:
    <?php
    class Image {
            var $FileName;
            var $FileSize;
            var $FileType;
            var $AllowedExtentions = array ("image/png", "image/gif", "image/jpeg", "image/pjpeg", "image/jpg"); 
            var $newWidth = 150; // new width 
            var $newHeight;
            var $TmpName;
            var $PicDir;  //store uploaded images
            var $MaxFileSize = 2000000;  //kbytes   
    		var $ImageQuality = 100;  // image compression (max value 100)
    
            function Image($FileName) {
                $this->FileName=$FileName;
            }
            
            function GetInfo() {
                $out='  <br><br>Upload success!<br>
                			Name: '.$this->FileName.'<br>
                        file size: '.$this->FileSize.' byte<br>
                        file type: '.$this->FileType.'<br>
    					<img src=' . dirname($_SERVER['PHP_SELF']) . '/' . $this->PicDir .  $this->FileName . '><br><br>';
    					
                return $out;        
            }
    		
    		 
    		 function GetFileExtention ($FileName) {
    			if (in_array ($this->FileType, $this -> AllowedExtentions)) {
    				return true; 		
    			} 	else {
    			   return false;			
    			}		 	
    		 
    		 } 			
    		 
    		 function ExistFile () {
    			$fileexist = $_SERVER['DOCUMENT_ROOT']  . 
    						  dirname($_SERVER['PHP_SELF']) . 
    						  '/' . $this->PicDir .  
    						        $this->FileName;
    				if (file_exists($fileexist)) { return true; }
    		 	}
    		 
    		function GetError ($error) {
    			
    			switch ($error) {			
    				case 0 :
    					echo "Error: Invalid file type <b>$this->FileType</b>! Allow type: .jpg, .jpeg, .gif, .png  <b>$this->FileName</b><br>";
    				break;
    				
    				case 1 :
    					echo "Error: File <b>$this->FileSize</b> is too large! You must upload 1000 MB file<br>";
    				break;				
    				
    				case 2 :
    					echo "Error: Please, select a file for uploading!<br>";
    				break;
    									
    				case 3 :
    					echo "Error: File <b>$this->FileName</b> already exist!<br>";
    				break;				
    			}
    			
    		}
    
    		
    		 function Resize () {
    			if (empty  ($this->TmpName)) 										{echo $this -> GetError (2);}
    				else if ($this->FileSize > $this->MaxFileSize)        			{echo $this -> GetError (1);}						
    				else if ($this -> GetFileExtention ($this->FileName) == false) 	{echo $this -> GetError (0);} 
    				else if ($this -> ExistFile())                 				    {echo $this -> GetError (3);}
    					
    					else {
    				
    			$ext = explode(".",$this->FileName);
    			$ext = end($ext);
    			$ext = strtolower($ext);
    			
    			// Get new sizes
    			list($width_orig, $height_orig) = getimagesize($this->TmpName);
    
    			
    			$ratio_orig = $width_orig/$height_orig;
    
    				if ($this->newWidth/$this->newHeight > $ratio_orig) {
       			$this->newWidth = $this->newHeight*$ratio_orig;
    				} else {
       			$this->newHeight = $this->newWidth/$ratio_orig;
    				}
    			
    			
    
    			$normal  = imagecreatetruecolor($this->newWidth, $this->newHeight);
    
    			if 	 		($ext == "jpg") { $source = imagecreatefromjpeg($this->TmpName);  }
    			else if 	($ext == "gif") { $source = imagecreatefromgif($this->TmpName);  }
    			else if 	($ext == "png") { $source = imagecreatefrompng($this->TmpName);  }
    
    			imagecopyresampled($normal, $source,    0, 0, 0, 0, $this->newWidth, $this->newHeight, $width_orig, $height_orig);
    
    
    			if 	 		($ext == "jpg") { 
    								//ob_start();
    							    imagejpeg($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality"); 
    								//$binaryThumbnail = ob_get_contents(); 
    								//ob_end_clean(); 
    								}
    			else if 	($ext == "gif") { imagegif ($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality");  }
    			else if 	($ext == "png") { imagepng ($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality");  }
    
    			imagedestroy($source);
     			
     			//echo $this -> GetInfo();
    						
     		}	
    	
    	} 				
    	 		 		       
            function Save() {		
    				if (empty  ($this->TmpName)) 									{echo $this -> GetError (2);}
    				else if ($this->FileSize > $this->MaxFileSize)        			{echo $this -> GetError (1);}						
    				else if ($this -> GetFileExtention ($this->FileName) == false) 	{echo $this -> GetError (0);} 
    				else if ($this -> ExistFile ())                 				{echo $this -> GetError (3);}         
            			
    					else {
                
    				copy($this->TmpName,$this->PicDir.$this->FileName);
                
    				//echo $this -> GetInfo();
                      	
    			 }
             }
         }
    
    ?>
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Here's the math. You'll use the values in the imagecopyresampled function call inside the Resize function. I'll leave the implementation up to you for practice.

    PHP:
    CenterX round(thumb_width/2);
    CenterY round(thumb_height/2);
    TopLeftX CenterX - (150/2);
    TopLeftY CenterY - (150/2);
     

Share This Page