| 為大家介紹php開發的檔案上傳類與圖片處理類,有需要的朋友,可以參考學習下。 php實現檔案的上傳比較簡單,如果再有一個高效的php檔案上傳類,那真是如虎添翼,不想飛都不行。那麼,那麼,如果再有一個好用的圖片處理類呢,可以縮減圖片,可以加浮水印,是不是更爽呢?小聲地告訴你,那是必須的,哈哈。 來看今天的php 教程吧。 1、檔案上傳類的代碼 file_name = $file_name;//重新命名方式代表以時間命名,其他則使用給予的名稱$this->save_path = (preg_match('/\/$/',$save_path)) ? $save_path : $save_path . '/';$this->allow_types = $allow_types == '' ? 'jpg|gif|png|zip|rar' : $allow_types;}/*** 上傳檔案* @access public* @param $files 等待上傳的檔案(表單傳來的$_FILES[])* @return boolean 返回布爾值*/public function upload_file($files) {$name = $files['name'];$type = $files['type'];$size = $files['size'];$tmp_name = $files['tmp_name'];$error = $files['error'];switch ($error) {case 0 : $this->errmsg = '';break;case 1 : $this->errmsg = '超過了php.ini中檔案大小';break;case 2 : $this->errmsg = '超過了MAX_FILE_SIZE 選項指定的檔案大小';break;case 3 : $this->errmsg = '檔案只有部分被上傳';break;case 4 : $this->errmsg = '沒有檔案被上傳';break;case 5 : $this->errmsg = '上傳檔案大小為0';break;default : $this->errmsg = '上傳檔案失敗!';break;}if($error == 0 && is_uploaded_file($tmp_name)) {//檢測檔案類型if($this->check_file_type($name) == FALSE){return FALSE;}//檢測檔案大小if($size > $this->max_size){$this->errmsg = '上傳檔案'.$name.'太大,最大支援'.ceil($this->max_size/1024).'kb的檔案';return FALSE;}$this->set_save_path();//設定檔案存放路徑$new_name = $this->file_name != 'date' ? $this->file_name.'.'.$this->ext : date('YmdHis').'.'.$this->ext;//設定新檔案名稱$this->uploaded = $this->save_path.$new_name;//上傳後的檔案名稱//移動檔案if(move_uploaded_file($tmp_name,$this->uploaded)){$this->errmsg = '檔案'.$this->uploaded.'上傳成功!';return TRUE;}else{$this->errmsg = '檔案'.$this->uploaded.'上傳失敗!';return FALSE;}}}/*** 檢查上傳檔案類型* @access public* @param string $filename 等待檢查的檔案名稱* @return 如果檢查通過返回TRUE 未通過則返回FALSE和錯誤訊息*/public function check_file_type($filename){$ext = $this->get_file_type($filename);$this->ext = $ext;$allow_types = explode('|',$this->allow_types);//分割允許上傳的副檔名為數組//echo $ext;//檢查上傳副檔名是否在請允許上傳的副檔名中if(in_array($ext,$allow_types)){return TRUE;}else{$this->errmsg = '上傳檔案'.$filename.'類型錯誤,只支援上傳'.str_replace('|',',',$this->allow_types).'等檔案類型!';return FALSE;}}/*** 取得檔案類型* @access public* @param string $filename 要取得檔案類型的目標檔案名* @return string 檔案類型*/public function get_file_type($filename){$info = pathinfo($filename);$ext = $info['extension'];return $ext;}/*** 設定檔案上傳後的儲存路徑*/public function set_save_path(){$this->save_path = (preg_match('/\/$/',$this->save_path)) ? $this->save_path : $this->save_path . '/';if(!is_dir($this->save_path)){//如果目錄不存在,建立目錄$this->set_dir();}}/*** 建立目錄* @access public* @param string $dir 要建立目錄的路徑* @return boolean 失敗時返回錯誤訊息和FALSE*/public function set_dir($dir = null){//檢查路徑是否存在if(!$dir){$dir = $this->save_path;}if(is_dir($dir)){$this->errmsg = '需要建立的檔案夾已經存在!';}$dir = explode('/', $dir);foreach($dir as $v){if($v){$d .= $v . '/';if(!is_dir($d)){$state = mkdir($d, 0777);if(!$state)$this->errmsg = '在建立目錄' . $d . '時出錯!';}}}return true;}}?>2、圖片處理類的代碼 mark_str)*7+5進* 行調整* 需要GD庫支援,為更好使用本類推薦使用GD庫2.0+** @author http://bbs.it-home.org*************************************************/class uploadImg extends uploadFile {public $mark_str = 'bbs.it-home.org'; //浮水印字串public $str_r = 0; //字串顏色Rpublic $str_g = 0; //字串顏色Gpublic $str_b = 0; //字串顏色Bpublic $mark_ttf = './upload/SIMSUN.TTC'; //浮水印文字字型檔(包含路徑)public $mark_logo = './upload/logo.png'; //浮水印圖片public $resize_h;//產生縮圖高public $resize_w;//產生縮圖寬public $source_img;//源圖片檔案public $dst_path = './upload/';//縮圖檔案存放目錄,不填則為源圖片存放目錄/*** 產生縮圖 產生後的圖* @access public* @param integer $w 縮小後圖片的寬(px)* @param integer $h 縮小後圖片的高(px)* @param string $source_img 源圖片(路徑+檔案名稱)*/public function img_resized($w,$h,$source_img = NULL){$source_img = $source_img == NULL ? $this->uploaded : $source_img;//取得源檔案的地址,如果為空白則預設為上次上傳的圖片if(!is_file($source_img)) { //檢查源圖片是否存在$this->errmsg = '檔案'.$source_img.'不存在';return FALSE;}$this->source_img = $source_img;$img_info = getimagesize($source_img);$source = $this->img_create($source_img); //建立源圖片$this->resize_w = $w;$this->resize_h = $h;$thumb = imagecreatetruecolor($w,$h);imagecopyresized($thumb,$source,0,0,0,0,$w,$h,$img_info[0],$img_info[1]);//產生縮圖片$dst_path = $this->dst_path == '' ? $this->save_path : $this->dst_path; //取得目標檔案夾路徑$dst_path = (preg_match('/\/$/',$dst_path)) ? $dst_path : $dst_path . '/';//將目標檔案夾後加上/if(!is_dir($dst_path)) $this->set_dir($dst_path); //如果不存在目標檔案夾則建立$dst_name = $this->set_newname($source_img);$this->img_output($thumb,$dst_name);//輸出圖片imagedestroy($source);imagedestroy($thumb);}/***打浮水印*@access public*@param string $source_img 源圖片路徑+檔案名稱*@param integer $mark_type 浮水印類型(1為英文字串,2為中文字串,3為圖片logo,預設為英文字串)*@param integer $mark_postion 浮水印位置(1為左下角,2為右下角,3為左上方,4為右上方,預設為右下角);*@return 打上浮水印的圖片*/public function img_mark($source_img = NULL,$mark_type = 1,$mark_postion = 2) {$source_img = $source_img == NULL ? $this->uploaded : $source_img;//取得源檔案的地址,如果為空白則預設為上次上傳的圖片if(!is_file($source_img)) { //檢查源圖片是否存在$this->errmsg = '檔案'.$source_img.'不存在';return FALSE;}$this->source_img = $source_img;$img_info = getimagesize($source_img);$source = $this->img_create($source_img); //建立源圖片$mark_xy = $this->get_mark_xy($mark_postion);//取得浮水印位置$mark_color = imagecolorallocate($source,$this->str_r,$this->str_g,$this->str_b);switch($mark_type) {case 1 : //加英文字串浮水印$str = $this->mark_str;imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);$this->img_output($source,$source_img);break;case 2 : //加中文字串浮水印if(!is_file($this->mark_ttf)) { //檢查字型檔是否存在$this->errmsg = '打浮水印失敗:字型檔'.$this->mark_ttf.'不存在!';return FALSE;}$str = $this->mark_str;//$str = iconv('gbk','utf-8',$str);//轉換字元編碼 如果使用GBK編碼請去掉此行注釋imagettftext($source,12,0,$mark_xy[2],$mark_xy[3],$mark_color,$this->mark_ttf,$str);$this->img_output($source,$source_img);break;case 3 : //加圖片浮水印if(is_file($this->mark_logo)){ //如果存在浮水印logo的圖片則取得logo圖片的基本資料,不存在則退出$logo_info = getimagesize($this->mark_logo);}else{$this->errmsg = '打浮水印失敗:logo檔案'.$this->mark_logo.'不存在!';return FALSE;}$logo_info = getimagesize($this->mark_logo);if($logo_info[0]>$img_info[0] || $logo_info[1]>$img_info[1]) { //如果源圖片小於logo大小則退出$this->errmsg = '打浮水印失敗:源圖片'.$this->source_img.'比'.$this->mark_logo.'小!';return FALSE;}$logo = $this->img_create($this->mark_logo);imagecopy ( $source, $logo, $mark_xy[4], $mark_xy[5], 0, 0, $logo_info[0], $logo_info[1]);$this->img_output($source,$source_img);break;default: //其它則為文字圖片$str = $this->mark_str;imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);$this->img_output($source,$source_img);break;}imagedestroy($source);}/*** 取得浮水印位置* @access private* @param integer $mark_postion 浮水印的位置(1為左下角,2為右下角,3為左上方,4為右上方,其它為右下角)* @return array $mark_xy 浮水印位置的座標(索引0為英文字串浮水印座標X,索引1為英文字串浮水印座標Y,* 索引2為中文字串浮水印座標X,索引3為中文字串浮水印座標Y,索引4為浮水印圖片座標X,索引5為浮水印圖片座標Y)*/private function get_mark_xy($mark_postion){$img_info = getimagesize($this->source_img);$stre_w = strlen($this->mark_str)*9+5 ; //浮水印英文字串的長度(px)(5號字的英文字元大小約為9px 為了美觀再加5px)//(12號字的中文字元大小為12px,在utf8裡一個漢字長度為3個位元組一個位元組4px 而一個英文字元長度一個位元組大小大約為9px// 為了在中英文混合的情況下顯示完全 設它的長度為位元組數*7px)$strc_w = strlen($this->mark_str)*7+5 ; //浮水印中文字串的長度(px)if(is_file($this->mark_logo)){ //如果存在浮水印logo的圖片則取得logo圖片的基本資料$logo_info = getimagesize($this->mark_logo);}//由於imagestring函數和imagettftext函數中對於字串開始位置不同所以英文和中文字串的Y位置也有所不同//imagestring函數是從文字的左上方為參照 imagettftext函數是從文字左下角為參照switch($mark_postion){case 1: //位置左下角$mark_xy[0] = 5; //浮水印英文字串座標X$mark_xy[1] = $img_info[1]-20;//浮水印英文字串座標Y$mark_xy[2] = 5; //浮水印中文字串座標X$mark_xy[3] = $img_info[1]-5;//浮水印中文字串座標Y$mark_xy[4] = 5;//浮水印圖片座標X$mark_xy[5] = $img_info[1]-$logo_info[1]-5;//浮水印圖片座標Ybreak;case 2: //位置右下角$mark_xy[0] = $img_info[0]-$stre_w; //浮水印英文字串座標X$mark_xy[1] = $img_info[1]-20;//浮水印英文字串座標Y$mark_xy[2] = $img_info[0]-$strc_w; //浮水印中文字串座標X$mark_xy[3] = $img_info[1]-5;//浮水印中文字串座標Y$mark_xy[4] = $img_info[0]-$logo_info[0]-5;//浮水印圖片座標X$mark_xy[5] = $img_info[1]-$logo_info[1]-5;//浮水印圖片座標Ybreak;case 3: //位置左上方$mark_xy[0] = 5; //浮水印英文字串座標X$mark_xy[1] = 5;//浮水印英文字串座標Y$mark_xy[2] = 5; //浮水印中文字串座標X$mark_xy[3] = 15;//浮水印中文字串座標Y$mark_xy[4] = 5;//浮水印圖片座標X$mark_xy[5] = 5;//浮水印圖片座標Ybreak;case 4: //位置右上方$mark_xy[0] = $img_info[0]-$stre_w; //浮水印英文字串座標X$mark_xy[1] = 5;//浮水印英文字串座標Y$mark_xy[2] = $img_info[0]-$strc_w; //浮水印中文字串座標X$mark_xy[3] = 15;//浮水印中文字串座標Y$mark_xy[4] = $img_info[0]-$logo_info[0]-5;//浮水印圖片座標X$mark_xy[5] = 5;//浮水印圖片座標Ybreak;default : //其它預設為右下角$mark_xy[0] = $img_info[0]-$stre_w; //浮水印英文字串座標X$mark_xy[1] = $img_info[1]-5;//浮水印英文字串座標Y$mark_xy[2] = $img_info[0]-$strc_w; //浮水印中文字串座標X$mark_xy[3] = $img_info[1]-15;//浮水印中文字串座標Y$mark_xy[4] = $img_info[0]-$logo_info[0]-5;//浮水印圖片座標X$mark_xy[5] = $img_info[1]-$logo_info[1]-5;//浮水印圖片座標Ybreak;}return $mark_xy;}/*** 建立源圖片* @access private* @param string $source_img 源圖片(路徑+檔案名稱)* @return img 從目標檔案建立的映像*/private function img_create($source_img) {$info = getimagesize($source_img);switch ($info[2]){case 1:if(!function_exists('imagecreatefromgif')){$source = @imagecreatefromjpeg($source_img);}else{$source = @imagecreatefromgif($source_img);}break;case 2:$source = @imagecreatefromjpeg($source_img);break;case 3:$source = @imagecreatefrompng($source_img);break;case 6:$source = @imagecreatefromwbmp($source_img);break;default:$source = FALSE;break;}return $source;}/*** 重新命名圖片* @access private* @param string $source_img 源圖片路徑+檔案名稱* @return string $dst_name 重新命名後的圖片名(路徑+檔案名稱)*/private function set_newname($sourse_img) {$info = pathinfo($sourse_img);$new_name = $this->resize_w.'_'.$this->resize_h.'_'.$info['basename'];//將檔案名稱修改為:寬_高_檔案名稱if($this->dst_path == ''){ //如果存放縮圖路徑為空白則預設為源檔案同檔案夾$dst_name = str_replace($info['basename'],$new_name,$sourse_img);}else{$dst_name = $this->dst_path.$new_name;}return $dst_name;}/*** 輸出圖片* @access private* @param $im 處理後的圖片* @param $dst_name 輸出後的的圖片名(路徑+檔案名稱)* @return 輸出圖片*/public function img_output($im,$dst_name) {$info = getimagesize($this->source_img);switch ($info[2]){case 1:if(!function_exists('imagegif')){imagejpeg($im,$dst_name);}else{imagegif($im, $dst_name);}break;case 2:imagejpeg($im,$dst_name);break;case 3:imagepng($im,$dst_name);break;case 6:imagewbmp($im,$dst_name);break;}}}?>就是這些了,有關php檔案上傳原理,大家有時間也可以看看,以更好地理解以上的代碼。 |