PHP图片上传小程序(功能强大,考虑周全)
功能强大,考虑细节多,程序不长,而且编写很有条理,非常值得学习。
上传前:
上传后:
下面为代码:
1 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 2 3 <?php 4 /****************************************************************************** 5 6 参数说明: 7 $max_file_size : 上传文件大小限制, 单位BYTE 8 $destination_folder : 上传文件路径 9 $watermark : 是否附加水印(1为加水印,其他为不加水印); 10 11 使用说明: 12 1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库; 13 2. 将extension_dir =改为你的php_gd2.dll所在目录; 14 ******************************************************************************/ 15 16 //上传文件类型列表 17 $uptypes=array( 18 'image/jpg', 19 'image/jpeg', 20 'image/png', 21 'image/pjpeg', 22 'image/gif', 23 'image/bmp', 24 'image/x-png' 25 ); 26 27 $max_file_size=2000000; //上传文件大小限制, 单位BYTE 28 $destination_folder="uploadimg/"; //上传文件路径 29 $watermark=1; //是否附加水印(1为加水印,其他为不加水印); 30 $watertype=1; //水印类型(1为文字,2为图片) 31 $waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中); 32 $waterstring="http://www.xplore.cn/"; //水印字符串 33 $waterimg="xplore.gif"; //水印图片 34 $imgpreview=1; //是否生成预览图(1为生成,其他为不生成); 35 $imgpreviewsize=1/2; //缩略图比例 36 ?> 37 <html> 38 <head> 39 <title>ZwelL图片上传程序</title> 40 <style type="text/css"> 41 <!-- 42 body 43 { 44 font-size: 9pt; 45 } 46 input 47 { 48 background-color: #66CCFF; 49 border: 1px inset #CCCCCC; 50 } 51 --> 52 </style> 53 </head> 54 55 <body> 56 <form enctype="multipart/form-data" method="post" name="upform"> 57 上传文件: 58 <input name="upfile" type="file"> 59 <input type="submit" value="上传"><br> 60 允许上传的文件类型为:<?=implode(', ',$uptypes)?> 61 </form> 62 63 <?php 64 if ($_SERVER['REQUEST_METHOD'] == 'POST') 65 { 66 if (!is_uploaded_file($_FILES["upfile"][tmp_name])) 67 //是否存在文件 68 { 69 echo "图片不存在!"; 70 exit; 71 } 72 73 $file = $_FILES["upfile"]; 74 if($max_file_size < $file["size"]) 75 //检查文件大小 76 { 77 echo "文件太大!"; 78 exit; 79 } 80 81 if(!in_array($file["type"], $uptypes)) 82 //检查文件类型 83 { 84 echo "文件类型不符!".$file["type"]; 85 exit; 86 } 87 88 if(!file_exists($destination_folder)) 89 { 90 mkdir($destination_folder); 91 } 92 93 $filename=$file["tmp_name"]; 94 $image_size = getimagesize($filename); 95 $pinfo=pathinfo($file["name"]); 96 $ftype=$pinfo['extension']; 97 $destination = $destination_folder.time().".".$ftype; 98 if (file_exists($destination) && $overwrite != true) 99 { 100 echo "同名文件已经存在了"; 101 exit; 102 } 103 104 if(!move_uploaded_file ($filename, $destination)) 105 { 106 echo "移动文件出错"; 107 exit; 108 } 109 110 $pinfo=pathinfo($destination); 111 $fname=$pinfo[basename]; 112 echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>"; 113 echo " 宽度:".$image_size[0]; 114 echo " 长度:".$image_size[1]; 115 echo "<br> 大小:".$file["size"]." bytes"; 116 117 if($watermark==1) 118 { 119 $iinfo=getimagesize($destination,$iinfo); 120 $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); 121 $white=imagecolorallocate($nimage,255,255,255); 122 $black=imagecolorallocate($nimage,0,0,0); 123 $red=imagecolorallocate($nimage,255,0,0); 124 imagefill($nimage,0,0,$white); 125 switch ($iinfo[2]) 126 { 127 case 1: 128 $simage =imagecreatefromgif($destination); 129 break; 130 case 2: 131 $simage =imagecreatefromjpeg($destination); 132 break; 133 case 3: 134 $simage =imagecreatefrompng($destination); 135 break; 136 case 6: 137 $simage =imagecreatefromwbmp($destination); 138 break; 139 default: 140 die("不支持的文件类型"); 141 exit; 142 } 143 144 imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); 145 imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); 146 147 switch($watertype) 148 { 149 case 1: //加水印字符串 150 imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); 151 break; 152 case 2: //加水印图片 153 $simage1 =imagecreatefromgif("xplore.gif"); 154 imagecopy($nimage,$simage1,0,0,0,0,85,15); 155 imagedestroy($simage1); 156 break; 157 } 158 159 switch ($iinfo[2]) 160 { 161 case 1: 162 //imagegif($nimage, $destination); 163 imagejpeg($nimage, $destination); 164 break; 165 case 2: 166 imagejpeg($nimage, $destination); 167 break; 168 case 3: 169 imagepng($nimage, $destination); 170 break; 171 case 6: 172 imagewbmp($nimage, $destination); 173 //imagejpeg($nimage, $destination); 174 break; 175 } 176 177 //覆盖原上传文件 178 imagedestroy($nimage); 179 imagedestroy($simage); 180 } 181 182 if($imgpreview==1) 183 { 184 echo "<br>图片预览:<br>"; 185 echo "<img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize); 186 echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\">"; 187 } 188 } 189 ?> 190 </body> 191 </html>
参考文章:http://blog.csdn.net/niushuai666/article/details/6672808