国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

php圖片自動添加水印代碼,php圖片水印函數(shù)代碼

php中文網(wǎng)
發(fā)布: 2016-07-25 08:51:53
原創(chuàng)
1160人瀏覽過
  1. /*
  2. * 功能:php圖片水印 (水印支持圖片或文字)
  3. * 參數(shù):
  4. *$groundimage 背景圖片,即需要加水印的圖片,暫只支持gif,jpg,png格式;
  5. *$waterpos 水印位置,有10種狀態(tài),0為隨機位置;
  6. *1為頂端居左,2為頂端居中,3為頂端居右;
  7. *4為中部居左,5為中部居中,6為中部居右;
  8. *7為底端居左,8為底端居中,9為底端居右;
  9. *$waterimage 圖片水印,即作為水印的圖片,暫只支持gif,jpg,png格式;
  10. *$watertext 文字水印,即把文字作為為水印,支持ascii碼,不支持中文;
  11. *$textfont 文字大小,值為1、2、3、4或5,默認為5;
  12. *$textcolor 文字顏色,值為十六進制顏色值,默認為#ff0000(紅色);
  13. *
  14. * 注意:support gd 2.0,support freetype、gif read、gif create、jpg 、png
  15. *$waterimage 和 $watertext 最好不要同時使用,選其中之一即可,優(yōu)先使用 $waterimage。
  16. *當$waterimage有效時,參數(shù)$waterstring、$stringfont、$stringcolor均不生效。
  17. *加水印后的圖片的文件名和 $groundimage 一樣。
  18. * 作者:longware @ 2004-11-3 14:15:13
  19. */
  20. function imagewatermark($groundimage,$waterpos=0,$waterimage="",$watertext="",$textfont=5,$textcolor="#ff0000")
  21. {
  22. $iswaterimage = false;
  23. $formatmsg = "暫不支持該文件格式,請用圖片處理軟件將圖片轉(zhuǎn)換為gif、jpg、png格式。";
  24. //讀取水印文件
  25. if(!empty($waterimage) && file_exists($waterimage))
  26. {
  27. $iswaterimage = true;
  28. $water_info = getimagesize($waterimage);
  29. $water_w = $water_info[0];//取得水印圖片的寬
  30. $water_h = $water_info[1];//取得水印圖片的高
  31. switch($water_info[2])//取得水印圖片的格式
  32. {
  33. case 1:$water_im = imagecreatefromgif($waterimage);break;
  34. case 2:$water_im = imagecreatefromjpeg($waterimage);break;
  35. case 3:$water_im = imagecreatefrompng($waterimage);break;
  36. default:die($formatmsg);
  37. }
  38. }
  39. //讀取背景圖片
  40. if(!empty($groundimage) && file_exists($groundimage))
  41. {
  42. $ground_info = getimagesize($groundimage);
  43. $ground_w = $ground_info[0];//取得背景圖片的寬
  44. $ground_h = $ground_info[1];//取得背景圖片的高
  45. switch($ground_info[2])//取得背景圖片的格式
  46. {
  47. case 1:$ground_im = imagecreatefromgif($groundimage);break;
  48. case 2:$ground_im = imagecreatefromjpeg($groundimage);break;
  49. case 3:$ground_im = imagecreatefrompng($groundimage);break;
  50. default:die($formatmsg);
  51. }
  52. }
  53. else
  54. {
  55. die("需要加水印的圖片不存在!");
  56. }
  57. //水印位置
  58. if($iswaterimage)//圖片水印
  59. {
  60. $w = $water_w;
  61. $h = $water_h;
  62. $label = "圖片的";
  63. }
  64. else//文字水印
  65. {
  66. $temp = imagettfbbox(ceil($textfont*2.5),0,"./cour.ttf",$watertext);//取得使用 truetype 字體的文本的范圍
  67. $w = $temp[2] - $temp[6];
  68. $h = $temp[3] - $temp[7];
  69. unset($temp);
  70. $label = "文字區(qū)域";
  71. }
  72. if( ($ground_w{
  73. echo "需要加水印的圖片的長度或?qū)挾缺人?.$label."還小,無法生成水印!";
  74. return;
  75. }
  76. switch($waterpos)
  77. {
  78. case 0://隨機
  79. $posx = rand(0,($ground_w - $w));
  80. $posy = rand(0,($ground_h - $h));
  81. break;
  82. case 1://1為頂端居左
  83. $posx = 0;
  84. $posy = 0;
  85. break;
  86. case 2://2為頂端居中
  87. $posx = ($ground_w - $w) / 2;
  88. $posy = 0;
  89. break;
  90. case 3://3為頂端居右
  91. $posx = $ground_w - $w;
  92. $posy = 0;
  93. break;
  94. case 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. break;
  118. default://隨機
  119. $posx = rand(0,($ground_w - $w));
  120. $posy = rand(0,($ground_h - $h));
  121. break;
  122. }
  123. //設(shè)定圖像的混色模式
  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. //釋放內(nèi)存
  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. //--------
  159. if(isset($_files) && !empty($_files['userfile']) && $_files['userfile']['size']>0)
  160. {
  161. $uploadfile = "./".time()."_".$_files['userfile']['name'];
  162. if (copy($_files['userfile']['tmp_name'], $uploadfile))
  163. {
  164. echo "ok
    ";
  165. //文字水印
  166. imagewatermark($uploadfile,0,"","http://blog.csdn.net/longware/",5,"#ff0000");
  167. //圖片水印
  168. //$waterimage="./hanweb_shuiyin.gif";//水印圖片路徑
  169. //imagewatermark($uploadfile,0,$waterimage);
  170. echo "php圖片自動添加水印代碼,php圖片水印函數(shù)代碼 ";
  171. }
  172. else
  173. {
  174. echo "fail
    ";
  175. }
  176. }
  177. ?>
  178. 文件:
復(fù)制代碼


PHP速學教程(入門到精通)
PHP速學教程(入門到精通)

PHP怎么學習?PHP怎么入門?PHP在哪學?PHP怎么學才快?不用擔心,這里為大家提供了PHP速學教程(入門到精通),有需要的小伙伴保存下載就能學習啦!

下載
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔相應(yīng)法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn
最新問題
開源免費商場系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學習者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學習
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號