PHP画像透かし関数コード(画像とテキストをサポート)

WBOY
リリース: 2016-07-25 08:51:56
オリジナル
1015 人が閲覧しました
  1. /*

  2. * 関数: php 画像透かし (透かしは画像またはテキストをサポートします)
  3. * パラメーター:
  4. * $groundImage 背景画像、つまり、必要な画像一時的に透かしを入れます。GIF、JPG、PNG 形式のみをサポートします。
  5. * $waterPos 透かしの位置は 10 個あり、0 はランダムな位置です。
  6. *1 は左上、2 は中央上、3 は右上を意味します。 4 は左中央、5 は中央中央、6 は右中央を意味します
  7. *7 は左下を意味します、8 は中央下を意味します、9 は右下を意味します
  8. * $waterImage 画像の透かし、つまり現在透かしとして使用されている画像です。 GIF、JPG、PNG 形式のみをサポートします。
  9. * $waterText テキスト透かし、つまりテキストが透かしとして使用されます。ASCII コードをサポートしますが、中国語はサポートされません。
  10. * $textFont テキスト サイズ、値は 1、2、3 です。 、4 または 5、デフォルトは 5 です。
  11. * $textColor テキストの色、値は 16 進数の色の値、デフォルトは #FF0000 (赤) です。
  12. * http://bbs.it-home.org
  13. * : GD 2.0 をサポート、FreeType、GIF Read、GIF Create、JPG、PNG をサポート
  14. * $waterImage と $waterText を同時に使用しないことをお勧めします。どちらかを選択し、最初に $waterImage を使用します。
  15. * $waterImage が有効な場合、パラメータ $waterString、$stringFont、および $stringColor は無効になります。
  16. * 透かし入り画像のファイル名は$groundImageと同じです。
  17. * 著者:longware @ 2004-11-3 14:15:13
  18. */
  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. case 2:$water_im = imagecreatefromjpeg($waterImage);break;
  34. case 3:$water_im = imagecreatefrompng($waterImage);break; $formatMsg);
  35. }
  36. }
  37. //背景画像を読み込みます
  38. if(!empty($groundImage) && file_exists($groundImage))
  39. {
  40. $ground_info = getimagesize($groundImage);
  41. $ground_w= $ground_info[ 0];// 背景画像の幅を取得
  42. $ground_h= $ground_info[1];// 背景画像の高さを取得
  43. switch($ground_info[2])// 背景画像の形式を取得
  44. {
  45. case 1:$ground_im = imagecreatefromgif($ groundImage);break;
  46. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  47. case 3:$ground_im = imagecreatefrompng($groundImage);break; ($formatMsg);
  48. }
  49. }
  50. else
  51. {
  52. die("透かしを入れる必要がある画像が存在しません!")
  53. }
  54. //透かしの位置
  55. if($isWaterImage)//画像の透かし
  56. {
  57. $w = $water_w;
  58. $h = $water_h;
  59. $label = "画像"
  60. }
  61. else//テキスト透かし
  62. {
  63. $temp = imagettfbbox(ceil($textFont*2.5),0," /cour.ttf",$waterText);//TrueType フォントを使用してテキストの範囲を取得
  64. $w = $temp[2] - $temp[6];
  65. $h = $temp[3] - $temp[7] ;
  66. unset($temp);
  67. $label = "テキストエリア";
  68. if( ($ground_w<$w) || ($ground_h<$h) )
  69. {
  70. echo "ウォーターマークを付ける必要がある画像はウォーターマーク「.$label」より長いです。小さいため、ウォーターマークを生成できません。 ";
  71. return;
  72. }
  73. switch($waterPos)
  74. {
  75. case 0://random
  76. $posX = rand(0,($ground_w - $w));
  77. $posY = rand(0,($ground_h) - $h));
  78. break;
  79. case 1://1 は左上を意味します
  80. $posY = 0;
  81. case 2://2 は上中央を意味します
  82. $posX = ($
  83. $posY = 0;
  84. $posY = 0;
  85. $posY = 0; ://4 は中央左を意味します
  86. $posX = 0;
  87. $posY = ($ground_h - $h) / 2;
  88. case 5://5 は中央中央を意味します
  89. $posX = ($ground_w - $w) ) / 2;
  90. $posY = ($ground_h - $h) / 2;
  91. case 6://6 は中央の右です
  92. $posX = $ground_w - $w; $h) / 2;
  93. case 7://7 は左下
  94. $posY = $ground_h - $h;
  95. case 8://8 は下中央
  96. $ posX = ($ground_w - $w) / 2;
  97. $posY = $ground_h - $h;
  98. case 9://9 は右下です
  99. $posX = $ground_w - $w; ground_h - $h;
  100. default://random
  101. $posX = rand(0,($ground_h - $h) ; posY, 0 , 0, $water_w,$water_h);//ウォーターマークを対象ファイルにコピー
  102. }
  103. else//テキストウォーターマーク
  104. {
  105. if( !empty($textColor) && (strlen($textColor)==7) )
  106. {
  107. $R = hexdec(substr($textColor,1,2));
  108. $G = hexdec(substr($textColor,3,2)); }
  109. else
  110. {
  111. die("ウォーターマークのテキストの色の形式が正しくありません。");
  112. }
  113. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  114. }
  115. //ウォーターマーク生成後の画像
  116. @unlink($groundImage);
  117. switch($ground_info[2])// 背景画像の形式を取得します
  118. {
  119. case 1:imagegif($ground_im,$groundImage);break;
  120. case 2:imagejpeg($) ground_im,$ groundImage);break;
  121. case 3:imagepng($ground_im,$groundImage);break;
  122. default:die($errorMsg);
  123. //メモリを解放する
  124. if(isset($water_info)) $water_info) ;
  125. if(isset($water_im)) imagedestroy($ground_info);
  126. }

  127. //--- ---

  128. if(isset($_FILES) && !empty($_FILES['userfile']) && $_FILES['userfile']['size']>0)
  129. {
  130. $uploadfile = "./" .time( )."_".$_FILES['userfile']['name'];
  131. if (copy($_FILES['userfile']['tmp_name'], $uploadfile))
  132. {
  133. echo "OK< ;br> ";
  134. //テキストのウォーターマーク
  135. imageWaterMark($uploadfile,0,"","あなたのウォーターテキスト",9,"#EBEBEB");
  136. //画像のウォーターマーク
  137. //$waterImage="./images /watermark.gif";//ウォーターマーク画像のパス
  138. //imageWaterMark($uploadfile,9,$waterImage);
  139. echo "";
  140. }
  141. else
  142. {
  143. echo "Fail
    "
  144. }
  145. ?>
  146. ="userfile " type="file">
  147. コードをコピーします
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!