首頁 > 後端開發 > php教程 > php圖片水印類別程式碼,php圖片添加文字浮水印與圖片浮水印

php圖片水印類別程式碼,php圖片添加文字浮水印與圖片浮水印

WBOY
發布: 2016-07-25 08:51:55
原創
1033 人瀏覽過
  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 = imagecreate $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. $this->waterImg_info = getimagesize($this->waterImg);
  49. switch ($this->waterImg_info[2]) {
  50. case 3:
  51. $this->water_im = imagecreatefrompng( $this->waterImg);
  52. break 1;
  53. case 2:
  54. $this->water_im = imagecreatefromjpeg($this->waterImg);
  55. break 1;
  56. case 1:
  57. $this->water_im = imagecreatefromgif($this->waterImg);
  58. break 1;
  59. default:
  60. die('水印圖片('.$this->srcImg.')格式不對,只支援PNG、JPEG、GIF。');
  61. }
  62. }
  63. private function waterpos() { //水印位置演算法
  64. switch ($this->pos) {
  65. case 0: //隨機位置
  66. $this ->x = rand(0,$this->srcImg_info[0]-$this->waterImg_info[0]);
  67. $this->y = rand(0,$this->srcImg_info[1] -$this->waterImg_info[1]);
  68. 休息1;
  69. 情況1: //上左
  70. $this->x = 0;
  71. $this->y = 0;
  72. 休息1;
  73. 情況2: //上中
  74. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  75. $ this->y = 0;
  76. 休息1;
  77. 情況3: //上右
  78. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  79. $this->y = 0;
  80. 休息1;
  81. 情況4: //中左
  82. $this->x = 0;
  83. $this->y = ($this ->srcImg_info[1]-$this->waterImg_info[1])/2;
  84. 休息1;
  85. case 5: //中中
  86. $this->x = ($this->srcImg_info [0]-$this->waterImg_info[0])/2;
  87. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  88. 休息1;
  89. 情況6: //中右
  90. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  91. $this->y = ( $this->srcImg_info[1]-$this->waterImg_info[1])/2;
  92. 休息1;
  93. 情況7: //下左
  94. $this->x = 0;
  95. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  96. 休息1;
  97. case 8: //下方
  98. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  99. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  100. 休息1;
  101. default: //下右
  102. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  103. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  104. 休息1;
  105. }
  106. }
  107. 私有函數waterimg() {
  108. if ($this-> srcImg_info[0] waterImg_info[0] || $this->; srcImg_info[1] waterImg_info[1]){
  109. die('水印比原圖大! ');
  110. }
  111. $this->waterpos();
  112. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  113. imagecopy( $cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[ 1]);
  114. $pct = $this->透明;
  115. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  116. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[ 1],$pct);
  117. }
  118. 私人函數waterstr() {
  119. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  120. $w = abs($ rect[2]-$rect[6]);
  121. $h = abs($rect[3]-$rect[7]);
  122. $fontHeight = $this->fontSize;
  123. $this ->water_im = imagecreatetruecolor($w, $h);
  124. imagealphablending($this->water_im,false);
  125. imagesavealpha($this->water_im,true);
  126. $white_alpha = imagecolor this->water_im,255,255,255,127);
  127. imagefill($this->water_im,0,0,$white_alpha);
  128. $color = imageColallocate($this->water_im,$this->fontor[0],$this] $this->fontColor[1],$this->fontColor[2]);
  129. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$ this->fontFile,$this->waterStr);
  130. $this->waterImg_info = array(0=>$w,1=>$h);
  131. $this->waterimg();
  132. }
  133. 函數輸出() {
  134. $this->imginfo();
  135. if ($this->waterType == 0) {
  136. $this->waterstr();
  137. } else {
  138. $this->waterimginfo();
  139. $this->waterimg();
  140. }
  141. switch ($this->srcImg_info[2]) {
  142. 情況3:
  143. imagepng($this->im,$this->srcImg);
  144. 休息1;
  145. 情況2:
  146. imagejpeg($this->im,$this->srcImg);
  147. 休息1;
  148. 情況1:
  149. imagegif($this->im,$this->srcImg);
  150. 休息1;
  151. default:
  152. die('添加浮水印失敗! ');
  153. 休息;
  154. }
  155. imagedestroy($this->im);
  156. imagedestroy($this->water_im);
  157. }
  158. }
  159. ?>
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板