画像に透かしを追加する PHP クラス

WBOY
リリース: 2016-07-25 08:42:33
オリジナル
848 人が閲覧しました
テキスト透かしと画像透かしをサポートします。透かしのランダムまたは固定位置の設定をサポートします(画像とテキストの透かしの両方がサポートされます)。画像の背景のフォント、色、サイズを設定します。ウォーターマークは透明です
  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; $fontColor = array(255,0,255); // ウォーターマークのテキストの色 (RGB)
  21. public $fontFile = 'AHGBold.ttf'; // フォント ファイル
  22. // ウォーターマークの画像
  23. private $srcImg = ''; //透かし入りの画像を追加する必要があります
  24. private $im = '' //画像のハンドル
  25. private $water_im = '';画像情報
  26. private $waterImg_info = ''; // ウォーターマーク画像情報
  27. private $str_w = '' //ウォーターマーク文字幅
  28. private $x = ''; /ウォーターマーク X 座標
  29. private $y = ''; //ウォーターマーク y 座標
  30. function __construct($img) { //デストラクター
  31. $this->srcImg = file_exists($img) ? '.$img.'" ソース ファイルが存在しません。 ');
  32. }
  33. private function imginfo() { //透かしを入れる必要がある画像に関する情報を取得し、画像をロードします。
  34. $this->srcImg_info = getimagesize($this->srcImg);
  35. switch ($this->srcImg_info[2]) {
  36. case 3:
  37. $this->im = imagecreatefrompng($this-> ;srcImg);
  38. ブレーク 1;
  39. ケース 2:
  40. $this->im = imagecreatefromjpeg($this->srcImg);
  41. ブレーク 1;
  42. ケース 1:
  43. $this->im = imagecreatefromgif($this ->srcImg);
  44. break 1;
  45. default:
  46. die('元の画像 ('.$this->srcImg.') の形式が間違っています。PNG、JPEG、GIF のみをサポートします。');
  47. }
  48. }
  49. private function Waterimginfo() { //ウォーターマーク画像の情報を取得し、画像を読み込みます。
  50. $this->waterImg_info = getimagesize($this->waterImg);
  51. switch ($this->waterImg_info[2]) {
  52. ケース 3:
  53. $this->water_im = imagecreatefrompng($this-> ;waterImg);
  54. ブレーク 1;
  55. ケース 2:
  56. $this->water_im = imagecreatefromjpeg($this->waterImg);
  57. ブレーク 1;
  58. ケース 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. case 3: //上右
  80. $this->x = $this->srcImg_info[0]-$this->waterImg_info[0];
  81. $this->y = 0;
  82. ブレーク 1;
  83. case 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. case 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. ケース 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. デフォルト: //下右
  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] <= $this->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 ->transparent;
  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 = imagecolorallocatealpha($this->water_im,255,255,255,127);
  129. imagefill($this->water_im,0,0,$white_alpha);
  130. $color = imagecolorallocate($this->water_im,$this->fontColor[0],$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. function Output() {
  136. $this->imginfo();
  137. if ($this->waterType == 0) {
  138. $this->waterstr();
  139. }else {
  140. $this->gt;waterimginfo();
  141. $ this->waterimg();
  142. }
  143. switch ($this->srcImg_info[2]) {
  144. case 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. デフォルト:
  154. die('追加水印失败!');
  155. Break;
  156. }
  157. imagedestroy($this->im);
  158. imagedestroy($this->water_im);
  159. }
  160. }
  161. ?> ;
复制代

PHP


関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!