ホームページ  >  記事  >  バックエンド開発  >  複数の画像アップロードに透かしを追加する PHP コード

複数の画像アップロードに透かしを追加する PHP コード

WBOY
WBOYオリジナル
2016-07-25 08:51:49911ブラウズ
  1. //phpウォーターマーク関数
  2. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000 ")
  3. {
  4. $isWaterImage = FALSE;
  5. $formatMsg = "このファイル形式はまだサポートされていません。画像を GIF、JPG、または PNG 形式に変換するには、画像処理ソフトウェアを使用してください。";
  6. //ウォーターマーク ファイルを読み取ります
  7. if(!empty($waterImage) && file_exists($waterImage))
  8. {
  9. $isWaterImage = TRUE;
  10. $water_info = getimagesize($waterImage); //結果は配列です
  11. $water_w = $water_info[0];//ウォーターマーク画像の幅を取得する
  12. $water_h = $water_info[1];//ウォーターマーク画像の高さを取得する
  13. switch($water_info[2])//Getウォーターマーク画像の形式
  14. {
  15. case 1:$water_im = imagecreatefromgif($waterImage);break; //画像を PHP で認識できるエンコード処理に変換します
  16. case 2:$water_im = imagecreatefromjpeg($waterImage) ;break; //画像を PHP が認識できるエンコード プロセスに変換します
  17. case 3:$water_im = imagecreatefrompng($waterImage);break; //画像を PHP が認識できるエンコード プロセスに変換します
  18. }
  19. }
  20. //読み取り 背景画像を取得します
  21. if(!empty($groundImage) && file_exists($groundImage))
  22. {
  23. $ground_info = getimagesize($groundImage)
  24. $ground_w = $ground_info[0];/ /背景画像の幅を取得
  25. $ ground_h = $ground_info[1] //背景画像の高さを取得
  26. switch($ground_info[2]) //背景画像の形式を取得
  27. {
  28. case 1 :$ground_im = imagecreatefromgif($groundImage);break;
  29. ケース 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  30. default:die($formatMsg) ;
  31. }
  32. }
  33. else
  34. {
  35. die("必須 透かしのある画像は存在しません! ");
  36. }
  37. //ウォーターマークの位置
  38. if($isWaterImage)//画像のウォーターマーク
  39. {
  40. $w = $water_w;
  41. $h = $water_h;
  42. $label = "画像";
  43. }
  44. else/ /テキストのウォーターマーク
  45. {
  46. $temp = imagettfbbox(ceil($textFont*2.5),0,"c:/windows/fonts/stcaiyun.ttf",$waterText);//TrueType フォントを使用してテキストの範囲を取得します
  47. $ w = $temp[2] - $temp[6];
  48. $h = $temp[3] - $temp[7];
  49. $label = "テキストエリア"; ($ground_w<$w) || ($ground_h<$h) )
  50. {
  51. echo "ウォーターマークを入れる画像の長さまたは幅がウォーターマーク「.$label.」より小さいため、ウォーターマークを生成できません! ";
  52. return;
  53. }
  54. switch($waterPos)
  55. {
  56. case 0://random
  57. $posX = rand(0,($ground_w - $w));
  58. $posY = rand(0,($ground_h) - $h));
  59. break;
  60. case 1://1 は左上を意味します
  61. $posY = 0;
  62. case 2://2 は上中央を意味します
  63. $posX = ($
  64. $posY = 0;
  65. $posY = 0;
  66. $posY = 0; ://4 は中央左を意味します
  67. $posX = 0;
  68. $posY = ($ground_h - $h) / 2;
  69. case 5://5 は中央中央を意味します
  70. $posX = ($ground_w - $w) ) / 2;
  71. $posY = ($ground_h - $h) / 2;
  72. case 6://6 は中央の右です
  73. $posX = $ground_w - $w; $h) / 2;
  74. case 7://7 は左下
  75. $posY = $ground_h - $h;
  76. case 8://8 は下中央
  77. $ posX = ($ground_w - $w) / 2;
  78. $posY = $ground_h - $h;
  79. case 9://9 は右下です
  80. $posX = $ground_w - $w; ground_h - $h;
  81. default://random
  82. $posX = rand(0,($ground_h - $h) ; posY, 0 , 0, $water_w,$water_h);//ウォーターマークを対象ファイルにコピー
  83. }
  84. else//テキストウォーターマーク
  85. {
  86. if( !empty($textColor) && (strlen($textColor)==7) )
  87. {
  88. $R = hexdec(substr($textColor,1,2));
  89. $G = hexdec(substr($textColor,3,2)); }
  90. else
  91. {
  92. die("ウォーターマークのテキストの色の形式が正しくありません。");
  93. }
  94. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  95. }
  96. //ウォーターマーク生成後の画像
  97. @unlink($groundImage);
  98. switch($ground_info[2])// 背景画像の形式を取得します
  99. {
  100. case 1:imagegif($ground_im,$groundImage); // gif で画像を作成します。 format
  101. case 2 :imagejpeg($ground_im,$groundImage);break; //jpeg 形式で画像を作成します
  102. case 3:imagepng($ground_im,$groundImage);break; //png 形式で画像を作成します
  103. デフォルト: die($errorMsg);
  104. }
  105. //メモリを解放します
  106. if(isset($water_info)) unset($water_info); imagedestroy($water_im);
  107. imagedestroy($ground_im) ;
  108. }
  109. ?>
  110. php 画像アップロード コード:
  111. for ($_FILES['userfile']['tmp_name'] );$i++)
  112. {
  113. $upfile="./img/".($i+1).".png";//ここのパスを自分のパスに変更します
  114. if(move_uploaded_file($_FILES['userfile'] ['tmp_name'] [$i],$upfile)){
  115. imageWaterMark($upfile,9,"./shuiyin.png","Made By Chenduan",5,"#FF0000");機能: 画像ウォーターマーク (ウォーターマークは画像またはテキストをサポートします)
  116. * imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")
  117. *パラメータ:
  118. * $ groundImage 背景画像、つまり透かしを入れる必要がある画像は、現在 GIF、JPG、および PNG 形式のみをサポートしています。
  119. * $waterPos 透かし位置、10 個の状態があり、0 はランダムな位置です。 * 1 は左上、2 は中央上、3 は右側を意味します
  120. * 4 は左側の中央、5 は中央の中央、6 は右側の中央を意味します
  121. * 7 は左側の下部を意味します。 、8 は中央の下、9 は右下です。
  122. * $waterImage 画像ウォーターマーク、つまりウォーターマークとして使用される画像は、現在 GIF、JPG、および PNG 形式のみをサポートしています。つまり、テキストは透かしとして使用され、ASCII コードをサポートしますが、中国語はサポートされません。
  123. * $textFont テキスト サイズ、値は 1 、 2、3、4 または 5、デフォルトは 5 です。値は 16 進数の色の値で、デフォルトは #FF0000 (赤) です。
  124. */
  125. echo "
  126. echo "いいえ。 ".($i+1)."画像の操作が成功しました
    ";
  127. }
  128. else{
  129. echo "いいえ". ($i+1)."画像をアップロードできません
    ";
  130. }
  131. }
  132. ?>
  133. コードをコピー
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。