PHP添加文字水印或图片水印的水印类完整源代码与使用示例

今天小编就为大家分享一篇关于PHP添加文字水印或图片水印的水印类完整源代码与使用示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧。

PHP实现的给图片添加水印功能,可添加文字水印或图片水印,使用文字水印时需要提供字体文件,使用图片水印时需要提供水印图片,水印图片不能比要添加水印的图片大,请使用背景透明的水印图片。

该水印类支持自定义水印位置、自定义水印大小和水印的透明度,字体水印可自定义颜色等,功能已相应完善。

完整源代码如下(注解中已给出使用示例):

  1. <?php
  2. /**
  3. * 图片加水印类,支持文字水印、透明度设置、自定义水印位置等。
  4. * 使用示例:
  5. * $obj = new WaterMask($imgFileName); //实例化对象
  6. * $obj->$waterType = 1; //类型:0为文字水印、1为图片水印
  7. * $obj->$transparent = 45; //水印透明度
  8. * $obj->$waterStr = 'www.jb51.net'; //水印文字
  9. * $obj->$fontSize = 18; //文字字体大小
  10. * $obj->$fontColor = array(255,255,255); //水印文字颜色(RGB)
  11. * $obj->$fontFile = 'AHGBold.ttf'; //字体文件
  12. * ……
  13. * $obj->output(); //输出水印图片文件覆盖到输入的图片文件
  14. */
  15. class WaterMask{
  16. public $waterType = 0; //水印类型:0为文字水印、1为图片水印
  17. public $pos = 0; //水印位置
  18. public $transparent = 45; //水印透明度
  19. public $waterStr = 'www.jb51.net'; //水印文字
  20. public $fontSize = 18; //文字字体大小
  21. public $fontColor = array(255,255,255); //水印文字颜色(RGB)
  22. public $fontFile = 'AHGBold.ttf'; //字体文件
  23. public $waterImg = 'logo.png'; //水印图片
  24. private $srcImg = ''; //需要添加水印的图片
  25. private $im = ''; //图片句柄
  26. private $water_im = ''; //水印图片句柄
  27. private $srcImg_info = ''; //图片信息
  28. private $waterImg_info = ''; //水印图片信息
  29. private $str_w = ''; //水印文字宽度
  30. private $str_h = ''; //水印文字高度
  31. private $x = ''; //水印X坐标
  32. private $y = ''; //水印y坐标
  33. function __construct($img) { //析构函数
  34. $this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!');
  35. }
  36. private function imginfo() { //获取需要添加水印的图片的信息,并载入图片。
  37. $this->srcImg_info = getimagesize($this->srcImg);
  38. switch ($this->srcImg_info[2]) {
  39. case 3:
  40. $this->im = imagecreatefrompng($this->srcImg);
  41. break 1;
  42. case 2:
  43. $this->im = imagecreatefromjpeg($this->srcImg);
  44. break 1;
  45. case 1:
  46. $this->im = imagecreatefromgif($this->srcImg);
  47. break 1;
  48. default:
  49. die('原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
  50. }
  51. }
  52. private function waterimginfo() { //获取水印图片的信息,并载入图片。
  53. $this->waterImg_info = getimagesize($this->waterImg);
  54. switch ($this->waterImg_info[2]) {
  55. case 3:
  56. $this->water_im = imagecreatefrompng($this->waterImg);
  57. break 1;
  58. case 2:
  59. $this->water_im = imagecreatefromjpeg($this->waterImg);
  60. break 1;
  61. case 1:
  62. $this->water_im = imagecreatefromgif($this->waterImg);
  63. break 1;
  64. default:
  65. die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
  66. }
  67. }
  68. private function waterpos() { //水印位置算法
  69. switch ($this->pos) {
  70. case 0: //随机位置
  71. $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
  72. $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
  73. break 1;
  74. case 1: //上左
  75. $this->x = 0;
  76. $this->y = 0;
  77. break 1;
  78. case 2: //上中
  79. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  80. $this->y = 0;
  81. break 1;
  82. case 3: //上右
  83. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  84. $this->y = 0;
  85. break 1;
  86. case 4: //中左
  87. $this->x = 0;
  88. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  89. break 1;
  90. case 5: //中中
  91. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  92. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  93. break 1;
  94. case 6: //中右
  95. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  96. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  97. break 1;
  98. case 7: //下左
  99. $this->x = 0;
  100. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  101. break 1;
  102. case 8: //下中
  103. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  104. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  105. break 1;
  106. default: //下右
  107. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  108. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  109. break 1;
  110. }
  111. }
  112. private function waterimg() {
  113. if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $this->waterImg_info[1]){
  114. die('水印比原图大!');
  115. }
  116. $this->waterpos();
  117. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  118. imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
  119. $pct = $this->transparent;
  120. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  121. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);
  122. }
  123. private function waterstr() {
  124. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  125. $w = abs($rect[2]-$rect[6]);
  126. $h = abs($rect[3]-$rect[7]);
  127. $fontHeight = $this->fontSize;
  128. $this->water_im = imagecreatetruecolor($w, $h);
  129. imagealphablending($this->water_im,false);
  130. imagesavealpha($this->water_im,true);
  131. $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
  132. imagefill($this->water_im,0,0,$white_alpha);
  133. $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  134. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
  135. $this->waterImg_info = array(0=>$w,1=>$h);
  136. $this->waterimg();
  137. }
  138. function output() {
  139. $this->imginfo();
  140. if ($this->waterType == 0) {
  141. $this->waterstr();
  142. }else {
  143. $this->waterimginfo();
  144. $this->waterimg();
  145. }
  146. switch ($this->srcImg_info[2]) {
  147. case 3:
  148. imagepng($this->im,$this->srcImg);
  149. break 1;
  150. case 2:
  151. imagejpeg($this->im,$this->srcImg);
  152. break 1;
  153. case 1:
  154. imagegif($this->im,$this->srcImg);
  155. break 1;
  156. default:
  157. die('添加水印失败!');
  158. break;
  159. }
  160. imagedestroy($this->im);
  161. imagedestroy($this->water_im);
  162. }
  163. }
  164. ?>