PHP图片上传程序(完整版)

从PHP100上搜刮来的,功能很强大。几乎考虑到了每个细节,与大家分享!~~~

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