ホームページ  >  記事  >  バックエンド開発  >  JPGE と PNG 画像の融合

JPGE と PNG 画像の融合

WBOY
WBOYオリジナル
2016-07-25 09:08:56980ブラウズ

JPGE 画像と PNG 画像の融合は、実は誰もがよく知っている透かし技術です。以下のコードの最も重要な文は次のとおりです:

//画像のカラーブレンドモードを設定します
imagealphablending($ground_im, true);

  1. /*
  2. * 関数: PHP 画像透かし (透かしは画像またはテキストをサポートします)
  3. * パラメーター:
  4. * $groundImage 背景画像、つまり透かしを入れる必要がある画像は、現在のみサポートされていますGIF、JPG、PNG 形式;
  5. * $waterPos のウォーターマークの位置は 10 個あり、0 はランダムな位置です
  6. * 1 は左上、2 は中央上、3 は右上です。左中央、5 は中央中央、6 は中央が右であることを意味します
  7. * 7 は左下、8 は中央下、9 は右下です。 WaterImage 画像透かし、つまり透かしとして使用される画像は、現在 GIF、JPG、および PNG 形式のみをサポートします
  8. * $waterText テキスト透かし、つまりテキストは透かしとして使用され、ASCII コードをサポートしますが、中国語はサポートされません。 ;
  9. * $textFont テキスト サイズ、値は 1、2、3、4、または 5、デフォルトは 5;
  10. * $textColor テキストの色、値は 16 進数の色の値、デフォルトは #FF0000 (赤)。
  11. *
  12. * 注: GD 2.0 をサポート、FreeType、GIF Read、GIF Create、JPG、PNG をサポート
  13. * $waterImage と $waterText が最適です。同時に使用しないでください。どちらか 1 つを選択して $ を使用してくださいまずは水の画像。
  14. * $waterImageが有効な場合、パラメータ$waterString、$stringFont、および$stringColorは無効になります。
  15. ※ウォーターマーク入り画像のファイル名は$groundImageと同じです。
  16. * 著者:longware @ 2004-11-3 14:15:13
  17. */
  18. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$ textColor="#FF0000")
  19. //function imageWaterMark($groundImage,$waterPos=0,$waterImage,$waterText=,$textFont=5,$textColor=#FF0000″)
  20. {
  21. $isWaterImage = FALSE;
  22. $ formatMsg = "このファイル形式はまだサポートされていません。画像処理ソフトウェアを使用して、画像を GIF、JPG、または PNG 形式に変換してください。";
  23. //ウォーターマーク ファイルを読み取ります
  24. if(!empty($waterImage) && file_exists($waterImage))
  25. {
  26. $isWaterImage = TRUE;
  27. $water_info = getimagesize($waterImage);
  28. $water_w = $water_info[ 0 ];//ウォーターマーク画像の幅を取得
  29. $water_h = $water_info[1];//ウォーターマーク画像の高さを取得
  30. switch($water_info[2])//ウォーターマーク画像の形式を取得
  31. {
  32. ケース 1:$water_im = imagecreatefromgif($waterImage);break;
  33. ケース 2:$water_im = imagecreatefromjpeg($waterImage);break;
  34. case 3:$water_im = imagecreatefrompng($waterImage);break;
  35. デフォルト:die( $formatMsg);
  36. }
  37. }
  38. //チェンバジ?
  39. if(!empty($groundImage) && file_exists($groundImage))
  40. {
  41. $ground_info = getimagesize($groundImage);
  42. $ground_w = $ground_info[ 0]; //背景画像の幅を取得
  43. $ground_h = $ground_info[1];//背景画像の高さを取得
  44. switch($ground_info[2])//背景画像の形式を取得
  45. {
  46. case 1:$ground_im = imagecreatefromgif ($groundImage);break;
  47. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  48. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  49. デフォルト:die ($formatMsg);
  50. }
  51. }
  52. else
  53. {
  54. die("透かしを入れる必要がある画像が存在しません!");
  55. }
  56. //透かしの位置
  57. if($isWaterImage)//画像の透かし
  58. {
  59. $w = $water_w;
  60. $h = $water_h;
  61. $label = "画像";
  62. }
  63. else//テキストの透かし
  64. {
  65. $temp = imagettfbbox(ceil($textFont*5),0,". /cour.ttf",$waterText);/ /TrueType フォントを使用してテキストの範囲を取得します
  66. $w = $temp[2] - $temp[6];
  67. $h = $temp[3] - $temp[7 ];
  68. unset($temp);
  69. $ label = "テキストエリア";
  70. }
  71. if( ($ground_w<$w) || ($ground_h<$h) )
  72. {
  73. echo "テキストエリアの長さまたは幅透かしを入れる必要がある画像は透かしより大きいです".$label. "透かしを生成するには若すぎます! ";
  74. return;
  75. }
  76. switch($waterPos)
  77. {
  78. case 0://random
  79. $posX = rand(0,($ground_w - $w));
  80. $posY = rand(0,($ground_h) - $h));
  81. ブレーク;
  82. ケース 1://1 は左上
  83. $posX = 0;
  84. $posY = 0;
  85. ブレーク;
  86. ケース 2://2 は中央上
  87. $posX = ($ ground_w - $w) / 2;
  88. $posY = 0;
  89. ブレーク;
  90. ケース 3://3 は右上です
  91. $posX = $ground_w - $w;
  92. $posY = 0;
  93. ブレーク;
  94. ケース 4 : //4 は中心が左側にあることを意味します
  95. $posX = 0;
  96. $posY = ($ground_h - $h) / 2;
  97. Break;
  98. case 5://5 は中心上の中心です
  99. $posX = ($ground_w - $w ) / 2;
  100. $posY = ($ground_h - $h) / 2;
  101. Break;
  102. case 6://6 は中央右です
  103. $posX = $ground_w - $w;
  104. $ posY = ($ground_h - $h) / 2;
  105. Break;
  106. case 7://7 は、下が左側にあることを意味します
  107. $posX = 0;
  108. $posY = $ground_h - $h;
  109. Break;
  110. case 8://8 は底が中央にあることを意味します
  111. $posX = ($ground_w - $w) / 2;
  112. $posY = $ground_h - $h;
  113. Break;
  114. case 9://9 は右下です
  115. $posX = $ground_w - $w;
  116. $posY = $ground_h - $h;
  117. ブレーク;
  118. デフォルト://ランダム
  119. $posX = rand(0,($ground_w - $w));
  120. $posY = rand (0,($ground_h - $h) );
  121. break;
  122. }
  123. //画像の色混合モードを設定します
  124. imagealphablending($ground_im, true);
  125. if($isWaterImage)//画像ウォーターマーク
  126. {
  127. imagecopy($ground_im, $water_im, $posX, $ posY, 0, 0, $water_w,$water_h);//ウォーターマークを対象ファイルにコピーします
  128. }
  129. else//テキストウォーターマーク
  130. {
  131. if( !empty( $textColor) && (strlen($textColor)==7) )
  132. {
  133. $R = hexdec(substr($textColor,1,2));
  134. $G = hexdec(substr($textColor,3,2)) ;
  135. $B = hexdec(substr($textColor,5) );
  136. }
  137. else
  138. {
  139. die("ウォーターマークのテキストの色の形式が正しくありません。 ");
  140. }
  141. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  142. }
  143. //ウォーターマーク生成後の画像
  144. @unlink($groundImage);
  145. switch($ground_info[2])//背景画像の形式を取得します
  146. {
  147. case 1:imagegif($ground_im,$groundImage);break;
  148. case 2:imagejpeg($ ground_im,$ groundImage);break;
  149. case 3:imagepng($ground_im,$groundImage);break;
  150. default:die($errorMsg);
  151. }
  152. //メモリを解放
  153. if(isset($water_info)) unset ($water_info );
  154. if(isset($water_im)) imagedestroy($water_im);
  155. unset($ground_info);
  156. imagedestroy($ground_im);
  157. }
  158. $uploadfile="your_picture.jpg"; //背景画像処理対象 Position
  159. $waterImage="Hat.png"; //透明画像の位置
  160. imageWaterMark($uploadfile,9,$waterImage);
  161. //ウェブ上に表示
  162. echo "";
  163. ?>
コードをコピー


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