这是一款完美的php文件上传代码,图片上传成功后并自动给图片增加上水印,这样很好的快速的提高的了要手工一张张增加水印效果,代码如下:
- 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;
- $watertype=$watertype;
- $watercontent=$content;
- if(emptyempty($uploadfile['name'][$i])) {
- die("未选择文件上传");
- }
- if($uploadfile['error'][$i] == 2) {
- die("上传的文件太大了");
- }
- $allow_filemaxsize = 2048000;
- $filesize = $uploadfile['size'][$i];
- if($filesize > $allow_filemaxsize) {
- die("上传的文件太大了");
- }
- $allow_filetypes = array("jpeg", "gif", "png","jpg","pjpeg");
- $allow = false;
- $mimetype = $uploadfile['type'][$i];
- 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;
-
- $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);
- 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") {
-
-
- $picture = upload($_files['picture'],1,2,"img/watermark.gif");
-
- }
上传代码如下:
- <form method="post" action="" enctype="multipart/form-data">
- <table border="1" cellpadding="5" cellspacing="0">
- <tr>
- <td>产品图片:</td>
- <td><input type="hidden" name="max_file_size" value="1024000">
- <input type="file" name="picture[]">
- </td>
- </tr>
- <tr>
- <td colspan="2" align="center"><input type="submit" value="提交"></td>
- </tr>
- </table>
- </form>
|