-
-
//--------------------------- ----------
- // 檔案說明:檔案上傳處理類別
- // 檔案作者:Jesse Lee
- //------------- ------------------------
- class upload {
- var $dir; //附件存放實體目錄
- var $time; //自訂檔案上傳時間
- var $allow_types; //允許上傳附件類型
- var $field; //上傳控制項名稱
- var $maxsize; //最大允許檔案大小,單位為KB
- var $thumb_width; //縮圖寬度
- var $thumb_height; //縮圖高度
- var $watermark_file; //水印圖片位址
- var $watermark_pos; //水印位置
- var🎜> var $watermark_pos; //水印位置
- var🎜> var $watermark_pos; //水印位置
- var🎜> var $watermark_pos; //水印位置
- var🎜> var $watermark_pos; //水印位置
- var🎜> var $watermark_pos; $watermark_trans;//浮水印透明度
- //建構子
- //$types : 允許上傳的檔案類型, $maxsize : 允許大小, $field : 上傳控制項名稱, $time : 自訂上傳時間
- function upload($types = 'jpg|png', $maxsize = 1024, $field = 'attach', $time = '') {
- $this->allow_types = explode('|',$types) ;
- $this->maxsize = $maxsize * 1024;
- $this->field = $field;
- $this->time = $time ? $time : time();
- }
- //設定並建立檔案特定存放的目錄
- //$basedir : 基底目錄,必須為實體路徑
- //$filedir : 自訂子目錄,可用參數{y}、{m} 、{d}
- function set_dir($basedir,$filedir = '') {
- $dir = $basedir;
- !is_dir($dir) && @mkdir($dir,0777); if (!empty($filedir)) {
- $filedir = str_replace(array('{y}','{m}','{y}'),array(date('Y',$this ->time),date('m',$this->time),date('d',$this->time)),strtolower($filedir));
- $dirs = explode('/' ,$filedir);
- foreach ($dirs as $d) {
- !empty($d) && $dir .= $d.'/';
- !is_dir($dir) && @mkdir ($dir,0777);
- }
- }
- $this->dir = $dir;
- }
- //圖片縮圖設置,如果不產生縮圖則不用設定
- //$width : 縮圖寬度, $height : 縮圖高度
- function set_thumb ($width = 0, $height = 0) {
- $this->thumb_width = $width;thumb_height = $height;
- }
- //圖片浮水印設置,如果不產生添加水印則不用設置
- //$file : 水印圖片, $pos : 水印位置, $trans : 水印透明度
- function set_watermark ($file, $pos = 6, $trans = 80) {
- $this->watermark_file = $file;
- $this->watermark_pos = $pos;
- $this
- $this->watermark_pos = $pos;
- $this ->watermark_trans = $trans;
- }
- /*---------------------------------- ------------------------------
- 執行檔上傳,處理完回傳一個包含上傳成功或失敗的檔案資訊數組,
- 其中:name 為檔案名,上傳成功時是上傳到伺服器上的檔案名,上傳失敗則是本地的檔案名稱
- dir 為伺服器上存放該附件的實體路徑,上傳失敗則是本地的檔案名稱
- dir 為伺服器上存放該附件的實體路徑,上傳失敗不存在該值
- size 為附件大小,上傳失敗不存在該值
- flag 為狀態標識,1表示成功,-1表示文件類型不允許,-2表示文件大小超出
- ------ -------------------------------------------------- --------- */
- function execute() {
- $files = array(); //成功上傳的檔案資訊
- $field = $this->field;
- $keys = array_keys($_FILES[$field]['name']);
- foreach ($keys as $key) {
- if (!$_FILES[$field]['name'][$ key]) continue;
-
- $fileext = $this->fileext($_FILES[$field]['name'][$key]); //取得檔案副檔名
- $filename = $ this->time.mt_rand(100,999).'.'.$fileext; //產生檔名
- $filedir = $this->dir; //附件實際存放目錄
- $filesize = $_FILES[$ field]['size'][$key]; //檔案大小
-
- //檔案類型不允許
- if (!in_array($fileext,$this->allow_types)) {
- $files[$key]['name'] = $_FILES[$field]['name'][$key];
- $files[$key]['flag'] = -1;
- continue ;
- }
- //檔案大小超出 if ($filesize > $this->maxsize) { $files[$key]['name'] = $_FILES[$field][ '名稱'][$key]; $files[$key]['flag'] = -2; continue; }
- $files[$key]['name'] = $filename;
- $files[$key]['dir'] = $filedir;
- $files[$key]['size'] = $filesize;
- //儲存上傳檔案並刪除暫存檔案
- if (is_uploaded_file($_FILES[$field]['tmp_name'][$key])) {
- move_uploaded_file($_FILES[$ field]['tmp_name'][$key],$filedir.$filename);
- @unlink($_FILES[$field]['tmp_name'][$key]);
- $files[$key ]['flag'] = 1;
- //對圖片進行加水印和產生縮圖
- if (in_array($fileext,array('jpg','png','gif'))) {
- if ($this->thumb_width) {
- if ($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)) {
- $files[$key] ['thumb'] = 'thumb_'.$filename; //縮圖檔名
- }
- }
- $this->create_watermark($filedir.$filename);
- }
- }
- }
- return $files;
- }
- //建立縮圖,以相同的副檔名產生縮圖
- //Php.aspx_file : 來源映像路徑, $thumb_file : 縮略圖路徑
- function create_thumb (Php.aspx_file,$thumb_file) {
- $t_width = $this->thumb_width;
- $t_height = $this->thumb_height_width;
- $t_height = $this->thumb_height; aspx_file)) return false;
- Php.aspx_info = getImageSize(Php.aspx_file);
- //如果來源映像小於或等於縮圖則拷貝來源映像為縮圖
- if (Php.aspx_info[0 ] if (!copy(Php.aspx_file,$thumb_file)) {
- return false;
- }
- return true ;
- }
- //按比例計算縮圖大小
- if (Php.aspx_info[0] - $t_width > Php.aspx_info[1] - $t_height) {
- $t_height = ($ t_width / Php.aspx_info[0]) * Php.aspx_info[1];
- } else {
- $t_width = ($t_height / Php.aspx_info[1]) * Php.aspx_info[0]; }
- //取得檔案副檔名
- $fileext = $this->fileext(Php.aspx_file);
- switch ($fileext) {
- case 'jpg' :
- Php. aspx_img = ImageCreateFromJPEG(Php.aspx_file); break;
- case 'png' :
- Php.aspx_img = ImageCreateFromPNG(Php.aspx_file); break;
- case '🎜 gif; ImageCreateFromGIF(Php.aspx_file); break;
- }
- //建立一個真彩色的縮圖
- $thumb_img = @ImageCreateTrueColor($t_width,$t_heightmple);的影像平滑度較好,優先考慮
- if (function_exists('imagecopyresampled')) {
- @ImageCopyResampled($thumb_img,Php.aspx_img,0,0,0,0,$t_wid,P$t_hex .aspx_info[0],Php.aspx_info[1]);
- } else {
- @ImageCopyResized($thumb_img,Php.aspx_img,0,0,0,0,$t_width,$tasp_height,P. [0],Php.aspx_info[1]);
- }
- //產生縮圖
- switch ($fileext) {
- case 'jpg' :
- ImageJPEG($thumb_img,$ thumb_file); break;
- case 'gif' :
- ImageGIF($thumb_img,$thumb_file); break;
- case 'png' :
- ImagePNG($thumb_breakimg,$thumb_breakimg); 🎜> }
- //銷毀臨時映像
- @ImageDestroy(Php.aspx_img);
- @ImageDestroy($thumb_img);
- return true;
- }
- //為圖片添加浮水印
- //$file : 要新增浮水印的檔案
- function create_watermark ($file) {
- //檔案不存在則回傳
- if (!file_exists( $this->watermark_file) || !file_exists($file)) return;
- if (!function_exists('getImageSize')) return;
-
- //檢查GD支持的文件類型
- $ gd_allow_types = array();
- if (function_exists('ImageCreateFromGIF')) $gd_allow_types['image/gif'] = 'ImageCreateFromGIF';
- if (function_existmageCreateFromGIF';
- if (function_exists)( png'] = 'ImageCreateFromPNG';
- if (function_exists('ImageCreateFromJPEG')) $gd_allow_types['image/jpeg'] = 'ImageCreateFromJPEG';
- // 取得檔案資料 $wminfo = getImageSize($this->watermark_file);
- if ($fileinfo[0] if (array_key_exists($fileinfo['mime'],$gd_allow_types)) {
- if (array_key_exists($wminfo['mime'],$gd_allow_types)) {
-
- //來自檔案建立映像
- $temp = $gd_allow_types[$fileinfo['mime']]($file);
- $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file);
- //浮水印位置
- switch ($this->watermark_pos) {
- case 1 : //頂部居左
- $dst_x = 0; $dst_y = 0; break;
- case 2 : //頂部居中
- $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break;
- case 3 : //上方居右
- $dst_x = $fileinfo[0]; $dst_y = 0; break;
- case 4 : //底部居左
- $dst_x = 0; $dst_y = $fileinfo[1]; break;
- case 5 : //底部居中
- $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break;
- case 6 : //底部居右邊
- $ dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break;
- default : //隨機
- $dst_x = mt_rand(0,$ fileinfo[0]-$wminfo[0]); $dst_y = mt_rand(0,$fileinfo[1]-$wminfo[1]);
- }
- if (function_exists('ImageAlphaBlending')) ImageAlphaBlending( $temp_wm,True); //設定影像的混色模式
- if (function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,True); //儲存完整的alpha 頻道資訊
- //為圖片新增浮水印
- if (function_exists('imageCopyMerge')) {
- ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1],$this- >watermark_trans);
- } else {
- ImageCopyMerge($temp,$temp_wm,$dst_x,$dst_y,0,0,$wminfo[0],$wminfo[1]);
- }
- //儲存圖片
- switch ($fileinfo['mime']) {
- case 'image/jpeg' :
- @imageJPEG($temp,$file);
- break;
- case 'image/png' :
- @imagePNG($temp,$file);
- break;
- case 'image/gif' :
- @imageGIF($temp,$file); break;
- }
- //銷毀零時圖片
- @imageDestroy($temp);
- @imageDestroy($temp_wm);
- }
- }
- }
- }
- //取得檔案副檔名
- function fileext($filename) {
- return strtolower(substr(strrchr($filename,'.'),1,10));
- }
- }
- ?>
-
複製程式碼
呼叫範例:
-
-
if ($_GET['action'] == '儲存') {
-
- $up =新上傳();
- $up->set_dir(dirname(__FILE__).'/upload/','{y}/{m}');
- $up->set_thumb(100,80 );
- $up->set_watermark(dirname(__FILE__).'/jblog/images/watermark.png',6,90);
- $fs = $up->execute();
-
- var_dump ($fs);
- }
- ?>
-
- 測試
-
-
-
複製程式碼
|