-
-
/*
- * 功能:php圖片水印(水印支援圖片或文字)
- * 參數:
- *$ groundimage 背景圖片,即需要加浮水印的圖片,暫只支援gif,jpg,png格式;
- *$waterpos 水印位置,有10種狀態,0為隨機位置;
- *1為頂端居左,2為頂端居中,3為頂端居右;
- *4為中部居左,5為中部居中,6為中部居右;
- *7為底端居左,8為底端居中,9為底端居右;
- *$waterimage 圖片浮水印,即作為浮水印的圖片,暫只支援gif,jpg,png格式;
- *$watertext 文字水印,即把文字作為為水印,支援ascii碼,不支援中文;
- *$textfont 文字大小,值為1、2、3、4或5,預設為5;
- *$textcolor 文字顏色,值為十六進位顏色值,預設為#ff0000(紅);
- *
- * 注意:support gd 2.0,support freetype、gif read、gif create、jpg 、png
- *$waterimage 和$watertext 最好不要同時使用,選其中之一即可,優先使用$waterimage。
- *當$waterimage有效時,參數$waterstring、$stringfont、$stringcolor均不生效。
- *加浮水印後的圖片的檔案名稱和 $groundimage 一樣。
- * 作者:longware @ 2004-11-3 14:15:13
- */
- function imagewatermark($groundimage,$waterpos=0,$waterimage="",$watertext="",$ textfont=5,$textcolor="#ff0000")
- {
- $iswaterimage = false;
- $formatmsg = "暫不支援此檔案格式,請用圖片處理軟體將圖片轉換為gif、jpg 、png格式。";
- //讀取浮水印檔案
- if(!empty($waterimage) && file_exists($waterimage))
- {
- $iswaterimage = true;
- $water_info = getimagesize($iswaterimage = true;
- $water_info = getimagesize($ waterimage);
- $water_w = $water_info[0];//取得浮水印圖片的寬度
- $water_h = $water_info[1];//取得水印圖片的高
- switch($water_info[2 ])//取得浮水印圖片的格式
- {
- case 1:$water_im = imagecreatefromgif($waterimage);break;
- case 2:$water_im = imagecreatefromjpeg($waterimage);break;break
- case 3:$water_im = imagecreatefrompng($waterimage);break;
- default:die($formatmsg);
- }
- }
- //讀取背景圖片
- if(!empty( $groundimage) && file_exists($groundimage))
- {
- $ground_info = getimagesize($groundimage);
- $ground_w = $ground_info[0];//取得背景圖片的寬度
- $ground_w = $ground_info[0];//取得背景圖片的寬度
- $ground_h = $ground_info[1];//取得背景圖片的高
- switch($ground_info[2])//取得背景圖片的格式
- {
- case 1:$ground_im = imagecreatefromgif($groundimage) ;break;
- case 2:$ground_im = imagecreatefromjpeg($groundimage);break;
- case 3:$ground_im = imagecreatefrompng($groundimage);break;
- default:die($formatmsg) }
- }
- else
- {
- die("需要浮水印的圖片不存在!");
- }
- //水印位置
- if($iswaterimage) //圖片浮水印
- {
- $w = $water_w;
- $h = $water_h;
- $label = "圖片的";
- }
- else//文字浮水印
- {
- $temp = imagettfbbox(ceil($textfont*2.5),0,"./cour.ttf",$watertext);//取得使用truetype 字型的文字的範圍
- $w = $ temp[2] - $temp[6];
- $h = $temp[3] - $temp[7];
- unset($temp);
- $label = "文字區域";
- }
- if( ($ground_w{
- echo "需要加水印的圖片的長度或寬度比水印".$label. "還小,無法產生浮水印! ";
- return;
- }
- switch($waterpos)
- {
- case 0://隨機
- $posx = rand(0,($ground_w - $w)) ;
- $posy = rand(0,($ground_h - $h));
- break;
- case 1://1為頂端居左
- $posx = 0;
- $posy = 0;
- break;
- case 2://2為頂端居中
- $posx = ($ground_w - $w) / 2;
- $posy = 0;
- break;
- case 3://3為頂端居右
- $posx = $ground_w - $w;
- $posy = 0;
- break;
- case 4://4為中部居左
- $posx = 0;
- $posy = ($ground_h - $h) / 2;
- break;
- case 5://5為中部居中
- $posx = ($ground_w - $w) / 2;
- $posy = ($ground_h - $h) / 2;
- break;
- case 6://6為中部居右
- $posx = $ground_w - $ w;
- $posy = ($ground_h - $h) / 2;
- break;
- case 7://7為底端居左
- $posx = 0;
- $posy = $ground_h - $h;
- break;
- case 8://8為底端居中
- $posx = ($ground_w - $w) / 2;
- $posy = $ground_h - $ h;
- break;
- case 9://9為底端居右邊
- $posx = $ground_w - $w;
- $posy = $ground_h - $h;
- break;
- default://隨機
- $posx = rand(0,($ground_w - $w));
- $posy = rand(0,($ground_h - $h));
- break ;
- }
- //設定影像的混色模式
- imagealphablending($ground_im, true);
- if($iswaterimage)//圖片浮水印
- {
- imagecopy($ground_im , $water_im, $posx, $posy, 0, 0, $water_w,$water_h);//拷貝水印到目標檔案
- }
- else//文字浮水印
- {
- if( ! empty($textcolor) && (strlen($textcolor)==7) )
- {
- $r = hexdec(substr($textcolor,1,2));
- $g = hexdec(substr( $textcolor,3,2));
- $b = hexdec(substr($textcolor,5));
- }
- else
- { die("水印文字顏色格式不正確!");
- }
- imagestring ( $ground_im, $textfont, $posx, $posy, $watertext, imagecolorallocate($ground_im, $r, $g, $b));
- }
- //產生浮水印後的圖片
- @unlink($groundimage);
- switch($ground_info[2])//取得背景圖片的格式
- {
- case 1:imagegif($ground_im, $groundimage);break;
- case 2:imagejpeg($ground_im,$groundimage);break;
- case 3:imagepng($ground_im,$groundimage);break;
- default:die($errormsg) ;
- }
- //釋放記憶體
- if(isset($water_info)) unset($water_info);
- if(isset($water_im)) imagedestroy($water_im);
- unset ($ground_info);
- imagedestroy($ground_im);
- }
- //--------
- if(isset($_files) && !empty($_files['userfile ']) && $_files['userfile']['size']>0)
- {
- $uploadfile = "./".time()."_".$_files['userfile'][ '名稱'];
- if (copy($_files['userfile']['tmp_name'], $uploadfile))
- {
- echo "ok
";
- //文字水印
- imagewatermark($uploadfile,0,"","http://blog.csdn.net/longware/",5,"#ff0000");
- //圖片浮水印
- //$ waterimage="./hanweb_shuiyin.gif";//水印圖片路徑
- //imagewatermark($uploadfile,0,$waterimage);
- echo "";
- }
- else
- {
- echo "fail
";
- }
- }
- ?>
-
- 檔案:
-
-
複製程式碼
|