首頁  >  文章  >  後端開發  >  php圖片浮水印類別支援浮水印透明與背景透明

php圖片浮水印類別支援浮水印透明與背景透明

WBOY
WBOY原創
2016-07-25 08:51:491370瀏覽
  1. /**

  2. * 加浮水印類,支援文字圖片浮水印的透明度設定、浮水印圖片背景透明。
  3. * 日期:2011-09-27
  4. * 作者:bbs.it-home.org
  5. * 使用:
  6. * $obj = new WaterMask($imgFileName); //實例化物件
  7. * $obj->$waterType = 1; //類型:0為文字浮水印、1為圖片浮水印
  8. * $obj->$transparent = 45; //水印透明度
  9. * $obj-> $waterStr = 'bbs.it-home.org'; //水印文字
  10. * $obj->$fontSize = 16; //文字字體大小
  11. * $obj->$fontColor = array(255, 0255); //浮水印文字顏色(RGB)
  12. * $obj->$fontFile = = 'AHGBold.ttf'; //字型檔
  13. * $obj->output(); //輸出浮水印圖片文件覆蓋到輸入的圖片檔
  14. */
  15. class WaterMask{
  16. public $waterType = 1; //浮水印類型:0為文字浮水印、1為圖片浮水印
  17. public $pos = 0; //浮水印位置
  18. public $transparent = 45; //浮水印透明度
  19. public $waterStr = 'bbs. it-home.org'; //浮水印文字
  20. public $fontSize = 16; //文字字體大小
  21. public $fontColor = array(255,0,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 = imagecreate $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. $this->waterImg_info = getimagesize($this->waterImg);
  51. switch ($this->waterImg_info[2]) {
  52. case 3:
  53. $this->water_im = imagecreatefrompng( $this->waterImg);
  54. break 1;
  55. case 2:
  56. $this->water_im = imagecreatefromjpeg($this->waterImg);
  57. break 1;
  58. case 1:
  59. $this->water_im = imagecreatefromgif($this->waterImg);
  60. break 1;
  61. default:
  62. die('水印圖片('.$this->srcImg.')格式不對,只支援PNG、JPEG、GIF。');
  63. }
  64. }
  65. private function waterpos() { //水印位置演算法
  66. switch ($this->pos) {
  67. case 0: //隨機位置
  68. $this ->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
  69. $this->y = rand(0,$this->srcImg_info[1] -$this->waterImg_info[1]);
  70. 休息1;
  71. 情況1: //上左
  72. $this->x = 0;
  73. $this->y = 0;
  74. 休息1;
  75. 情況2: //上中
  76. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  77. $ this->y = 0;
  78. 休息1;
  79. 情況3: //上右
  80. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  81. $this->y = 0;
  82. 休息1;
  83. 情況4: //中左
  84. $this->x = 0;
  85. $this->y = ($this ->srcImg_info[1]-$this->waterImg_info[1])/2;
  86. 休息1;
  87. case 5: //中中
  88. $this->x = ($this->srcImg_info [0]-$this->waterImg_info[0])/2;
  89. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  90. 休息1;
  91. 情況6: //中右
  92. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  93. $this->y = ( $this->srcImg_info[1]-$this->waterImg_info[1])/2;
  94. 休息1;
  95. 情況7: //下左
  96. $this->x = 0;
  97. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  98. 休息1;
  99. case 8: //下方
  100. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  101. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  102. 休息1;
  103. default: //下右
  104. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  105. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  106. 休息1;
  107. }
  108. }
  109. 私有函數waterimg() {
  110. if ($this-> srcImg_info[0] waterImg_info[0] || $this->; srcImg_info[1] waterImg_info[1]){
  111. die('水印比原圖大! ');
  112. }
  113. $this->waterpos();
  114. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  115. imagecopy( $cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[ 1]);
  116. $pct = $this->透明;
  117. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  118. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[ 1],$pct);
  119. }
  120. 私人函數waterstr() {
  121. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  122. $w = abs($ rect[2]-$rect[6]);
  123. $h = abs($rect[3]-$rect[7]);
  124. $fontHeight = $this->fontSize;
  125. $this ->water_im = imagecreatetruecolor($w, $h);
  126. imagealphablending($this->water_im,false);
  127. imagesavealpha($this->water_im,true);
  128. $white_alpha = imagecolor this->water_im,255,255,255,127);
  129. imagefill($this->water_im,0,0,$white_alpha);
  130. $color = imageColallocate($this->water_im,$this->fontor[0],$this] $this->fontColor[1],$this->fontColor[2]);
  131. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$ this->fontFile,$this->waterStr);
  132. $this->waterImg_info = array(0=>$w,1=>$h);
  133. $this->waterimg();
  134. }
  135. 函數輸出() {
  136. $this->imginfo();
  137. if ($this->waterType == 0) {
  138. $this->waterstr();
  139. } else {
  140. $this->waterimginfo();
  141. $this->waterimg();
  142. }
  143. switch ($this->srcImg_info[2]) {
  144. 情況3:
  145. imagepng($this->im,$this->srcImg);
  146. 休息1;
  147. 情況2:
  148. imagejpeg($this->im,$this->srcImg);
  149. 休息1;
  150. 情況1:
  151. imagegif($this->im,$this->srcImg);
  152. 休息1;
  153. default:
  154. die('添加浮水印失敗! ');
  155. 休息;
  156. }
  157. imagedestroy($this->im);
  158. imagedestroy($this->water_im);
  159. }
  160. }
  161. ?>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn