將圖片添加浮水印的PHP類

WBOY
發布: 2016-07-25 08:42:33
原創
882 人瀏覽過
支援文字浮水印、圖片浮水印支援浮水印的位置隨機或固定(九宮格)浮水印透明度設定(圖片浮水印和文字浮水印都支援)文字浮水印的字體、顏色、大小設定圖片浮水印的背景透明
  1. /**
  2. * 加浮水印類,支援文字圖片浮水印的透明度設定、浮水印圖片背景透明。
  3. * 日期:2011-09-27
  4. * 作者:www.itwhy.org
  5. * 使用:
  6. * $obj = new WaterMask($imgFileName); //實例化物件
  7. * $obj->$waterType = 1; //類型:0為文字浮水印、1為圖片浮水印
  8. * $obj->$transparent = 45; //水印透明度
  9. * $obj->$waterStr = 'www.itwhy.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 = 'www.itwhy.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 $waterImg_info = ''; //水印圖片資訊
  30. private $str_w = ''; //浮水印文字寬度
  31. private $str_h = ''; //浮水印文字高度
  32. private $x = ''; //水印X座標
  33. private $y = '' ; //浮水印y座標
  34. function __construct($img) { //析構函數
  35. $this->srcImg = file_exists($img) ? $img : die('"'.$img .'" 原始檔不存在! ');
  36. }
  37. private function imginfo() { //取得需要加入浮水印的圖片的信息,並載入圖片。
  38. $this->srcImg_info = getimagesize($this->srcImg);
  39. switch ($this->srcImg_info[2]) {
  40. case 3:
  41. $this->im = imagecreate $this->srcImg);
  42. break 1;
  43. case 2:
  44. $this->im = imagecreatefromjpeg($this->srcImg);
  45. break 1;
  46. case 1:
  47. $this->im = imagecreatefromgif($this->srcImg);
  48. break 1;
  49. default:
  50. die('原文圖片('.$this->srcImg.')格式不對,只支援PNG、JPEG、GIF。
  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. 情況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. 規則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. 案例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. 中斷1;
  104. 預設值: //下右
  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->透明;
  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( $矩形[2]-$矩形[6]);
  124. $h = abs($矩形[3]-$矩形[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-00,00,000,000
  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. 函數輸出() {
  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. 情況3:
  146. imagepng($ this->im,$this->srcImg);
  147. 中斷1;
  148. 情況>情況2:
  149. imagejpeg($this->im,$this->srcImg);
  150. 規則1;
  151. 情況1:
  152. imagegif($this->im,$this->srcImg);
  153. break 1;
  154. 預設:
  155. die('新增浮水印失敗!' );
  156. 規則;
  157. }
  158. imagedestroy($this->im);
  159. imagedestroy(imagedestroy($this->im);
  160. imagedestroy(imagedestroy( $this->water_im);
  161. }
  162. }
?> ;
複製程式碼

PHP


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