class.img.php 6.94 KB
<?php


class img
{
	private $source_name = false;
	private $source_w = 0;
	private $source_h = 0;
	private $source_type = false;
	private $source_mime = false;
	
	private $error_message = false;
	
	private $rst_w = 0;
	private $rst_h = 0;
	private $rst_type = false;
	private $rst_mime = false;
	
	private $crop_x = 0;	// êîðäèíàòà x íà÷àëüíîé òî÷êè íà èñõîäíîì èçîáðàæåíèè
	private $crop_y = 0;	// êîðäèíàòà y íà÷àëüíîé òî÷êè íà èñõîäíîì èçîáðàæåíèè
	private $crop_w = 0;		//	øèðèíà íîâîãî èçîáðàæåíèÿ
	private	$crop_h = 0;		//	âûñîòà íîâîãî èçîáðàæåíèÿ

	
	private $flag_resize = false;
	private $flag_crop = false;
	private $flag_watermark = false;
	
	
	private $FILE_MIMES = array(
								'image/pjpeg'=>"jpeg",
								'image/jpeg'=>"jpeg",
								'image/jpg'=>"jpeg",
								'image/png'=>"png",
								'image/x-png'=>"png",
								'image/gif'=>"gif");
	//private $alpha = 100;
	private $quality = 100;
	
	
//	var $rst_name =false;
	//var $more_resize = false;	//	óâåëè÷èâàòü èçîáðàæåíèå
	

		//	çàãðóæàåì èçîáðàæåíèå
	public function __construct($name){
		
			// ôàéëà íåò, âîçâðàùàåì îáúåêò ñ îøèáêîé
		if ( !file_exists($name) ){
			$this -> make_error( sprintf(E_IMG_FILE_NOT_FOUND,$name) );
			return false;
		}
			//	ôàéë íå èçîáðàæåíèå, âîçâðàùàåì îáúåêò ñ îøèáêîé
		$info = getimagesize($name);
		if ( empty( $info[0]) ){
			$this -> make_error( sprintf(E_IMG_FILE_IS_NO_IMAGE,$name)  );
			return false;
		}
			//	ñ òàêèì èçîáðàæåíèåì ðàáîòàòü ñèñòåìà íå ìîæåò
		
		$type = $this -> mime_to_type($info['mime']);
		
		if ( !function_exists('imagecreatefrom'.$type) ){
			$this -> make_error( sprintf(E_IMG_FILE_BAD_TYPE,$name)  );
			return false;
		}
			//	óñòàíàâëèâàåì ïàðàìåòðû ïî óìîë÷àíèþ
		$this -> source_name = $name;
		$this -> width = $this -> source_w = $this -> rst_w = $info[0];
		$this -> height = $this -> source_h = $this -> rst_h =  $info[1];
		$this -> mime = $this -> rst_mime = $this -> source_mime = $info['mime'];
		$this -> type = $this -> source_type =  $type;
		$this -> rst_type =  $type;
	}
	
		//	òèï íîâîãî ôàéëà
	public function set_type($v){
		if ( empty($v) ){
			$this -> make_error( E_IMG_EMPTY_TYPE );
			return false;	
		}
		if ( !function_exists('image'.$v) ){
			$this -> make_error( sprintf(E_IMG_FILE_BAD_TYPE,$v) );
			return false;
		}
		$this -> rst_mime = $this -> type_to_mime($v);
		if ( !function_exists('image'.$v) ){
			$this -> make_error( sprintf(E_IMG_BAD_MIME,$v) );
			return false;
		}
		$this -> rst_type = $v;
		return true;
	}
	
		//	èìÿ íîâîãî ôàéëà
	public function set_name($v){
		if ( empty($v) ){
			$this -> make_error( E_IMG_EMPTY_NAME );
			return false;	
		}
		
		if ( preg_match('#[^a-z0-9\-=._]#i',$v,$res) ){
		 
			$this -> make_error(E_IMG_BAD_NAME);
			return false;	
		}
		$this -> rst_name = $v;
		return true;	
	}
	
		//	çàäà¸ì ïàïêó äëÿ ñîõðàíåíèÿ ðåçóëüòàòà
	public function set_path($v){
		if ( empty($v) ){
			$this -> make_error( E_IMG_EMPRY_PATH );
			return false;
		}
		if ( !is_dir($v) ){
			$this -> make_error( sprintf(E_IMG_BAD_PATH,$v) );
			return false;
		}
		
		if ( !is_writable($v) ){
			$this -> make_error( sprintf(E_IMG_PATH_WRITE_ACCESS,$v) );
			return false;
		}
		$this -> rst_path = $v;
		return true;
	}
	
	
	

	/**
	* Âû÷èñëÿåò ðàçìåðû íîâîãî èçîáðàæåíèÿ
	*	
	* @project cms
	* @class img
	* @name resize
	* @param w int øèðèíà íîâîãî èçîáðàæåíèÿ
	* @param h int âûñîòà íîâîãî èçîáðàæåíèÿ
	* @param strict bool æ¸ñòêîå èëè ïðîïîðöèîíàëüíîå âû÷èñëåíèå ðàçìåðîâ èçîáðàæåíèÿ
	* @return bool
	*/
	public function resize($w=0,$h=0,$strict = false){
		$this -> flag_resize = true;
		
			//	æ¸ñòêèé ðåñàéç
		if ( $strict ){
			
			$w =  intval($w);
			$h =  intval($h);
				//	øî çà ðåñàéç áåç ðàçìåðîâ èçîáðàæåíèÿ
			if ( empty($w) || empty($h) ){
				$this -> make_error(E_IMG_EMPTY_SIZE);
				return false;
			}
			$this -> rst_w = $w;
			$this -> rst_h = $h;
			return true;
		}
		
		$w = empty($w) ? $this -> source_w : intval($w);
		$h = empty($h) ? $this -> source_h : intval($h);
		
			//	ðàçìåðû íîâîãî èçîáðàæåíèÿ áóäóò ïðåâûøàòü ðàçìåðû òåêóùåãî
		if ( $w>=$this -> source_w  &&  $h>=$this -> source_h ){
			return false;
		}
		
			//	âûÿñíèì íà ñêîëüêî íóæíî ñæàòü èçîáðàæåíèå
		$koof_w = round($this -> source_w / $w );
		$koof_h = round($this -> source_h / $h );
		$koof_max = max($koof_w,$koof_h);
		$this -> rst_w = round( $this -> source_w / $koof_max);
		$this -> rst_h = round( $this -> source_h / $koof_max);
			//	ðåñàéç íå òðåáóåòñÿ
		if ( $this -> rst_w==$this -> source_w && $this -> rst_h==$this -> source_h ){
			return false;
		}
		
		
		return true;
	}
	
	
	
	
	public function crop($x,$y,$w,$h){
		$this -> crop_x = intval($x);
		$this -> crop_y = intval($y);
		$this -> crop_w = intval($w);
		$this -> crop_h = intval($h);
		$this -> flag_crop = true;
		return true;
	}
	

	

		//	ñîõðàíÿåì ðåçóëüòàò
	public function save(){
		
			//	óñòàíîâêà ïàðàìåòðîâ ïðèâåëà ê îøèáêå
		if ( false!==$this -> error() ){
			return false;
		}
			
		
			//	çàãðóæàåì èñõîäíèê äëÿ ðàáîòû
		$func_name = 'imagecreatefrom'.$this -> source_type;
		$source  =  $func_name($this -> source_name);
		

			//	äåëàåì ðåñàéç
		if ( false!==$this -> flag_resize ){
		
			
			$target = imagecreatetruecolor($this -> rst_w , $this -> rst_h);
			  //  äîáàâèì ïèíãàì íåìíîãî ïðîçðà÷íîñòè
		  imagealphablending($target, false);
      imagesavealpha($target, true);
			imagecopyresampled(
					$target, 
					$source, 0,0,0,0,
					$this -> rst_w,$this -> rst_h,$this -> source_w,$this -> source_h  
			);
			
			$source = $target;
		}
			
				//	äåëàåì êðîï
		if ( false!==$this -> flag_crop ){
		  
			$target = imagecreatetruecolor($this -> crop_w , $this -> crop_h);
			imagecopy(
					$target, 
					$source, 0, 0,$this -> crop_x, $this -> crop_y, $this -> crop_w, $this -> crop_h
			);	
			$source = $target;
		}
		
			//	äåëàåì âîäÿíîé çíàê
		if ( false!==$this -> flag_watermark ){
			//	òóò íàíîñèì âîäÿíûå çíàêè íà èçîáðàæåíèå
			
		}
		
			//	âûâîäèì ðåçóëüòàò
		$func_name = 'image'.$this -> rst_type;
		if ( $this -> rst_type=='png' ){
		  $this -> quality =  round($this -> quality / 10)-1;
		}
	
	 
		if ( empty($this -> rst_name) ){
		 
			header("Content-type:  ".$this -> rst_mime); 
			if ( !$func_name($target,false,$this -> quality) ){
				$this -> make_error( E_IMG_CREATE_FALSE );
			}
			
		}else{
		
			if ( !$func_name($target,$this -> rst_path.'/'.$this -> rst_name,$this -> quality) ){
				$this -> make_error( E_IMG_CREATE_FALSE );
			}
			
		}
		imagedestroy($target);
		//imagedestroy($source);
		return true;
	}
	
	public function make_error($msg){
		$this -> error_message =  $msg;	
	}
	
	public function error(){
		return $this -> error_message;
	}
	
	
		//	ïåðåâîäèì mime-type èçîáðàæåíèÿ â åãî ôîðìàò
	private function mime_to_type($v){
	
		if (!empty($this -> FILE_MIMES[$v]) ){
			return $this -> FILE_MIMES[$v];
		}else{
			return false;
		}
	}
	
			//	ïîëó÷àåì mime-type èçîáðàæåíèÿ ïî åãî ðàñøèðåíèþ
	private function type_to_mime($i){
		foreach( $this -> FILE_MIMES as $k=>$v ){
			if ( $i==$v ){
				return  $k;
			}
		}
		return false;
	}
	
}

?>