">
当前位置:Gxlcms > PHP教程 > php图片水印代码,php上传图片加水印实例代码

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

时间:2021-07-01 10:21:17 帮助过:19人阅读

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

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

人气教程排行