欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php图片水印代码,php上传图片加水印实例代码

程序员文章站 2022-04-23 08:16:06
...
  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="http://bbs.it-home.org/"; //水印字符串
  18. $waterimg="xplore.gif"; //水印图片
  19. $imgpreview=1; //是否生成预览图(1为生成,其他为不生成);
  20. $imgpreviewsize=1/2; //缩略图比例
  21. ?>
  22. ZwelL图片上传程序
  23. method="post" name="upform">
  24. 上传文件:

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

  72. 文件名:
  73. ".$destination_folder.
  74. $fname."

  75. ";
  76. echo " 宽度:".$image_size[0];
  77. echo " 长度:".$image_size[1];
  78. echo "
    大小:".$file["size"]." bytes";
  79. if($watermark==1)
  80. {
  81. $iinfo=getimagesize($destination,$iinfo);
  82. $nimage=imagecreatetruecolor($image_size[0]
  83. ,$image_size[1]);
  84. $white=imagecolorallocate($nimage,255,255,255);
  85. $black=imagecolorallocate($nimage,0,0,0);
  86. $red=imagecolorallocate($nimage,255,0,0);
  87. imagefill($nimage,0,0,$white);
  88. switch ($iinfo[2])
  89. {
  90. case 1:
  91. $simage =imagecreatefromgif($destination);
  92. break;
  93. case 2:
  94. $simage =imagecreatefromjpeg($destination);
  95. break;
  96. case 3:
  97. $simage =imagecreatefrompng($destination);
  98. break;
  99. case 6:
  100. $simage =imagecreatefromwbmp($destination);
  101. break;
  102. default:
  103. die("不支持的文件类型");
  104. exit;
  105. }
  106. imagecopy($nimage,$simage,0,0,0,0,
  107. $image_size[0],$image_size[1]);
  108. imagefilledrectangle($nimage,1,
  109. $image_size[1]-15,80,$image_size[1],$white);
  110. switch($watertype)
  111. {
  112. case 1: //加水印字符串
  113. imagestring($nimage,2,3,$image_size[1]-15,
  114. $waterstring,$black);
  115. break;
  116. case 2: //加水印图片
  117. $simage1 =imagecreatefromgif("xplore.gif");
  118. imagecopy($nimage,$simage1,0,0,0,0,85,15);
  119. imagedestroy($simage1);
  120. break;
  121. }
  122. switch ($iinfo[2])
  123. {
  124. case 1:
  125. //imagegif($nimage, $destination);
  126. imagejpeg($nimage, $destination);
  127. break;
  128. case 2:
  129. imagejpeg($nimage, $destination);
  130. break;
  131. case 3:
  132. imagepng($nimage, $destination);
  133. break;
  134. case 6:
  135. imagewbmp($nimage, $destination);
  136. //imagejpeg($nimage, $destination);
  137. break;
  138. }
  139. //覆盖原上传文件
  140. imagedestroy($nimage);
  141. imagedestroy($simage);
  142. }
  143. if($imgpreview==1)
  144. {
  145. echo "
    图片预览:
    ";
  146. echo "height=".($image_size[1]*$imgpreviewsize);"
  147. echo " alt=\"图片预览:\r文件名:".
  148. $destination."\r上传时间:\" />";
  149. }
  150. }
  151. ?>
复制代码