Heim >Backend-Entwicklung >PHP-Tutorial >php图片加水印与上传图片加水印类

php图片加水印与上传图片加水印类

WBOY
WBOYOriginal
2016-07-25 09:07:401223Durchsuche
  1. //上传文件类型列表
  2. $uptypes=array(
  3. 'image/jpg',
  4. 'image/jpeg',
  5. 'image/png',
  6. 'image/pjpeg',
  7. 'image/gif',
  8. 'image/bmp',
  9. 'image/x-png'
  10. );
  11. $max_file_size=2000000; //上传文件大小限制, 单位BYTE
  12. $destination_folder="uploadimg/"; //上传文件路径
  13. $watermark=1; //是否附加水印(1为加水印,其他为不加水印);
  14. $watertype=1; //水印类型(1为文字,2为图片)
  15. $waterposition=1; //水印位置(1为左下角,2为右下角
  16. ,3为左上角,4为右上角,5为居中);
  17. $waterstring="
  18. http://www.xplore.cn/
  19. "; //水印字符串
  20. $waterimg="xplore.gif"; //水印图片
  21. $imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
  22. $imgpreviewsize=1/2; //缩略图比例
  23. ?>
  24. ZwelL图片上传程序
  25. method="post" name="upform">
  26. 上传文件:

  27. 允许上传的文件类型为:=implode(', ',$uptypes)?>
  28. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  29. {
  30. if (!is_uploaded_file($_FILES["upfile"]
  31. [tmp_name]))
  32. //是否存在文件
  33. {
  34. echo "图片不存在!";
  35. exit;
  36. }
  37. $file = $_FILES["upfile"];
  38. if($max_file_size //检查文件大小
  39. {
  40. echo "文件太大!";
  41. exit;
  42. }
  43. if(!in_array($file["type"], $uptypes))
  44. //检查文件类型
  45. {
  46. echo "文件类型不符!".$file["type"];
  47. exit;
  48. }
  49. if(!file_exists($destination_folder))
  50. {
  51. mkdir($destination_folder);
  52. }
  53. $filename=$file["tmp_name"];
  54. $image_size = getimagesize($filename);
  55. $pinfo=pathinfo($file["name"]);
  56. $ftype=$pinfo['extension'];
  57. $destination = $destination_folder.
  58. time().".".$ftype;
  59. if (file_exists($destination) &&
  60. $overwrite != true)
  61. {
  62. echo "同名文件已经存在了";
  63. exit;
  64. }
  65. if(!move_uploaded_file ($filename,
  66. $destination))
  67. {
  68. echo "移动文件出错";
  69. exit;
  70. }
  71. $pinfo=pathinfo($destination);
  72. $fname=$pinfo[basename];
  73. echo " 已经成功上传

  74. 文件名:
  75. ".$destination_folder.
  76. $fname."

  77. ";
  78. echo " 宽度:".$image_size[0];
  79. echo " 长度:".$image_size[1];
  80. echo "
    大小:".$file["size"]." bytes";
  81. if($watermark==1)
  82. {
  83. $iinfo=getimagesize($destination,$iinfo);
  84. $nimage=imagecreatetruecolor($image_size[0]
  85. ,$image_size[1]);
  86. $white=imagecolorallocate($nimage,255,255,255);
  87. $black=imagecolorallocate($nimage,0,0,0);
  88. $red=imagecolorallocate($nimage,255,0,0);
  89. imagefill($nimage,0,0,$white);
  90. switch ($iinfo[2])
  91. {
  92. case 1:
  93. $simage =imagecreatefromgif($destination);
  94. break;
  95. case 2:
  96. $simage =imagecreatefromjpeg($destination);
  97. break;
  98. case 3:
  99. $simage =imagecreatefrompng($destination);
  100. break;
  101. case 6:
  102. $simage =imagecreatefromwbmp($destination);
  103. break;
  104. default:
  105. die("不支持的文件类型");
  106. exit;
  107. }
  108. imagecopy($nimage,$simage,0,0,0,0,
  109. $image_size[0],$image_size[1]);
  110. imagefilledrectangle($nimage,1,
  111. $image_size[1]-15,80,$image_size[1],$white);
  112. switch($watertype)
  113. {
  114. case 1: //加水印字符串
  115. imagestring($nimage,2,3,$image_size[1]-15,
  116. $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 "
    图片预览:
    ";
  148. echo "height=".($image_size[1]*$imgpreviewsize);"
  149. echo " alt=\"图片预览:\r文件名:".
  150. $destination."\r上传时间:\" />";
  151. }
  152. }
  153. ?>
复制代码



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn