ホームページ  >  記事  >  バックエンド開発  >  PHP 画像透かしクラスは透かしの透明性と背景の透明性をサポートします

PHP 画像透かしクラスは透かしの透明性と背景の透明性をサポートします

WBOY
WBOYオリジナル
2016-07-25 08:51:491369ブラウズ
  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) $obj->$fontFile = = 'AHGBold.ttf'; //フォントファイル
  12. * $obj->output(); //出力ウォーターマーク画像ファイルは入力画像ファイルを上書きします
  13. */
  14. class WaterMask{
  15. public $waterType = 1; //ウォーターマークのタイプ: 0 はテキストのウォーターマーク、1 は画像のウォーターマークです
  16. public $pos = 0; //ウォーターマークの位置
  17. public $transparent = 45; //ウォーターマークの透明度
  18. $waterStr = 'bbs.it-home.org' //ウォーターマークのテキスト
  19. //テキストのフォントSize
  20. public $fontColor = array(255,0,255); //ウォーターマークのテキストの色 (RGB)
  21. public $fontFile = 'AHGBold.ttf'; //フォント ファイル
  22. public $waterImg = 'logo.png';画像
  23. private $srcImg = ''; //透かしを入れる必要のある画像
  24. private $im = ''; //画像のハンドル
  25. private $water_im = '' //透かしを入れる画像のハンドル
  26. $srcImg_info = ''; / /画像情報
  27. private $waterImg_info = '' //ウォーターマーク画像情報
  28. private $str_w = ''; //ウォーターマーク文字幅
  29. private $x = ''; ; //ウォーターマーク $img) ? $img : die('"'.$img.'" ソースファイルが存在しません!')
  30. }
  31. private function imginfo() { //画像の情報を取得します透かしを入れて画像をロードする必要があります。
  32. $this->srcImg_info = getimagesize($this->srcImg);
  33. switch ($this->srcImg_info[2]) {
  34. ケース 3:
  35. $this->im = imagecreatefrompng($this-> ;srcImg);
  36. ブレイク 1;
  37. $this->im = imagecreatefromjpeg($this->srcImg);
  38. ブレイク 1:
  39. $this->im = imagecreatefromgif($this) ->srcImg);
  40. break 1;
  41. default:
  42. die('元の画像 ('.$this->srcImg.') の形式が正しくありません。サポートされているのは PNG、JPEG、および GIF のみです。') ;
  43. }
  44. }
  45. private function Waterimginfo() { //ウォーターマーク画像の情報を取得し、画像を読み込みます。
  46. $this->waterImg_info = getimagesize($this->waterImg);
  47. switch ($this->waterImg_info[2]) {
  48. ケース 3:
  49. $this->water_im = imagecreatefrompng($this-> ;waterImg);
  50. ブレイク 1;
  51. $this->water_im = imagecreatefromjpeg($this->waterImg);
  52. ブレイク 1:
  53. $this->water_im = imagecreatefromgif($this) ->waterImg);
  54. break 1;
  55. default:
  56. die('ウォーターマーク画像 ('.$this->srcImg.') は、PNG、JPEG、GIF のみをサポートしています。');
  57. }
  58. }
  59. private function Waterpos() { //水印位置算法
  60. switch ($this->pos) {
  61. case 0: //随机位置
  62. $this->x = rand(0,$this- >srcImg_info[0]-$this->waterImg_info[0]);
  63. $this->y = rand(0,$this->srcImg_info[1]-$this->waterImg_info[1]);
  64. 休憩1;
  65. ケース 1: //上左
  66. $this->x = 0;
  67. $this->y = 0;
  68. 休憩1;
  69. ケース 2: //上中
  70. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  71. $this->y = 0;
  72. 休憩1;
  73. ケース 3: //上右
  74. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  75. $this->y = 0;
  76. 休憩1;
  77. ケース 4: //中左
  78. $this->x = 0;
  79. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  80. 休憩1;
  81. ケース 5: //中中
  82. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  83. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  84. 休憩1;
  85. ケース 6: //中右
  86. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  87. $this->y = ($this->srcImg_info[1]-$this->waterImg_info[1])/2;
  88. 休憩1;
  89. ケース 7: //下左
  90. $this->x = 0;
  91. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  92. 休憩1;
  93. ケース 8: //下中
  94. $this->x = ($this->srcImg_info[0]-$this->waterImg_info[0])/2;
  95. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  96. 休憩1;
  97. デフォルト: //下右
  98. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  99. $this->y = $this->srcImg_info[1]-$this->waterImg_info[1];
  100. 休憩1;
  101. }
  102. }
  103. プライベート関数 Waterimg() {
  104. if ($this->srcImg_info[0] <= $this->waterImg_info[0] || $this->srcImg_info[1] <= $ this->waterImg_info[1]){
  105. die('水印比原图大!');
  106. }
  107. $this->waterpos();
  108. $cut = imagecreatetruecolor($this->waterImg_info[0],$this->waterImg_info[1]);
  109. imagecopy($cut,$this->im,0,0,$this->x,$this->y,$this->waterImg_info[0],$this->waterImg_info[1] );
  110. $pct = $this->透明;
  111. imagecopy($cut,$this->water_im,0,0,0,0,$this->waterImg_info[0],$this->waterImg_info[1]);
  112. imagecopymerge($this->im,$cut,$this->x,$this->y,0,0,$this->waterImg_info[0],$this->waterImg_info[1] ,$pct);
  113. }
  114. プライベート関数waterstr() {
  115. $rect = imagettfbbox($this->fontSize,0,$this->fontFile,$this->waterStr);
  116. $w = abs($rect[2]-$rect[6]);
  117. $h = abs($rect[3]-$rect[7]);
  118. $fontHeight = $this->fontSize;
  119. $this->water_im = imagecreatetruecolor($w, $h);
  120. imagealphablending($this->water_im,false);
  121. imagesavealpha($this->water_im,true);
  122. $white_alpha = imagecolorallocatealpha($this->water_im,255,255,255,127);
  123. imagefill($this->water_im,0,0,$white_alpha);
  124. $color = imagecolorallocate($this->water_im,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  125. imagettftext($this->water_im,$this->fontSize,0,0,$this->fontSize,$color,$this->fontFile,$this->waterStr);
  126. $this->waterImg_info = array(0=>$w,1=>$h);
  127. $this->waterimg();
  128. }
  129. 関数出力() {
  130. $this->imginfo();
  131. if ($this->waterType == 0) {
  132. $this->waterstr();
  133. }else {
  134. $this->waterimginfo();
  135. $this->waterimg();
  136. }
  137. switch ($this->srcImg_info[2]) {
  138. ケース 3:
  139. imagepng($this->im,$this->srcImg);
  140. 休憩1;
  141. ケース 2:
  142. imagejpeg($this->im,$this->srcImg);
  143. 休憩1;
  144. ケース 1:
  145. imagegif($this->im,$this->srcImg);
  146. 休憩1;
  147. デフォルト:
  148. die('添加水印失败!');
  149. 休憩;
  150. }
  151. imagedestroy($this->im);
  152. imagedestroy($this->water_im);
  153. }
  154. }
  155. ?>

复制代


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。