<?php
//namespace Verot\Upload;	
function save_pic($up_id="",$width="",$height="",$modules="",$plural="",$convert="",$arr="",$process="",$crop="",$upfile="",$type="") {
	include_once XOOPS_ROOT_PATH."/modules/neillibrary/class/upload/class.upload.php";
	//save_pic($up_id=$showimg,$width=$width,$height=$height,$modules="".$xoops_theme."",$plural=true,$convert=$convert,$arr=true,$process=1,$crop=true);
	//參數
	//$up_id=圖片名稱
	//$width=圖片寬度
	//$height=圖片高度，設0自動等比例縮放
	//$modules=儲存圖片資料夾名稱
	//$plural=圖片編號false不編號true迴圈編號
	//$convert=檔類附檔名，預設是JPG
	//$arr=輸入參數是否為陣列類型true陣列false單檔
	//$process=儲存檔案路徑
	//$crop是否裁切圖片true裁切false不裁切
	//var_dump($upfile);
	$_FILES['upfile']= empty($upfile) ? $_FILES['upfile'] : $upfile;
	// get TRUE
	switch($type) {
		case "1":  //單張圖片上傳
		//   $pic = new Upload($_FILES['upfile'], 'zh_TW');
		$pic = new \Verot\Upload\Upload($_FILES['upfile']);
		if ($pic->uploaded) {
			//大圖
			$pic->file_new_name_body = $up_id;
			$pic->file_overwrite     = true;
			$pic->image_resize       = true;
			$pic->image_x            = $width;
			if(empty($height)) {
				$pic->image_ratio_y = true;
				// 按比例縮放高度
			} else {
				$pic->image_y = $height;
			}
			$pic->image_convert      = $convert;
			//是否裁切圖片 true/false
			if($crop==true) {
				$pic->image_ratio_crop   = true;
				//裁切
			} else {
				$pic->image_ratio_crop   = false;
				//不裁切
			}
			$pic->Process(XOOPS_ROOT_PATH.'/uploads/'.$modules.'/');
			if (!$pic->processed) {
				return 'error : ' . $pic->error;
			}
		}
		break;
		//單檔上傳
		case "2": 
		   // $pic = new Upload($_FILES['upfile'], 'zh_TW');
		$pic = new \Verot\Upload\Upload($_FILES['upfile']);
		if ($pic->uploaded) {
			$pic->file_new_name_body = $up_id;
			// $pic->image_ratio_y = true; 
			$pic->image_convert      = $convert;
			//副檔名
			//$pic->image_ratio_crop   = false;
			$pic->Process(XOOPS_ROOT_PATH.'/uploads/'.$modules.'/');
			//指定允許上傳的文檔mime類型
			$pic->clean();
			if (!$pic->processed) {
				return 'error : ' . $pic->error;
			}
		}
		break;
		//多單檔上傳
		case "3":  

			
		$files = array();
		foreach ($_FILES['upfile'] as $k => $l) {
			foreach ($l as $i => $v) {
				if (!array_key_exists($i, $files)) $files[$i] = array();
				$files[$i][$k] = $v;
			}
		}
		foreach ($files as $i => $file) {	

			$handle = new \Verot\Upload\Upload($file);
			if ($handle->uploaded) {
				$handle->file_new_name_body = $up_id[$i];
				$handle->image_convert = $convert[$i];
                $configvar=configvar($modulesid="neillibrary");			
     	    
     	    	//取得圖片尺寸寬度	
	        	$arrayimg = getimagesize($file['tmp_name']);	     
	           //必須是圖片類型且原始圖寬度超過預設寬度->才做縮圖
                if(!empty($arrayimg[2]) &&   $arrayimg[0] > $configvar['neillibrary13']){                	
				$handle->file_overwrite = true;
				$handle->image_resize = true;
			    //進行縮圖處理  
				$handle->image_x = $configvar['neillibrary13'];
				$handle->image_ratio_y = true;
				}
				$handle->Process(XOOPS_ROOT_PATH.'/uploads/'.$modules.'/');

				if (!$handle->processed) {
					return 'error : ' . $handle->error;
					unset($handle);
				}
			}
		}
		break;
		default:
		 //多圖上傳
		$files = array();
		foreach ($_FILES['upfile'] as $k => $l) {
			foreach ($l as $i => $v) {
				if (!array_key_exists($i, $files)) $files[$i] = array();
				$files[$i][$k] = $v;
			}
		}
		foreach ($files as $i => $file) {
			//$handle = new Upload($file);
			$handle = new \Verot\Upload\Upload($file);
			if ($handle->uploaded) {
				if($plural==false) {
					$handle->file_new_name_body = $ {
						"up_id"
					}
					;
				} else {
					$handle->file_new_name_body = $up_id[$i];
				}
				$handle->file_overwrite = true;
				$handle->image_resize = true;
				if($arr==true) {
					$handle->image_x = $width[$i];
				} else {
					$handle->image_x = $width;
				}
				if(empty($height[$i])) {
					$handle->image_ratio_y = true;
					// 按比例縮放高度
				} else {
					if($arr==true) {
						$handle->image_y = $height[$i];
					} else {
						$handle->image_y = $height;
					}
				}
				// $pic->image_ratio_y = true; // 按比例縮放高度
				if(empty($convert)) {
					$handle->image_convert = 'jpg';
				} else {
					if(!empty($arr)) {
						$handle->image_convert = $convert[$i];
					} else {
						$handle->image_convert = $convert;
					}
				}
				//是否裁切圖片 true/false
				if($crop==true) {
					$handle->image_ratio_crop = true;
					//裁切
				} else {
					$handle->image_ratio_crop = false;
					//不裁切
				}
				switch ($process) {
					case "1":  
					$handle->Process(XOOPS_ROOT_PATH.'/uploads/'.$modules.'/');
					break;
					default: 	 
					$handle->Process(XOOPS_ROOT_PATH.'/uploads/'.$modules.'/image/.thumbs/');
				}
				if ($handle->processed) {
					echo 'OK';
				} else {
					echo 'Error: ' .$handle->error;
				}
			} else {
				echo 'Error: ' . $handle->error;
			}
			unset($handle);
		}
	}
}
?>