PHP_メソッド_PHPウォーターマークの追加方法

7735 ワード

概要:機能:PHP画像透かし(透かしサポート画像または文字)*パラメータ:*$groundImage背景画像、すなわち透かしが必要な画像は、GIF、JPG、PNG形式のみをサポートする.*waterPos
簡単なテスト:imageWaterMark($upload_path.$large_image_name.$_SESSION['user_file_ext'],9,'logo_110 x 55.jpg',',5,'#ccc',',0,0);

<?PHP
/*
*   :PHP     (    [zhi chi]     [wen zi])
*   [can shu]:
*       $groundImage         ,         ,    [zhi chi]GIF,JPG,PNG  ;
*       $waterPos            [wei zhi], 10   [zhuang tai],0     [wei zhi];
*                       1     ,2     ,3     ;
*                       4     ,5     ,6     ;
*                       7     ,8     ,9     ;
*       $waterImage          ,        ,    [zhi chi]GIF,JPG,PNG  ;
*       $waterText         [wen zi]  ,    [wen zi]     ,  [zhi chi]ASCII ,   [zhi chi]  [zhong wen];
*       $fontSize          [wen zi]  ,  1、2、3、4 5,  [mo ren] 5;
*       $textColor         [wen zi]  ,      [shi liu jin zhi]   ,  [mo ren] #CCCCCC(   );
*       $fontfile        ttf  [zi ti]  [wen jian],     [she zhi]  [wen zi]     [zi ti]。  windows   [yong hu]   [xi tong] [xi tong pan]    
*                         [sou suo]*.ttf      [xi tong]   [an zhuang]   [zi ti]  [wen jian],      [wen jian]    [wang zhan]      ,
*                         [mo ren]     [dang qian mu lu] arial.ttf。
*       $xOffset              [pian yi liang],    [mo ren]             ,  [mo ren] 0,         
*                                ,    [she zhi]   , :2       [mo ren]       [you yi]2   [dan wei],-2      [zuo yi]   [dan wei]
*       $yOffset              [pian yi liang],    [mo ren]             ,  [mo ren] 0,         
*                                ,    [she zhi]   , :2       [mo ren]       2   [dan wei],-2         [dan wei]
*    :
*        0       
*        1              [zhi chi]
*        2              
*        3                         [wen zi]  [qu yu]  ,      
*        4     [zi ti]  [wen jian]   
*        5       [wen zi]       
*        6                [zhi chi]
*   [xiu gai]  :
*         
*   :Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
*       $waterImage   $waterText         ,       ,     $waterImage。
*        $waterImage  [you xiao] ,  [can shu]$waterString、$stringFont、$stringColor    。
*                 [wen jian] [wen jian ming]  $groundImage   。
*   :   
*   :2007-4-28
*   [shuo ming]:     longware       。
*/
function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$fontSize=12,$textColor="#CCCCCC", $fontfile='./arial.ttf',$xOffset=0,$yOffset=0)
{

   $isWaterImage = FALSE;
     //  [du qu]    [wen jian]
     if(!empty($waterImage) && file_exists($waterImage)) {
         $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;
             case 3:$water_im = imagecreatefrompng($waterImage);break;
             default:return 1;
         }
     }

     //  [du qu]    
     if(!empty($groundImage) && file_exists($groundImage)) {
         $ground_info = getimagesize($groundImage);
         $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:return 1;
         }
     } else {
         return 2;
     }

     //    [wei zhi]
     if($isWaterImage) { //      
         $w = $water_w;
         $h = $water_h;
         $label = "   ";
         } else {  
     //  [wen zi]  
        if(!file_exists($fontfile))return 4;
         $temp = imagettfbbox($fontSize,0,$fontfile,$waterText);//     TrueType   [zi ti]   [wen ben]   [fan wei]
         $w = $temp[2] - $temp[6];
         $h = $temp[3] - $temp[7];
         unset($temp);
     }
     if( ($ground_w < $w) || ($ground_h < $h) ) {
         return 3;
     }
     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;     
     }

     //    [tu xiang]     [mo shi]
     imagealphablending($ground_im, true);

     if($isWaterImage) { //    
         imagecopy($ground_im, $water_im, $posX + $xOffset, $posY + $yOffset, 0, 0, $water_w,$water_h);//  [kao bei]     [mu biao]  [wen jian]         
     } else {//  [wen zi]  
         if( !empty($textColor) && (strlen($textColor)==7) ) {
             $R = hexdec(substr($textColor,1,2));
             $G = hexdec(substr($textColor,3,2));
             $B = hexdec(substr($textColor,5));
         } else {
           return 5;
         }
         imagettftext ( $ground_im, $fontSize, 0, $posX + $xOffset, $posY + $h + $yOffset, imagecolorallocate($ground_im, $R, $G, $B), $fontfile, $waterText);
     }

     //        
     @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: return 6;
     }

     //  [shi fang]  [nei cun]
     if(isset($water_info)) unset($water_info);
     if(isset($water_im)) imagedestroy($water_im);
     unset($ground_info);
     imagedestroy($ground_im);
     //
     return 0;
}
?>