Heim > Backend-Entwicklung > PHP-Tutorial > php图片水印类代码,php图片添加文字水印与图片水印

php图片水印类代码,php图片添加文字水印与图片水印

WBOY
Freigeben: 2016-07-25 08:51:55
Original
1011 Leute haben es durchsucht
  1. /**

  2. * 加水印类,支持文字图片水印的透明度设置、水印图片背景透明。
  3. * 使用: (php文字水印效果)
  4. * $obj = new WaterMask($imgFileName); //实例化对象
  5. * $obj->$waterType = 1; //类型:0为文字水印、1为图片水印
  6. * $obj->$transparent = 45; //水印透明度
  7. * $obj->$waterStr = 'bbs.it-home.org'; //水印文字
  8. * $obj->$fontSize = 16; //文字字体大小
  9. * $obj->$fontColor = array(255,0255); //水印文字颜色(RGB)
  10. * $obj->$fontFile = = 'AHGBold.ttf'; //字体文件
  11. * $obj->output(); //输出水印图片文件覆盖到输入的图片文件
  12. */
  13. class WaterMask{
  14. public $waterType = 1; //水印类型:0为文字水印、1为图片水印
  15. public $pos = 0; //水印位置
  16. public $transparent = 45; //水印透明度
  17. public $waterStr = 'bbs.it-home.org'; //水印文字
  18. public $fontSize = 16; //文字字体大小
  19. public $fontColor = array(255,0,255); //水印文字颜色(RGB)
  20. public $fontFile = 'AHGBold.ttf'; //字体文件
  21. public $waterImg = 'logo.png'; //水印图片
  22. private $srcImg = ''; //需要添加水印的图片
  23. private $im = ''; //图片句柄
  24. private $water_im = ''; //水印图片句柄
  25. private $srcImg_info = ''; //图片信息
  26. private $waterImg_info = ''; //水印图片信息
  27. private $str_w = ''; //水印文字宽度
  28. private $str_h = ''; //水印文字高度
  29. private $x = ''; //水印X坐标
  30. private $y = ''; //水印y坐标
  31. function __construct($img) { //析构函数

  32. $this->srcImg = file_exists($img) ? $img : die('"'.$img.'" 源文件不存在!');
  33. }
  34. private function imginfo() { //获取需要添加水印的图片的信息,并载入图片。
  35. $this->srcImg_info = getimagesize($this->srcImg);
  36. switch ($this->srcImg_info[2]) {
  37. case 3:
  38. $this->im = imagecreatefrompng($this->srcImg);
  39. break 1;
  40. case 2:
  41. $this->im = imagecreatefromjpeg($this->srcImg);
  42. break 1;
  43. case 1:
  44. $this->im = imagecreatefromgif($this->srcImg);
  45. break 1;
  46. default:
  47. die('原图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
  48. }
  49. }
  50. private function waterimginfo() { //获取水印图片的信息,并载入图片。
  51. $this->waterImg_info = getimagesize($this->waterImg);
  52. switch ($this->waterImg_info[2]) {
  53. case 3:
  54. $this->water_im = imagecreatefrompng($this->waterImg);
  55. break 1;
  56. case 2:
  57. $this->water_im = imagecreatefromjpeg($this->waterImg);
  58. break 1;
  59. case 1:
  60. $this->water_im = imagecreatefromgif($this->waterImg);
  61. break 1;
  62. default:
  63. die('水印图片('.$this->srcImg.')格式不对,只支持PNG、JPEG、GIF。');
  64. }
  65. }
  66. private function waterpos() { //水印位置算法
  67. switch ($this->pos) {
  68. case 0: //随机位置
  69. $this->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
  70. $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
  71. break 1;
  72. case 1: //上左
  73. $this->x = 0;
  74. $this->y = 0;
  75. break 1;
  76. case 2: //上中
  77. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  78. $this->y = 0;
  79. break 1;
  80. case 3: //上右
  81. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  82. $this->y = 0;
  83. break 1;
  84. case 4: //中左
  85. $this->x = 0;
  86. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  87. break 1;
  88. case 5: //中中
  89. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  90. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  91. break 1;
  92. case 6: //中右
  93. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  94. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  95. break 1;
  96. case 7: //下左
  97. $this->x = 0;
  98. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  99. break 1;
  100. case 8: //下中
  101. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  102. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  103. break 1;
  104. default: //下右
  105. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  106. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  107. break 1;
  108. }
  109. }
  110. private function waterimg() {
  111. if ($this->srcImg_info[0] waterImg_info[0] || $this->srcImg_info[1] waterImg_info[1]){
  112. die('水印比原图大!');
  113. }
  114. $this->waterpos();
  115. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  116. imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1]);
  117. $pct = $this->transparent;
  118. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  119. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1],$pct);
  120. }
  121. private function waterstr() {
  122. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  123. $w = abs($rect[2]-$rect[6]);
  124. $h = abs($rect[3]-$rect[7]);
  125. $fontHeight = $this->fontSize;
  126. $this->water_im = imagecreatetruecolor($w, $h);
  127. imagealphablending($this->water_im,false);
  128. imagesavealpha($this->water_im,true);
  129. $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
  130. imagefill($this->water_im,0,0,$white_alpha);
  131. $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  132. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
  133. $this->waterImg_info = array(0=>$w,1=>$h);
  134. $this->waterimg();
  135. }
  136. function output() {
  137. $this->imginfo();
  138. if ($this->waterType == 0) {
  139. $this->waterstr();
  140. }else {
  141. $this->waterimginfo();
  142. $this->waterimg();
  143. }
  144. switch ($this->srcImg_info[2]) {
  145. case 3:
  146. imagepng($this->im,$this->srcImg);
  147. break 1;
  148. case 2:
  149. imagejpeg($this->im,$this->srcImg);
  150. break 1;
  151. case 1:
  152. imagegif($this->im,$this->srcImg);
  153. break 1;
  154. default:
  155. die('添加水印失败!');
  156. break;
  157. }
  158. imagedestroy($this->im);
  159. imagedestroy($this->water_im);
  160. }
  161. }
  162. ?>
复制代码


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