php 图片上传代码,支持水印,缩略图

此代码可以为上传的图片加水印同时还可以比例缩放图片,有需要的朋友可以参考一下,php 图片上传代码如下:

  1. <?php
  2. $uptypes=array('image/jpg', //上传文件类型列表
  3. 'image/jpeg',
  4. 'image/png',
  5. 'image/pjpeg',
  6. 'image/gif',
  7. 'image/bmp',
  8. 'image/x-png');
  9. $max_file_size=5000000; //上传文件大小限制, 单位BYTE
  10. $destination_folder="upload/"; //上传文件路径
  11. $watermark=1; //是否附加水印(1为加水印,其他为不加水印);
  12. $watertype=1; //水印类型(1为文字,2为图片)
  13. $waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
  14. $waterstring="newphp.site.cz"; //水印字符串
  15. $waterimg="xplore.gif"; //水印图片
  16. $imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
  17. $imgpreviewsize=1/2; //缩略图比例
  18. ?>
  19. <html>
  20. <head>
  21. <title>M4U BLOG - fywyj.cn</title>
  22. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  23. <style type="text/css">body,td{font-family:tahoma,verdana,arial;font-size:11px;line-height:15px;background-color:white;color:#666666;margin-left:20px;}
  24. strong{font-size:12px;}
  25. aink{color:#0066CC;}
  26. a:hover{color:#FF6600;}
  27. aisited{color:#003366;}
  28. a:active{color:#9DCC00;}
  29. table.itable{}
  30. td.irows{height:20px;background:url("index.php?i=dots" repeat-x bottom}</style>
  31. </head>
  32. <body>
  33. <center><form enctype="multipart/form-data" method="post" name="upform">
  34. 上传文件: <br><br><br>
  35. <input name="upfile" type="file" size="17">
  36. <input type="submit" value="上传" size="17"><br><br><br>
  37. 允许上传的文件类型为:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf <br><br>
  38. <a href="index.php">返回</a>
  39. </form>
  40. <?php
  41. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  42. {
  43. if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
  44. //是否存在文件
  45. {
  46. echo "<font color='red'>文件不存在!</font>";
  47. exit;
  48. }
  49. $file = $_FILES["upfile"];
  50. if($max_file_size < $file["size"])
  51. //检查文件大小
  52. {
  53. echo "<font color='red'>文件太大!</font>";
  54. exit;
  55. }
  56. if(!in_array($file["type"], $uptypes))
  57. //检查文件类型
  58. {
  59. echo "<font color='red'>只能上传图像文件或Flash!</font>";
  60. exit;
  61. }
  62. if(!file_exists($destination_folder))
  63. mkdir($destination_folder);
  64. $filename=$file["tmp_name"];
  65. $image_size = getimagesize($filename);
  66. $pinfo=pathinfo($file["name"]);
  67. $ftype=$pinfo[extension];
  68. $destination = $destination_folder.time().".".$ftype;
  69. if (file_exists($destination) && $overwrite != true)
  70. {
  71. echo "<font color='red'>同名文件已经存在了!</a>";
  72. exit;
  73. }
  74. //开源代码phpfensi.com
  75. if(!move_uploaded_file ($filename, $destination))
  76. {
  77. echo "<font color='red'>移动文件出错!</a>";
  78. exit;
  79. }
  80. $pinfo=pathinfo($destination);
  81. $fname=$pinfo[basename];
  82. echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";
  83. echo " 宽度:".$image_size[0];
  84. echo " 长度:".$image_size[1];
  85. if($watermark==1)
  86. {
  87. $iinfo=getimagesize($destination,$iinfo);
  88. $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
  89. $white=imagecolorallocate($nimage,255,255,255);
  90. $black=imagecolorallocate($nimage,0,0,0);
  91. $red=imagecolorallocate($nimage,255,0,0);
  92. imagefill($nimage,0,0,$white);
  93. switch ($iinfo[2])
  94. {
  95. case 1:
  96. $simage =imagecreatefromgif($destination);
  97. break;
  98. case 2:
  99. $simage =imagecreatefromjpeg($destination);
  100. break;
  101. case 3:
  102. $simage =imagecreatefrompng($destination);
  103. break;
  104. case 6:
  105. $simage =imagecreatefromwbmp($destination);
  106. break;
  107. default:
  108. die("<font color='red'>不能上传此类型文件!</a>");
  109. exit;
  110. }
  111. imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
  112. imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
  113. switch($watertype)
  114. {
  115. case 1: //加水印字符串
  116. imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
  117. break;
  118. case 2: //加水印图片
  119. $simage1 =imagecreatefromgif("xplore.gif");
  120. imagecopy($nimage,$simage1,0,0,0,0,85,15);
  121. imagedestroy($simage1);
  122. break;
  123. }
  124. switch ($iinfo[2])
  125. {
  126. case 1:
  127. //imagegif($nimage, $destination);
  128. imagejpeg($nimage, $destination);
  129. break;
  130. case 2:
  131. imagejpeg($nimage, $destination);
  132. break;
  133. case 3:
  134. imagepng($nimage, $destination);
  135. break;
  136. case 6:
  137. imagewbmp($nimage, $destination);
  138. //imagejpeg($nimage, $destination);
  139. break;
  140. }
  141. //覆盖原上传文件
  142. imagedestroy($nimage);
  143. imagedestroy($simage);
  144. }
  145. if($imgpreview==1)
  146. {
  147. echo "<br>图片预览:<br>";
  148. echo "<a href="".$destination."" target='_blank'><img src="".$destination."" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
  149. echo " alt="图片预览:r文件名:".$destination."r上传时间:" ></a>";
  150. }
  151. }
  152. ?>
  153. </center>
  154. </body>
  155. </html>