Location: PHPKode > scripts > ImageUpload > imageUpload.php
<?php
/**
 * --> class ImageUpload
 *
 * Description : 	Upload the picture, rename it and put it in the directy indicate.
 * 					If the directory doesn't exist, it will create it.
 * 					If the file exist it will replace it.
 * 					You give a maximal dimension to your picture. If the image is too big, it reduce the size of the image.
 * 					You can choose the quality of the image from .jpg
 * 
 *
 * @author Florence Latour <hide@address.com> 
 * @version 2.4 (04/2011)
 * @link www.florencelatour.com
 * @copyright GNU GPL
 *			  > English : http://www.gnu.org
 *			  > Choose your langage : http://www.gnu.org/#translations
 *			  > You can find a french version of this classes on http://www.luciolab.com/equipe-labo
 * 
 * This is my first class, I hop it will be usefull. You can send me a mail if you find an error, thanks for your help.
 */

class ImageUpload {
	private $input_file;
	private $temp_name_file;
	private $directory;
	private $max_dimension;
	private $side;
	private $new_name;
	private $quality;
	private $raison;
	private $max_size;
	
	
	/**
	 * Constructor, explanation of the parameters
	 *
	 * @param $input_file : name of your input picture (string)
	 * @param $directory : directory where the picture while be registred (string)
	 * @param $max_dimension : max size of the picture in pixel. Resize only if max_size < size of picture (integer)
	 * @param $side : what is the reference side -> height or width (string)
	 * @param $new_name : new name of the picture (string)
	 * @param $quality : quality of the picture for .jpg files (integer). A number between 0 (low quality) to 100 (hight quality)
	 * @param $max_size : maximal weight allowed (octets)
	 **/

	public function __construct ($input_file, $directory, $max_dimension, $side, $new_name, $quality, $max_size) {
		$this->input_file = $_FILES[$input_file]['name'];
		$this->temp_name_file = $_FILES[$input_file]['tmp_name'];
		$this->size_file = $_FILES[$input_file]['size'];
		$this->max_size = $max_size;
		$this->directory = $directory;
		$this->max_dimension = $max_dimension;
		$this->side = $side;
		$this->new_name = $new_name;
		$this->quality = $quality;
	}
	
	# Upload the picture	
	public function uploadImg () {
		if($this->size_file <= $this->max_size) {
			#1# Verify that the file is uploaded	
			if (is_uploaded_file($this->temp_name_file)) {
				#2# Test the type of image, in the array the autorized types
				$autorized_type = array('jpg', 'jpeg', 'png', 'gif', 'wbmp', 'pjpeg');
				$type = strtolower(pathinfo($this->input_file, PATHINFO_EXTENSION));	
			 	if(in_array($type, $autorized_type)){
					$this->new_name = $this->new_name.'.'.$type;
					#3# Does the directory exist ? Now, it is
					if (is_dir($this->directory) == false) {
						mkdir($this->directory, 0705);
					}
					else {
						// it's alright
					}
			
					#4# Does the file exist ? yes, so erase it
					if (file_exists($this->directory.$this->new_name)) {
					    unlink($this->directory.$this->new_name);
					}
					else {
					    // wait a litle more
					}
	
					#5# Resize or not ?
					#5.1 get the info about size
					$info = getimagesize($this->temp_name_file);
			        $src_width = $info[0];
			        $src_height = $info[1];
	
					#5.2 witch type of file
					switch ($type) {
						case 'jpg':		$case_type = 'imagecreatefromjpeg';
										$create_type = 'imagejpeg';
										break;
								
						case 'jpeg':	$case_type = 'imagecreatefromjpeg';
										$create_type = 'imagejpeg';
										break;	
						
						case 'pjpeg':	$case_type = 'imagecreatefromjpeg';
										$create_type = 'imagejpeg';
										break;
																			
						case 'png':		$case_type = 'imagecreatefrompng';
										$create_type = 'imagepng';
										break;		
											
						case 'gif':		$case_type = 'imagecreatefromgif';
										$create_type = 'imagegif';
										break;
									
						case 'wbmp':	$case_type = 'imagecreatefromwbmp';
										$create_type = 'imagewbmp';
										break;		
					}
					#5.3 let's go
					// width resize
					if (($this->side == 'width') && ($src_width > $this->max_dimension)) {
						$width = $this->max_dimension;
						$height = round($src_height*($width/$src_width));
						### A - Create resource				
					    $dest_img = imagecreatetruecolor($width,$height);
						imagealphablending($dest_img,false);
						imagesavealpha($dest_img,true);
				        $src_img = $case_type($this->temp_name_file);
				        imagecopyresampled($dest_img, $src_img, 0,0,0,0, $width, $height, $src_width, $src_height);
				        ### B - Create image
						if ($create_type == 'imagejpeg') {
							$create_type($dest_img, $this->directory.$this->new_name, $this->quality);
						}
						else {
							$create_type($dest_img, $this->directory.$this->new_name);
						}			        
						### C - Clean memory
				        imagedestroy($dest_img);
				 		imagedestroy($src_img);
					}   
					// height resize
					else if (($this->side == 'height') && ($src_height > $this->max_dimension)) {
						$height = $this->max_dimension;
						$width = round($src_width*($height/$src_height));
						### A - Création des ressources
					    $dest_img = imagecreatetruecolor($width,$height);
						imagealphablending($dest_img, false);
						imagesavealpha($dest_img, true);
				        $src_img = $case_type($this->temp_name_file);
				        imagecopyresampled($dest_img, $src_img, 0,0,0,0, $width, $height, $src_width, $src_height);
				        ### B - Fabrication des images
						if ($create_type == 'imagejpeg') {
							$create_type($dest_img, $this->directory.$this->new_name, $this->quality);
						}
						else {
							$create_type($dest_img, $this->directory.$this->new_name);
						}
				        ### C - On vide la mémoire
				        imagedestroy($dest_img);
				 		imagedestroy($src_img);
					}
					// no resize
					else {
						if ($create_type == 'imagejpeg') {
							$copy_temp = $case_type($this->temp_name_file);
							$create_type($copy_temp, $this->directory.$this->new_name, $this->quality);
						}
						else {
							$copy_temp = $case_type($this->temp_name_file);
							$create_type($copy_temp, $this->directory.$this->new_name);
						}
						imagedestroy($copy_temp);
					}	
					// print 'Your image have been successfully updated'
				}
				// extent isn't allowed
				else {
					// $this->raison = 'Ce fichier n\'est pas autorisé, veuillez sélectionner une image ou vérifier le tableau des extensions';
					$this->raison = 'File extent is not allowed, select an image or check the extent\'s array';
				}
			}
			// file isn't uploaded
			else {
				// $this->raison = 'Le fichier n\'a pas été pris en compte pour le téléchargement';
			 	$this->raison = 'The file is not uploaded';
			}
		// if the size of the picture is > than the max size allowed
		}
		else {
			$this->raison = 'The file is too big. Maximal is '.$this->max_size.' octets';
		}
	} // END imgUpload
	
	#Give the new name of the picture
	public function getNameImg () {
		return $this->new_name ;
	}// END getNameImg
	
	#Give information when the picture is't uploaded. We can use it as a condition (cf.example.php)
	public function getExplanation () {
		return $this->raison;
	}// END getExplanation
	
} // END class 
?>
Return current item: ImageUpload