<?
/*----------------------------------------------------------------------------------
| Class SDImageUploading image uploading											|
| Example:																			|
|																					|
|	$iu = new SDImageUploading();													|
|	// 'newname' - optional field, leave it 										|
|	//empty if you want to use original file name 									|
|	$iu->doUpload('/usr/html/images/',$HTTP_POST_FILES["img"],'newname');			|
|	//return uploaded image name (include server path) or false						|
| 																					|
| 	//Also you can print error code (if false result returned)						|
| 	echo $iu->error																	|
| 																					|
-----------------------------------------------------------------------------------*/
class SDImageUploading {


	var $disk_path;			//disk path where new image will be uploaded
	var $new_uimage_name;	//new image name
	var $uimage_extension;	//new image extension
	var $uimage;			//uploaded image
	var $error;				//error code
	var $uploaded_file;		//succesfully uploaded file name
	

	function SDImageUploading(){
    	$this->my_max_file_size	= "1536000"; # in bytes
	}


	//check image type function
	function addcheckImgType(){ 
		if((strcmp($this->uimage['type'],'image/jpeg')==0)||(strcmp($this->uimage['type'],'image/gif')==0)|| (strcmp($this->uimage['type'],'image/pjpeg')==0)||(strcmp($this->uimage['type'],'image/jpg')==0)||(strcmp($this->uimage['type'],'image/x-png')==0)){ 
			switch($this->uimage['type']){ 
	 	 		case 'image/jpg': 
	 	 			$this->uimage_extension = '.jpg'; 
	 	 		break; 
	 	 		case 'image/jpeg': 
	 	 			$this->uimage_extension = '.jpg'; 
	 	 		break; 
	 	 		case 'image/pjpeg': 
	 	 			$this->uimage_extension = '.jpg'; 
	 	 		break; 
	 	 		case 'image/gif': 
	 	 			$this->uimage_extension = '.gif'; 
	 	 		break;  
	 	 		case 'image/x-png':
	 	 			$this->uimage_extension = '.png';
	 	 		break;
	 	 	}
	 	 	return true; 
	 	}else{ 
			$this->error .= sprintf("\n<li>Érvénytelen a file típusa (%s)!</li>", $this->uimage['type']);
	 		return false;
	 	} 
	} 


	function addcheckImgSize(){
		if($this->uimage['size']<$this->my_max_file_size){
			return true;
	 	}else{ 
			$this->error .= sprintf("\n<li>A fájl nagyobb (%s), mint a megengedett méret (%s)!</li>", $this->uimage['size'], $this->my_max_file_size);	
	 		return false;
	 	} 
	} 
	
	function addcheckImgDimz(){ 
		$flag=true;
		$size = @GetImageSize($this->uimage['tmp_name']);

		if ($size[0] > $this->image_max_width) {
			$this->error .= "\n<li>A kép nem lehet szélesebb, mint " . $this->image_max_width . " pixel!</li>";
			$flag=false;
		}
		
		if ($size[1] > $this->image_max_height) {
			$this->error .= "\n<li>A kép nem lehet magasabb, mint " . $this->image_max_height . " pixel!</li>";
			$flag=false;
		}

		if($flag){
			return true;
		} else {
	 		return false;
		}

	} 


	function addcheckImgExts(){ 
		if($this->uimage_extension){
			return true;
	 	}else{ 
			$this->error .=  sprintf("\n<li>Ezt a kiterjesztést (%s) nem lehet használni!</li>", $this->uimage_extension);
			return false;
	 	} 
	} 




	//start upload and check image type
	function doUpload($new_disk_path,$new_uimage,$new_uimage_name=''){
		$this->disk_path = $new_disk_path;
		$this->uimage = $new_uimage;
		$this->uploaded_file = '';
		if($new_uimage_name != ''){
			$this->new_uimage_name = $new_uimage_name;
		}else{
			$this->new_uimage_name = $this->uimage_name;
		}
/*
		$this->addcheckImgType();

		$this->addcheckImgSize();

		$this->addcheckImgDimz();

		$this->addcheckImgExts();
*/
		
		if(!$this->error==""){
			return false;
		}


		//$uimageFinal = $this->disk_path.$this->new_uimage_name.$this->uimage_extension;
		$uimageFinal = $this->disk_path.$this->new_uimage_name;
		if(copy($this->uimage['tmp_name'], $uimageFinal)){
			//$this->uploaded_file = $this->new_uimage_name.$this->uimage_extension;
			$this->uploaded_file = $this->new_uimage_name;
			return $this->uploaded_file;
		}else{
			$this->error .= '<br>Cannot copy image to '.$uimageFinal.'. Check chmod and server path '.$this->disk_path;
			return false;
		}

	}
}
?>
