<?
if(!defined('SOFTPUBS_IN')){
    die('Hacking attempt');
}
class glo_images
{
	function make_thumb($type,$source_dir,$target_dir,$new_width,$new_height,$if_cut=0)
   {
				$thumb_width	=	$new_width;
				$thumb_height	=	$new_height;
				$img_type		= 	$type;
	
	        $tar_url = $target_dir;
			
			  if($img_type=="image/pjpeg"||$img_type=="image/jpeg"||$img_type=="image/jpg"){
				 	 $temp_img = imagecreatefromjpeg($source_dir);
			  }elseif($img_type=="image/gif"){
				  $temp_img = imagecreatefromgif($source_dir);
			  }elseif($img_type=="image/x-png"||$img_type=="image/png"){
				  $temp_img = imagecreatefrompng($source_dir);
			  }
			
	        $old_width  = imagesx($temp_img);
	        $old_height = imagesy($temp_img);
	
	        $new_ratio = $new_width/$new_height;
	        $old_ratio = $old_width/$old_height;
	
	        if($if_cut=="1"){
	  		            $new_width  = $new_width;
	  		            $new_height = $new_height;
	  		            if($old_ratio>=$new_ratio){
	  						   $old_width  = $old_height*$new_ratio;
	  						   $old_height = $old_height;
	  						}else{
								$old_width  = $old_width;
	  						   $old_height = $old_width/$new_ratio;
							}
	  		   }else{
	  		            $old_width  = $old_width;
	  		            $old_height = $old_height;
	  		            if($old_ratio>=$new_ratio){
			                $new_width  = $new_width;
			                $new_height = $new_width/$old_ratio;
			            }else{
	  						     $new_width  = $new_height*$old_ratio;
	  						     $new_height = $new_height;
	  						}
				}
			   $dst_x = (abs($thumb_width  - $new_width))  / 2;
	         $dst_y = (abs($thumb_height - $new_height)) / 2;
	        $new_img = imagecreatetruecolor($thumb_width,$thumb_height);

			  if (empty($bgcolor)){
	            $bgcolor = "#FFFFFF";
        	  }
	        $bgcolor = trim($bgcolor,"#");
   	     sscanf($bgcolor, "%2x%2x%2x", $red, $green, $blue);
      	  $clr = imagecolorallocate($new_img, $red, $green, $blue);
       	  imagefilledrectangle($new_img, 0, 0, $thumb_width, $thumb_height, $clr);	
				
			  imagecopyresampled($new_img,$temp_img,$dst_x,$dst_y,0,0,$new_width,$new_height,$old_width,$old_height);
			 
			  $bgcolortrans=ImageColorTransparent($new_img,$clr);
			  imagepng($new_img,$tar_url);
			  imagedestroy($new_img); 
	}	

	function upload_img($file_arr,$type,$image_name='',$width='',$height='')
	{
		if(!$file_arr['name']){
			return false;
		}else{
			$ext			=	file_ext_name($file_arr['name']);
			if($ext!='jpeg'&&$ext!='gif'&&$ext!='jpg'&&$ext!='bmp'&&$ext!='png'){
				return false;
			}
		}
		if($file_arr['error']){
			return false;
		}
	
		require_once(ROOT_PATH.'includes/glo_upload.php');	
		$upload_obj			=		new glo_upload();
		if(!$image_name){
				$image_name				=	name_from_url(preg_replace('/\.[^\.]+/','',$file_arr['name']));
		}
		if($width&&$height){
			$upload_dir			=		$upload_obj->get_upload_dir($file_arr,$type);
			$upload_dir 		=		$upload_dir.'/'.$image_name.'.png';
			require_once(ROOT_PATH.'includes/glo_images.php');
			$this->make_thumb($file_arr['type'],$file_arr['tmp_name'],$upload_dir,$width,$height);
		}else{
			$name					=		$image_name.'.'.$ext;
			$upload_dir			=		$upload_obj->upload_file($file_arr,$type,$name);
		}
		$upload_dir				=		preg_replace('|^'.preg_filtrate(UPLOAD_DIR).'|si','',$upload_dir);
		return $upload_dir;
	}	
}
