function upload($uploadfile,$watermark=1,$watertype=1,$content){ foreach($uploadfile['name'] as $key=>$name) { //多檔案上傳函數 uploadall($uploadfile,$key,$watermark,$watertype,$content); } } function uploadall($uploadfile,$i,$watermark,$watertype,$content) { $watermark=$watermark; //是否附加浮水印(1為加浮水印,其他為不加浮水印); $watertype=$watertype; //浮水印類型(1為文字,2為圖片) $watercontent=$content; //浮水印的內容 if(empty($uploadfile['name'][$i])) { die("未選擇檔案上傳"); } if($uploadfile['error'][$i] == 2) { //驗證html判斷結果 die("上傳的檔案太大了"); } $allow_filemaxsize = 2048000; // 2m $filesize = $uploadfile['size'][$i]; if($filesize > $allow_filemaxsize) { die("上傳的檔案太大了"); } $allow_filetypes = array("jpeg", "gif", "png","jpg","pjpeg");//允許上傳的檔案統一資源類型 mimetype $allow = false; //預設都不允許 $mimetype = $uploadfile['type'][$i]; //上傳檔案的mime檔案類型 foreach($allow_filetypes as $t) { if(strpos($mimetype, $t) !== false) { $allow = true; //找到了符合上傳條件的檔案類型 break; } } if($allow == false) { die("上傳的檔案類型不被允許"); } $result = is_uploaded_file($uploadfile['tmp_name'][$i]); //判斷是否為上傳動作產生的 if(!$result) { die("上傳的檔案有誤"); } $uploaddir = "img/"; //上傳檔案儲存目錄 if(!file_exists($uploaddir)) mkdir($uploaddir, 0777, true);//若上傳儲存目錄不存在,則遞迴建立 /**重新命名檔案**/ $filetype = explode(".", $uploadfile['name'][$i]); $filetype = array_pop($filetype); $uploadfilename = time().".".$filetype; $_session['filename'] = $uploadfilename; //end $result = move_uploaded_file($uploadfile['tmp_name'][$i], $uploaddir.$uploadfilename); if($result) { echo "檔案上傳成功"; }else{ switch($uploadfile['error'][$i]) { case 1:return "上傳的檔案超出了php.ini中設定的最大值";break; case 2:return "上傳的檔案超出了html中設定的最大值";break; case 3:return "檔案只有部分被上傳";break; case 4;return "沒有檔案被上傳";break; default:die("檔案上傳失敗"); } } if($watermark==1) { $iinfo=getimagesize($uploaddir.$uploadfilename); //擷取圖片的相關資訊,得到數組 $nimage=imagecreatetruecolor($iinfo[0],$iinfo[1]); $white=imagecolorallocate($nimage,255,255,255); //設定背景顏色為白色 $black=imagecolorallocate($nimage,0,0,0); //設定背景顏色為黑色 $red=imagecolorallocate($nimage,255,0,0); //設定背景顏色為紅色 imagefill($nimage,0,0,$white); //背景填充為白色 switch ($iinfo[2]) { case 1: $simage =imagecreatefromgif($uploaddir.$uploadfilename); break; case 2: $simage =imagecreatefromjpeg($uploaddir.$uploadfilename); break; case 3: $simage =imagecreatefrompng($uploaddir.$uploadfilename); break; case 6: $simage =imagecreatefromwbmp($uploaddir.$uploadfilename); break; default: die("不支援的檔案類型"); exit; } imagecopy($nimage,$simage,0,0,0,0,$iinfo['0'],$iinfo['1']); switch($watertype) { case 1: //加浮水印字串 imagestring($nimage,5,$iinfo['0']/2-50,$iinfo['1']-30,$watercontent,$black); break; case 2: //加浮水印圖片 $simage1 =imagecreatefromgif($watercontent); $size = getimagesize($watercontent); imagecopy($nimage,$simage1,$iinfo['0']/2+50,$iinfo['1']-100,0,0,$size[0],$size[1]); imagedestroy($simage1); break; } switch ($iinfo[2]) { case 1: imagejpeg($nimage, $uploaddir.$uploadfilename); //將映像$nimage以$destination檔案名稱建立一個jpeg的格式檔案 break; case 2: imagejpeg($nimage, $uploaddir.$uploadfilename); break; case 3: imagepng($nimage, $uploaddir.$uploadfilename); break; case 6: imagewbmp($nimage, $uploaddir.$uploadfilename); break; } imagedestroy($nimage); //覆蓋原上傳檔案 imagedestroy($simage); } } if(@$_get['act'] == "insert") { //未作參數校正 //該函數的四個參數分別是:上傳控制項的name值;是否加浮水印(1為加,其他數字為不佳); //浮水印的類型(1為字串,2為圖片);浮水印的內容,字串時寫資料,圖片時寫圖片的地址; $picture = upload($_files['picture'],1,2,"img/watermark.gif");//上傳檔案,並返回上傳後的檔案路徑名 } ?> |