一個循環給指定目錄下的圖片文件添加水印的程序

下面是一個添加水印的方法,可以添加文字水印和圖片水印

  1. <?php  
  2. /* 
  3. * 功能:PHP圖片水印 (水印支持圖片或文字) 
  4. * 參數: 
  5. *      $groundImage    背景圖片,即需要加水印的圖片,暫只支持GIF,JPG,PNG格式; 
  6. *      $waterPos        水印位置,有10種狀態,0爲隨機位置; 
  7. *                        1爲頂端居左,2爲頂端居中,3爲頂端居右; 
  8. *                        4 爲中部居左,5爲中部居中,6爲中部居右; 
  9. *                        7爲底端居左,8爲底端居中,9爲底端居右; 
  10. *      $waterImage        圖片水印,即作爲水印的圖片,暫只支持GIF,JPG,PNG格式; 
  11. *      $waterText        文字水印,即把文字作爲爲水印,支持ASCII碼,不支持中文; 
  12. *      $textFont        文字大小,值爲1、2、3、4或5,默認爲5; 
  13. *      $textColor        文字顏色,值爲十六進制顏色值,默認爲#FF0000(紅色); 
  14. * 
  15. * 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG 
  16. *      $waterImage 和 $waterText 最好不要同時使用,選其中之一即可,優先使用 $waterImage。 
  17. *      當$waterImage有效時,參數$waterString、$stringFont、$stringColor均不生效。 
  18. *      加水印後的圖片的文件名和 $groundImage 一樣。 
  19. * 作者:longware @ 2004-11-3 14:15:13 
  20. */  
  21. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000")  
  22. {  
  23.     $isWaterImage = FALSE;  
  24.     $formatMsg = "暫不支持該文件格式,請用圖片處理軟件將圖片轉換爲GIF、JPG、PNG格式。";  
  25.     //讀取水印文件  
  26.     if(!empty($waterImage) && file_exists($waterImage))  
  27.     {  
  28.         $isWaterImage = TRUE;  
  29.         $water_info = getimagesize($waterImage);  
  30.         $water_w    = $water_info[0];//取得水印圖片的寬  
  31.         $water_h    = $water_info[1];//取得水印圖片的高  
  32.         switch($water_info[2])//取得水印圖片的格式  
  33.         {  
  34.             case 1:$water_im = imagecreatefromgif($waterImage);break;  
  35.             case 2:$water_im = imagecreatefromjpeg($waterImage);break;  
  36.             case 3:$water_im = imagecreatefrompng($waterImage);break;  
  37.             default:die('水印圖片格式不正確');  
  38.         }  
  39.     }  
  40.     //讀取背景圖片  
  41.     if(!empty($groundImage) && file_exists($groundImage))  
  42.     {  
  43.         $ground_info = getimagesize($groundImage);  
  44.         $ground_w    = $ground_info[0];//取得背景圖片的寬  
  45.         $ground_h    = $ground_info[1];//取得背景圖片的高  
  46.         switch($ground_info[2])//取得背景圖片的格式  
  47.         {  
  48.             case 1:$ground_im = imagecreatefromgif($groundImage);break;  
  49.             case 2:$ground_im = imagecreatefromjpeg($groundImage);break;  
  50.             case 3:$ground_im = imagecreatefrompng($groundImage);break;  
  51.             default:{  
  52.                 echo('需要加水的圖片格式不支持!');  
  53.                 return false;  
  54.             }  
  55.         }  
  56.     }  
  57.     else  
  58.     {  
  59.         echo("需要加水印的圖片不存在!");  
  60.         return true;  
  61.     }  
  62.     //水印位置  
  63.     if($isWaterImage)//圖片水印  
  64.     {  
  65.         $w = $water_w;  
  66.         $h = $water_h;  
  67.         $label = "圖片的";  
  68.     }  
  69.     else//文字水印  
  70.     {  
  71.         $temp = imagettfbbox(ceil($textFont*2.5),0,"./cour.ttf",$waterText);//取得使用 TrueType 字體的文本的範圍  
  72.         $w = $temp[2] - $temp[6];  
  73.         $h = $temp[3] - $temp[7];  
  74.         unset($temp);  
  75.         $label = "文字區域";  
  76.     }  
  77.     if( ($ground_w<$w) || ($ground_h<$h) )  
  78.     {  
  79.         echo "需要加水印的圖片的長度或寬度比水印".$label."還小,無法生成水印!<br/>";  
  80.         return true;  
  81.     }  
  82.     switch($waterPos)  
  83.     {  
  84.         case 0://隨機  
  85.             $posX = rand(0,($ground_w - $w));  
  86.             $posY = rand(0,($ground_h - $h));  
  87.             break;  
  88.         case 1://1爲頂端居左  
  89.             $posX = 0;  
  90.             $posY = 0;  
  91.             break;  
  92.         case 2://2爲頂端居中  
  93.             $posX = ($ground_w - $w) / 2;  
  94.             $posY = 0;  
  95.             break;  
  96.         case 3://3爲頂端居右  
  97.             $posX = $ground_w - $w;  
  98.             $posY = 0;  
  99.             break;  
  100.         case 4://4爲中部居左  
  101.             $posX = 0;  
  102.             $posY = ($ground_h - $h) / 2;  
  103.             break;  
  104.         case 5://5爲中部居中  
  105.             $posX = ($ground_w - $w) / 2;  
  106.             $posY = ($ground_h - $h) / 2;  
  107.             break;  
  108.         case 6://6爲中部居右  
  109.             $posX = $ground_w - $w;  
  110.             $posY = ($ground_h - $h) / 2;  
  111.             break;  
  112.         case 7://7爲底端居左  
  113.             $posX = 0;  
  114.             $posY = $ground_h - $h;  
  115.             break;  
  116.         case 8://8爲底端居中  
  117.             $posX = ($ground_w - $w) / 2;  
  118.             $posY = $ground_h - $h;  
  119.             break;  
  120.         case 9://9爲底端居右  
  121.             $posX = $ground_w - $w;  
  122.             $posY = $ground_h - $h;  
  123.             break;  
  124.         default://隨機  
  125.             $posX = rand(0,($ground_w - $w));  
  126.             $posY = rand(0,($ground_h - $h));  
  127.             break;      
  128.     }  
  129.     //設定圖像的混色模式  
  130.     imagealphablending($ground_im, true);  
  131.     if($isWaterImage)//圖片水印  
  132.     {  
  133.         imagecopy($ground_im$water_im$posX$posY, 0, 0, $water_w,$water_h);//拷貝水印到目標文件          
  134.     }  
  135.     else//文字水印  
  136.     {  
  137.         if( !empty($textColor) && (strlen($textColor)==7) )  
  138.         {  
  139.             $R = hexdec(substr($textColor,1,2));  
  140.             $G = hexdec(substr($textColor,3,2));  
  141.             $B = hexdec(substr($textColor,5));  
  142.         }  
  143.         else  
  144.         {  
  145.             die("水印文字顏色格式不正確!");  
  146.         }  
  147.         imagestring ( $ground_im$textFont$posX$posY$waterText, imagecolorallocate($ground_im$R$G$B));          
  148.     }  
  149.     //生成水印後的圖片  
  150.     @unlink($groundImage);  
  151.     switch($ground_info[2])//取得背景圖片的格式  
  152.     {  
  153.         case 1:imagegif($ground_im,$groundImage);break;  
  154.         case 2:imagejpeg($ground_im,$groundImage);break;  
  155.         case 3:imagepng($ground_im,$groundImage);break;  
  156.         default:{  
  157.             echo($errorMsg);  
  158.             return false;  
  159.               
  160.         }  
  161.     }  
  162.     //釋放內存  
  163.     if(isset($water_info)) unset($water_info);  
  164.     if(isset($water_im)) imagedestroy($water_im);  
  165.     unset($ground_info);  
  166.     imagedestroy($ground_im);  
  167.     return true;  
  168. }  
  169. ?>  

 

下面的代碼是給指定目錄下的所有jpg,gif,png圖片添加水印

  1. <?php  
  2. require_once 'addWater.php';  
  3.   
  4. set_time_limit(6000);  
  5. header("Chontent-Type:text/html;charset=gbk");  
  6. $arr=addWaterToAll('property_image');  
  7. echo '未添加水印成功的圖片路徑如下:<br/>';  
  8. for($j=0;$j<count($arr);$j++){  
  9.     echo $arr[$j].'<br/>';  
  10. }  
  11.   
  12. function addWaterToAll($dirPath){  
  13.     $failImg = array();  
  14.     $pathArr=get_filetree($dirPath);  
  15.     echo '圖片數量:'.count($pathArr).'<br/>';  
  16.     for ($i=0;$i<count($pathArr);$i++){  
  17.         $path$pathArr[$i];  
  18.         $typeArr=array('.jpg','.gif','.png');  
  19.         if(in_array(substr($path,strrpos($path,'.')),$typeArr)){  
  20.             //chmod($path,0777);  
  21.             //echo substr($path,strrpos($path,'.'));  
  22.             //echo $path;  
  23.             $succ=imageWaterMark($path,5,"images/water.png"); //添加水印  
  24.             if($succ){  
  25.                 echo ($i+1).$path.'添加水印成功!<br/>';  
  26.             }else{  
  27.                 echo '失敗:'.$path.'<br/>';  
  28.                 $failImg[]=$path;  
  29.             }     
  30.         }  
  31.     }  
  32.     return $failImg;  
  33. }  
  34. function get_filetree($path){  
  35.     $tree = array();  
  36.     foreach(glob($path.'/*'as $single){  
  37.         if(is_dir($single)){  
  38.             $tree = array_merge($tree,get_filetree($single));  
  39.         }  
  40.         else{  
  41.             $tree[] = $single;  
  42.         }  
  43.     }  
  44.       
  45.     return $tree;  
  46. }  
  47.   
  48. ?>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章