Heim > Backend-Entwicklung > PHP-Tutorial > 一个加强版的php水印代码

一个加强版的php水印代码

WBOY
Freigeben: 2016-07-25 09:07:15
Original
853 Leute haben es durchsucht
  1. /*

  2. * 功能:PHP图片水印 (水印支持图片或文字)
  3. * 参数:
  4. * $groundImage 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
  5. * $waterPos 水印位置,有10种状态,0为随机位置;
  6. * 1为顶端居左,2为顶端居中,3为顶端居右;
  7. * 4为中部居左,5为中部居中,6为中部居右;
  8. * 7为底端居左,8为底端居中,9为底端居右;
  9. * $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
  10. * $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
  11. * $textFont 文字大小,值为1、2、3、4或5,默认为5;
  12. * $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
  13. *
  14. * 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
  15. * $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。
  16. * 当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。
  17. * 加水印后的图片的文件名和 $groundImage 一样。
  18. * 作者:longware
  19. */
  20. function imageWaterMark($groundImage,$waterPos=9,$waterImage=”",$waterText=”",$textFont=5,$textColor=”#FF0000″)
  21. {
  22. $isWaterImage = FALSE;
  23. $formatMsg = “暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。”;
  24. //读取水印文件
  25. if(!empty($waterImage) && file_exists($waterImage))
  26. {
  27. $isWaterImage = TRUE;
  28. $water_info = getimagesize($waterImage);
  29. $water_w = $water_info[0];//取得水印图片的宽
  30. $water_h = $water_info[1];//取得水印图片的高
  31. switch($water_info[2])//取得水印图片的格式
  32. {
  33. case 1:$water_im = imagecreatefromgif($waterImage);break;
  34. case 2:$water_im = imagecreatefromjpeg($waterImage);break;
  35. case 3:$water_im = imagecreatefrompng($waterImage);break;
  36. default:die($formatMsg);
  37. }
  38. }
  39. //读取背景图片
  40. if(!empty($groundImage) && file_exists($groundImage))
  41. {
  42. $ground_info = getimagesize($groundImage);
  43. $ground_w = $ground_info[0];//取得背景图片的宽
  44. $ground_h = $ground_info[1];//取得背景图片的高
  45. switch($ground_info[2])//取得背景图片的格式
  46. {
  47. case 1:$ground_im = imagecreatefromgif($groundImage);break;
  48. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  49. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  50. default:die($formatMsg);
  51. }
  52. }
  53. else
  54. {
  55. die(“需要加水印的图片不存在!”);
  56. }
  57. //水印位置
  58. if($isWaterImage)//图片水印
  59. {
  60. $w = $water_w;
  61. $h = $water_h;
  62. $label = “图片的”;
  63. }
  64. else//文字水印
  65. {
  66. $temp = imagettfbbox(ceil($textFont*2.5),0,”simfang.ttf”,$waterText);//取得使用 TrueType 字体的文本的范围
  67. $w = $temp[2] – $temp[6];
  68. $h = $temp[3] – $temp[7];
  69. unset($temp);
  70. $label = “文字区域”;
  71. }
  72. if( ($ground_w{
  73. echo “需要加水印的图片的长度或宽度比水印”.$label.”还小,无法生成水印!”;
  74. return;
  75. }
  76. switch($waterPos)
  77. {
  78. case 0://随机
  79. $posX = rand(0,($ground_w – $w));
  80. $posY = rand(0,($ground_h – $h));
  81. break;
  82. case 1://1为顶端居左
  83. $posX = 0;
  84. $posY = 0;
  85. break;
  86. case 2://2为顶端居中
  87. $posX = ($ground_w – $w) / 2;
  88. $posY = 0;
  89. break;
  90. case 3://3为顶端居右
  91. $posX = $ground_w – $w;
  92. $posY = 0;
  93. break;
  94. case 4://4为中部居左
  95. $posX = 0;
  96. $posY = ($ground_h – $h) / 2;
  97. break;
  98. case 5://5为中部居中
  99. $posX = ($ground_w – $w) / 2;
  100. $posY = ($ground_h – $h) / 2;
  101. break;
  102. case 6://6为中部居右
  103. $posX = $ground_w – $w;
  104. $posY = ($ground_h – $h) / 2;
  105. break;
  106. case 7://7为底端居左
  107. $posX = 0;
  108. $posY = $ground_h – $h;
  109. break;
  110. case 8://8为底端居中
  111. $posX = ($ground_w – $w) / 2;
  112. $posY = $ground_h – $h;
  113. break;
  114. case 9://9为底端居右
  115. $posX = $ground_w – $w;
  116. $posY = $ground_h – $h;
  117. break;
  118. default://随机
  119. $posX = rand(0,($ground_w – $w));
  120. $posY = rand(0,($ground_h – $h));
  121. break;
  122. }
  123. //设定图像的混色模式
  124. imagealphablending($ground_im, true);
  125. if($isWaterImage)//图片水印
  126. {
  127. imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
  128. }
  129. else//文字水印
  130. {
  131. if( !empty($textColor) && (strlen($textColor)==7) )
  132. {
  133. $R = hexdec(substr($textColor,1,2));
  134. $G = hexdec(substr($textColor,3,2));
  135. $B = hexdec(substr($textColor,5));
  136. }
  137. else
  138. {
  139. die(“水印文字颜色格式不正确!”);
  140. }
  141. //$waterText=iconv(‘utf-8′,’gbk’,$waterText);
  142. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  143. }
  144. //生成水印后的图片
  145. @unlink($groundImage);
  146. switch($ground_info[2])//取得背景图片的格式
  147. {
  148. case 1:imagegif($ground_im,$groundImage);break;
  149. case 2:imagejpeg($ground_im,$groundImage);break;
  150. case 3:imagepng($ground_im,$groundImage);break;
  151. default:die($errorMsg);
  152. }
  153. //释放内存
  154. if(isset($water_info)) unset($water_info);
  155. if(isset($water_im)) imagedestroy($water_im);
  156. unset($ground_info);
  157. imagedestroy($ground_im);
  158. }
  159. //------------------

  160. if(isset($_FILES) && !empty($_FILES['userfile']) && $_FILES['userfile']['size']>0)
  161. {
  162. $uploadfile = “./”.time().”_”.$_FILES['userfile']['name'];
  163. if (copy($_FILES['userfile']['tmp_name'], $uploadfile))
  164. {
  165. echo “OK
  166. “;
  167. //文字水印
  168. imageWaterMark($uploadfile,9,”Noname.gif”,”www.nj-kingsway.com “,5,”#FF0000″);
  169. //图片水印
  170. //$waterImage=”./Hanweb_shuiyin.gif”;//水印图片路径
  171. //imageWaterMark($uploadfile,0,$waterImage);
  172. echo ““;
  173. }
  174. else
  175. {
  176. echo “Fail
  177. “;
  178. }
  179. }
  180. ?>
复制代码



Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
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
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage