小灰灰在使用哦,记录一下,以备后用!!
上传之后效果图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | <?php header("content-type:text/html;charset=utf8"); //上传文件类型列表 $uptypes=array( 'image/jpg', 'image/jpeg', 'image/png', 'image/jpeg', 'image/gif', 'image/x-png' ); $time_up=date("Ym",time()); $max_file_size=2000000; //上传文件大小限制, 单位BYTE $destination_folder="../upload/$time_up/"; //上传文件路径 $watermark=0; //是否附加水印(1为加水印,其他为不加水印); $watertype=1; //水印类型(1为文字,2为图片) $waterposition=2; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); $waterstring="by:xiaohuihui"; //水印字符串 $waterimg="xplore.gif"; //水印图片 $imgpreview=1; //是否生成预览图(1为生成,其他为不生成); $imgpreviewsize=1/2; //缩略图比例 if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (!is_uploaded_file($_FILES["upfile"][tmp_name])){ //是否存在文件 echo "图片不存在!";exit; } $file = $_FILES["upfile"]; if($max_file_size < $file["size"]){ //检查文件大小 echo "文件太大!";exit; } if(!in_array($file["type"], $uptypes)){ //检查文件类型 echo "文件类型不符!".$file["type"];exit; } if(!file_exists($destination_folder)) { mkdir($destination_folder); } $filename=$file["tmp_name"]; $image_size = getimagesize($filename); $pinfo=pathinfo($file["name"]); $ftype=$pinfo['extension']; $destination = $destination_folder.time().".".$ftype; if (file_exists($destination) && $overwrite != true) { echo "同名文件已经存在了"; exit; } if(!move_uploaded_file ($filename, $destination)) { echo "移动文件出错"; exit; } $pinfo=pathinfo($destination); $fname=$pinfo[basename]; $pic_name = str_replace("../","/",$destination); $file_size= getFilesize($file["size"]); echo "<div class=\"form\">"; echo " <font color=red><b>已经成功上传!</b></font><br><br>"; echo " 宽度:".$image_size[0]."px<br>"; echo " 长度:".$image_size[1]."px<br>"; echo " 大小:".$file_size." <br><br>"; echo "</div>"; $filename1="pic_json"; /*创建json文件并保存到相应的文件夹下*/ $data_pic_json =json_encode($pinfo); $path="../cache/pic_json/"; if (is_dir($path)){ file_put_contents($path.$filename1.'.json',$data_pic_json);//写入*/ }else{ //第三个参数是“true”表示能创建多级目录,iconv防止中文目录乱码 $res=mkdir(iconv("UTF-8", "GBK", $path),0777,true); if ($res){ file_put_contents($path.$filename1.'.json',$data_pic_json);//写入*/ }else{ echo "目录 $path 创建失败"; } } if($watermark==1) { $iinfo=getimagesize($destination,$iinfo); $nimage=imagecreatetruecolor($image_size[0],$image_size[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($destination); break; case 2: $simage =imagecreatefromjpeg($destination); break; case 3: $simage =imagecreatefrompng($destination); break; case 6: $simage =imagecreatefromwbmp($destination); break; default: die("不支持的文件类型"); exit; } imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); switch($watertype) { case 1: //加水印字符串 imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); break; case 2: //加水印图片 $simage1 =imagecreatefromgif("xplore.gif"); imagecopy($nimage,$simage1,0,0,0,0,85,15); imagedestroy($simage1); break; } switch ($iinfo[2]) { case 1: //imagegif($nimage, $destination); imagejpeg($nimage, $destination); break; case 2: imagejpeg($nimage, $destination); break; case 3: imagepng($nimage, $destination); break; case 6: imagewbmp($nimage, $destination); //imagejpeg($nimage, $destination); break; } //覆盖原上传文件 imagedestroy($nimage); imagedestroy($simage); } if($imgpreview==1) { echo "<div class=\"form\">"; echo "<img src=\"".$destination."\" style='max-height:130px'"; echo "</div>"; } } //字节数Byte转换为KB、MB、GB、TB的方法 function getFilesize($num){ $p = 0; $format='bytes'; if($num>0 && $num<1024){ $p = 0; return number_format($num).' '.$format; } if($num>=1024 && $num<pow(1024, 2)){ $p = 1; $format = 'KB'; } if ($num>=pow(1024, 2) && $num<pow(1024, 3)) { $p = 2; $format = 'MB'; } if ($num>=pow(1024, 3) && $num<pow(1024, 4)) { $p = 3; $format = 'GB'; } if ($num>=pow(1024, 4) && $num<pow(1024, 5)) { $p = 3; $format = 'TB'; } $num /= pow(1024, $p); return number_format($num, 3).' '.$format; } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>上传图片</title> <style type="text/css"> body{font-family: "微软雅黑";font-size: 16px;} .form{margin-top: 20px;margin-left: 25px;} input[type=file]{ width: 250px; font-size: 15px;padding: 5px; background: #1E9FFF; color:#fff; } input[type=submit]{display: inline-block; padding: 8px 18px; background-color: #009688; color: #fff; white-space: nowrap; text-align: center; font-size: 14px; border: none; border-radius: 2px; cursor: pointer; opacity: .9;} .up_pic_wenzi{margin-top: 13px;} </style> </head> <body> <div class="form" style="margin-left: 0"> <form enctype="multipart/form-data" method="post" name="upform"> <input name="upfile" type="file"> <input type="submit" value="上传图片"><br> <div class="up_pic_wenzi"> 允许上传的文件类型为:<?=implode(', ',$uptypes)?><br><br> </div> </form> </div> </body> </html> |
补充说明:上传之后会自动创建json数据(图片信息),已便回调使用。
小灰灰亲测修改并使用,如有错误或不完善之处还请多多支持,谢谢!