当前位置:   article > 正文

个人技术作品 - PHP 水印类及上传图片加水印结合使用的API及使用实例_new watermark api

new watermark api
水印类:

cls.WaterMark.php
<? php
/* *
 * cls.WaterMark.php
 * 
 * GMT 系列 - 类库接口
 * 
 * 功能库名: 水印图制造类
 * 
 * @example 
 * $WM = new WaterMark('Blue hills.jpg','RAND',false,true); -> 参数1:处理图,参数2:水印位置,参数3:水印方式,参数4:输出方式
 * $WM->WM_Letter = "RCgame.cn"; -> 当参数3为false时,此水印字符串有效
 * $WM->WM_FontCol = '#000000'; -> 当参数3为false时,此水印字符串颜色设置有效
 * $WM->WM_ImagePath = '1.png'; -> 当参数3为true时,此水印图片有效
 * $WM->ExtrudeWaterMark(); -> 压出水印效果图
 
*/
class  WaterMark
{
    
/* *
     * 水印依附位置
     * LT = 左上角 , LB = 左下角 , RT = 右上角 , RB = 右下角 , CC = 居中
     * CT = 顶端居中 CB = 底端居中 为数组时,则为自定义水印位置 array(x,y)
     
*/
    
public   $WM_Position   =   ' LT ' ;
    
    
/* *
     * 取出当前水印位置
     * 
     * 以数组方式返回当前水印位置 array(x,y)
     
*/
    
public   $WM_FetchPosition   =   array ();
    
    
/* *
     * 水印依附方式
     * true = 文字依附 , false = 图片依附
     
*/
    
public   $WM_Method   =   false ;
    
    
/* *
     * 水印文字信息
     * 当 $WM_Method 为 true 时,此参数设置有效
     
*/
    
public   $WM_Letter   =   ' information empty ' ;
    
    
/* *
     * 水印文字信息倾斜度
     
*/
    
public   $WM_Slant   =   0 ;
    
    
/* *
     * 水印文字字体
     * 当值为 1,2,3,4,5 时为内置字体
     
*/
    
public   $WM_Font   =   ' 1.ttf ' ;
    
    
/* *
     * 水印文字大小
     * 
     
*/
    
public   $WM_FontSize   =   12 ;
    
    
/* *
     * 水印文字颜色
     * 当值为 false 时,字体颜色随机,否则为固定
     
*/
    
public   $WM_FontCol   =   false ;
    
    
/* *
     * 水印文字离边微调
     
*/
    
public   $WM_Inching   =   10 ;
    
    
/* *
     * 水印图片地址
     * 当 $WM_Method 为 false 时,此参数设置有效
     
*/
    
public   $WM_ImagePath   =   '' ;
    
    
/* *
     * 图片源
     
*/
    
private   $WM_ImageSource   =   '' ;
    
    
/* *
     * 图象制压过程图片源
     
*/
    
private   $WM_CourseImageSource   =   '' ;
    
    
/* *
     * 背景图
     
*/
    
private   $WM_BackImage   =   '' ;
    
    
/* *
     * 对外输出图片连接接口
     * 上传处理时,可使用源路径,亦可以使用此参数路径输出
     
*/
    
public   $WM_OutPutImage   =   '' ;
    
    
/* *
     * 保存/预览输出
     * 
     * 如果 True 时,则为保存输出,图片一但被压入类中,处理成功,立即改变源图片(即加了水印的效果图)
     * 如果 False 时,则为预览输出,图片被压入类中,处理成功,只会输出预览图,但源图将不会被改变(即加了水印后的预览图)
     * 
     * 如果用于上传处理,必须使用 True
     
*/
    
private   $WM_SaveOutPut   =   false ;
    
    
/* *
     * 限制图片大小小于设置参数时,不添加水印
     * array($w,$h,true)
     * $w 限制宽度 $h 限制高度 true 双方条件满足/ false单方条件满足
     
*/
    
public   $WM_LimitParam   =   array ( 0 , 0 , true );
    
    
/* *
     * 是否文字水印使用描秒效果
     
*/
    
public   $WM_FontOuter   =   false ;
    
    
/* *
     * 构造函数
     *
     * @param string $ImageSource
     * @param string $Position
     * @param booleam $Method
     
*/
    
public   function  WaterMark( $ImageSource , $Position   =   ' LT ' , $Method   =   false , $SaveOutPut   =   false ) {
        
if ( $ImageSource   ==   '' ) die ( ' 图片源未设置 ' );
        
if ( ! file_exists ( $ImageSource )) die ( ' 未发现有效图片源 ' );
        
        
$this -> WM_ImageSource  =   $ImageSource ;
        
$this -> WM_Position  =   $Position ;
        
$this -> WM_Method  =   $Method ;
        
$this -> WM_SaveOutPut  =   $SaveOutPut ;
    }
    
    
public   function  ExtrudeWaterMark() {
        
//  取图象信息
         $ImageType   =   '' ;
        
$ImageType   =   getimagesize ( $this -> WM_ImageSource , $ImageType );
        
//  根据源图象信息,创建大小一样的真色彩图模
         $this -> WM_BackImage  =  imagecreatetruecolor( $ImageType [ 0 ] , $ImageType [ 1 ]);
        
//  分配 WBR 颜色
         $White   =  imagecolorallocate( $this -> WM_BackImage , 255 , 255 , 255 );  //  白
          
          // 把背景填充为白色

          imagefill( $this -> WM_BackImage , 0 , 0 , $White );
          
          
//  判断源图片文件类型,创建一个源图
           switch  ( $ImageType [ 2 ])
          {
               
case   1 :
                
$this -> WM_CourseImageSource  =  imagecreatefromgif( $this -> WM_ImageSource);      // gif
                 break
               
case   2 :
                
$this -> WM_CourseImageSource  =  imagecreatefromjpeg( $this -> WM_ImageSource);      // jpg
                 break
               
case   3 :
                
$this -> WM_CourseImageSource  =  imagecreatefrompng( $this -> WM_ImageSource);      // png
                 break ;
               
default :
                
return   false ;
          }
          
          
//  根据源比例,把源图片压入真色彩图模
          imagecopy( $this -> WM_BackImage , $this -> WM_CourseImageSource , 0 , 0 , 0 , 0 , $ImageType [ 0 ] , $ImageType [ 1 ]);  
          
          
if ( $this -> WM_Method) {
              
//  颜色选择方案
               if ( ! $this -> WM_FontCol) {
                  
$FontColor   =  imageColorAllocate( $this -> WM_BackImage ,   mt_rand ( 0 ,   255 ) ,   mt_rand ( 0 ,   255 ) ,   mt_rand ( 0 ,   255 ));
              } 
else  {
                  
$FontColor   =   $this -> FetchColor( $this -> WM_FontCol);
              }
              
// 支持中文水印
               $str   =   iconv ( ' GB2312 ' , ' UTF-8 ' , $this -> WM_Letter);
              
              
//  限制水印压入
               if ( false   !==   $this -> WM_isLimitType) {
                  
if ( $this -> WM_LimitParam[ 2 ]) {
                      
if ( $ImageType [ 0 >   $this -> WM_LimitParam[ 0 &&   $ImageType [ 1 >   $this -> WM_LimitParam[ 1 ]) return   false ;
                  } 
else  {
                      
if ( $ImageType [ 0 >   $this -> WM_LimitParam[ 0 ||   $ImageType [ 1 >   $this -> WM_LimitParam[ 1 ]) return   false ;
                  }
              }
              
              
switch  ( $this -> WM_Position) {
                  
case   ' LT ' :
                      
$x   =   $this -> WM_Inching;
                      
$y   =   $this -> WM_Inching  *   2 ;
                    
break ;
                  
case   ' LB ' :
                      
$x   =   $this -> WM_Inching;
                      
$y   =   $ImageType [ 1 -   $this -> WM_Inching;
                    
break ;
                  
case   ' RT ' :
                      
$x   =   $ImageType [ 0 -  ( strlen ( $this -> WM_Letter)  *   7.5 -   10 ;
                      
$y   =   $this -> WM_Inching  *   2 ;
                    
break ;
                  
case   ' RB ' :
                      
$x   =   $ImageType [ 0 -  ( strlen ( $this -> WM_Letter)  *   7.5 -   10 ;
                      
$y   =   $ImageType [ 1 -   $this -> WM_Inching;
                    
break ;
                  
case   ' CC ' :
                      
$x   =   $ImageType [ 0 /   2   -   strlen ( $this -> WM_Letter)  *   6   /   2 ;
                      
$y   =   $ImageType [ 1 /   2 ;
                    
break ;
                
case   ' CT ' :
                      
$x   =   $ImageType [ 0 /   2   -   strlen ( $this -> WM_Letter)  *   6   /   2 ;
                      
$y   =   $this -> WM_Inching  *   2 ;
                    
break ;
                
case   ' CB ' :
                      
$x   =   $ImageType [ 0 /   2   -   strlen ( $this -> WM_Letter)  *   6   /   2 ;
                      
$y   =   $ImageType [ 1 -   $this -> WM_Inching;
                    
break ;
                
case   ' RAND ' :
                      
$x   =   rand ( 10 , $ImageType [ 0 -  ( strlen ( $this -> WM_Letter)  *   7.5 -   10 );
                      
$y   =   rand ( 10 , $ImageType [ 1 -   10 );
                    
break ;
                
case   is_array ( $this -> WM_Position) :
                    
$x   =   $this -> WM_Position[ 0 ];
                      
$y   =   $this -> WM_Position[ 1 ];
                    
break ;
                
default :
                    
$x   =   rand ( 10 , $ImageType [ 0 -  ( strlen ( $this -> WM_Letter)  *   7.5 -   10 );
                      
$y   =   rand ( 10 , $ImageType [ 1 -   10 );
                    
break ;
              }
              
              
$this -> WM_FetchPosition  =   array ( $x , $y );  //  当前位置赋值
              
              imagettftext(
$this -> WM_BackImage ,   $this -> WM_FontSize ,   $this -> WM_Slant ,
                                   
$x ,   $y ,   $FontColor ,   $this -> WM_Font ,   $str );
          } 
else  {
              
//  限制水印压入
               if ( false   !==   $this -> WM_isLimitType) {
                  
if ( $this -> WM_LimitParam[ 2 ]) {
                      
if ( $ImageType [ 0 >   $this -> WM_LimitParam[ 0 &&   $ImageType [ 1 >   $this -> WM_LimitParam[ 1 ]) return   false ;
                  } 
else  {
                      
if ( $ImageType [ 0 >   $this -> WM_LimitParam[ 0 ||   $ImageType [ 1 >   $this -> WM_LimitParam[ 1 ]) return   false ;
                  }
              }
              
if ( ! file_exists ( $this -> WM_ImagePath)) die ( ' 压入水印图片时,无法定位水印图片位置 ' );
              
              
$WaterInfo   =   '' ;
              
$WaterInfo   =   getimagesize ( $this -> WM_ImagePath , $WaterInfo );
              
switch  ( $WaterInfo [ 2 ])
              {
                   
case   1 :
                    
$WaterImage   =  imagecreatefromgif( $this -> WM_ImagePath);      // gif
                     break
                   
case   2 :
                    
$WaterImage   =  imagecreatefromjpeg( $this -> WM_ImagePath);      // jpg
                     break
                   
case   3 :
                    
$WaterImage   =  imagecreatefrompng( $this -> WM_ImagePath);      // png
                     break
                   
default :
                    
return   false ;
              }
              
              
switch  ( $this -> WM_Position) {
                  
case   ' LT ' :
                      
$x   =   0 ;
                      
$y   =   0 ;
                    
break ;
                  
case   ' LB ' :
                      
$x   =   0 ;
                      
$y   =   $ImageType [ 1 -   $WaterInfo [ 1 ];
                    
break ;
                  
case   ' RT ' :
                      
$x   =   $ImageType [ 0 -   $WaterInfo [ 0 ];
                      
$y   =   0 ;
                    
break ;
                  
case   ' RB ' :
                      
$x   =   $ImageType [ 0 -   $WaterInfo [ 0 ];
                      
$y   =   $ImageType [ 1 -   $WaterInfo [ 1 ];
                    
break ;
                  
case   ' CC ' :
                      
$x   =   $ImageType [ 0 /   2   -   $WaterInfo [ 0 /   2 ;
                      
$y   =   $ImageType [ 1 /   2 ;
                    
break ;
                
case   ' CT ' :
                      
$x   =   $ImageType [ 0 /   2   -   $WaterInfo [ 0 /   2 ;
                      
$y   =   0 ;
                    
break ;
                
case   ' CB ' :
                      
$x   =   $ImageType [ 0 /   2   -   $WaterInfo [ 0 /   2 ;
                      
$y   =   $ImageType [ 1 -   $WaterInfo [ 1 ];
                    
break ;
                
case   ' RAND ' :
                      
$x   =   rand ( 0 , $ImageType [ 0 -   $WaterInfo [ 0 ]);
                      
$y   =   rand ( 0 , $ImageType [ 1 -   $WaterInfo [ 1 ]);
                    
break ;
                
case   is_array ( $this -> WM_Position) :
                    
$x   =   $this -> WM_Position[ 0 ];
                      
$y   =   $this -> WM_Position[ 1 ];
                    
break ;
                
default :
                    
$x   =   rand ( 0 , $ImageType [ 0 -   $WaterInfo [ 0 ]);
                      
$y   =   rand ( 0 , $ImageType [ 1 -   $WaterInfo [ 1 ]);
                    
break ;
              }
              
$this -> WM_FetchPosition  =   array ( $x , $y );  //  当前位置赋值
              imagecopy( $this -> WM_BackImage , $WaterImage , $x , $y , 0 , 0 , $WaterInfo [ 0 ] , $WaterInfo [ 1 ]);
              imagedestroy(
$WaterImage );
          }
          
          
//  判断源图象格式,按原格式重新输出图象
           switch  ( $ImageType [ 2 ])
          {
               
case   1 :
                   
if ( $this -> WM_SaveOutPut) {
                       imagegif(
$this -> WM_BackImage ,   $this -> WM_ImageSource);      // gif
                   }  else  {
                       
header ( " Content-type: image/gif " );
                       imagegif(
$this -> WM_BackImage);      // gif
                   }
                
break ;
               
case   2 :
                   
if ( $this -> WM_SaveOutPut) {
                    imagejpeg(
$this -> WM_BackImage ,   $this -> WM_ImageSource);      // jpg
                   }  else  {
                       
header ( " Content-type: image/jpeg " );
                       imagejpeg(
$this -> WM_BackImage);      // jpg
                   }
                
break ;
               
case   3 :
                   
if ( $this -> WM_SaveOutPut) {
                    imagepng(
$this -> WM_BackImage ,   $this -> WM_ImageSource);      // png
                }  else  {
                    
header ( " Content-type: image/png " );
                       imagepng(
$this -> WM_BackImage);      // png
                   }
                
break ;
               
default :
                
return   false ;
          }
          
          
$this -> WM_OutPutImage  =   $this -> WM_ImageSource;
          
          @imagedestroy(
$this -> WM_BackImage);
          @imagedestroy(
$this -> WM_CourseImageSource);
          
return   true ;
    }
    
    
/* *
     * 取色,十进值转二进制函数
     *
     * @param Color $Color
     * @return Color
     
*/
    
private   function  FetchColor( $Color   =   '' )
    {
        
$Color   =   ereg_replace ( " ^# " , "" , $Color );
        
$r   =   $Color [ 0 ] . $Color [ 1 ];
        
$r   =   hexdec ( $r );
        
$b   =   $Color [ 2 ] . $Color [ 3 ];
        
$b   =   hexdec ( $b );
        
$g   =   $Color [ 4 ] . $Color [ 5 ];
        
$g   =   hexdec ( $g );
        
$Color   =  imagecolorallocate ( $this -> WM_BackImage ,   $r ,   $b ,   $g );
        
return   $Color ;
    }
}
?>

 

例子程序:
上传图片 + 水印

新建 uploadimg 目录存在上传图片的文件夹;
新建 Water 目录存放水印文件;

upload.php

<? php
include ( ' cls.WaterMark.php ' );
$uptypes = array ( ' image/jpg ' ,     // 上传文件类型列表
     ' image/jpeg ' ,  
    
' image/png ' ,  
    
' image/pjpeg ' ,
    
' image/gif ' ,
    
' image/bmp ' ,
    
' image/x-png ' );
$max_file_size = 2000000 ;      // 上传文件大小限制, 单位BYTE
$destination_folder = " uploadimg/ " // 上传文件路径
$watermark = 1 ;       // 是否附加水印(1为加水印,其他为不加水印);
$watertype = 1 ;       // 水印类型(1为文字,2为图片)
$waterposition = 1 ;      // 水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring = " lengshuye.3322.org " ;   // 水印字符串
$waterimg = " xplore.gif " ;     // 水印图片
$imgpreview = 1 ;       // 是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize = 1 / 2 ;     // 缩略图比例
?>
< html >
< head >
< title > lengshuye . 3322 . org </ title >
< style type = " text/css " >
<!--
body 
{  
 font
- size :  9pt;

input {
 background
- color :   # 66CCFF;
 border :  1px inset  # CCCCCC;
}
-->  
</ style >
</ head >

< body >
< form enctype = " multipart/form-data "  method = " post "  name = " upform " >
  上传文件
:
  
< input name = " upfile "  type = " file " >
  
< input type = " submit "  value = " 上传 " >< br >
  允许上传的文件类型为
:<?= implode ( ' ' , $uptypes ) ?>   // implode将数组元素用“,”分开
</ form >

<? php
if  ( $_SERVER [ ' REQUEST_METHOD ' ==   ' POST ' )

 
if  ( ! is_uploaded_file ( $_FILES [ " upfile " ][ " tmp_name " ]))  // is_uploaded_file判断文件是否是通过 HTTP POST 上传的
 //是否存在文件

 {  
  
echo   " 图片不存在! " ;
  
exit ;
 }
 
    
$file   =   $_FILES [ " upfile " ];
   
if ( $max_file_size   <   $file [ " size " ])
   
// 检查文件大小
   {
    
echo   " 文件太大! " ;
    
exit ;
   }

 
if ( ! in_array ( $file [ " type " ] ,   $uptypes ))   // in_array -- 检查数组中是否存在某个值
 //检查文件类型

 {
    
echo   " 文件类型不符! " . $file [ " type " ];
    
exit ;  
 }
 
 
if ( ! file_exists ( $destination_folder ))   // file_exists -- 检查文件或目录是否存在
   mkdir ( $destination_folder );
  
 
$filename = $file [ " tmp_name " ];
 
$image_size   =   getimagesize ( $filename );  
 
$pinfo = pathinfo ( $file [ " name " ]); // pathinfo -- 返回文件路径的信息
  $ftype = $pinfo [ " extension " ]; // extension表示后缀,例如:gif jpg
  $destination   =   $destination_folder . time () . " . " . $ftype ;
 
if  ( file_exists ( $destination &&   $overwrite   !=   true )
 { 
            
echo   " 同名文件已经存在了 "
            
exit ;
    }
    
    
if ( ! move_uploaded_file  ( $filename ,   $destination )) // move_uploaded_file -- 将上传的文件移动到新位置
    {
       
echo   " 移动文件出错 "
            
exit
    }

 
$pinfo = pathinfo ( $destination );
 
$fname = $pinfo [ " basename " ];
 
echo   "  <font color=red>已经成功上传</font><br>文件名:  <font color=blue> " . $destination_folder . $fname . " </font><br> "
 
echo   "  宽度: " . $image_size [ 0 ];
 
echo   "  长度: " . $image_size [ 1 ];
 
echo   " <br> 大小: " . $file [ " size " ] . "  bytes " ;

$WM   =   new  WaterMark( $destination , ' CB ' , false , true );
$WM -> WM_Letter  =   ' RCGAME.CN ' ;
$WM -> WM_FontCol  =   ' #FFFFFF ' ;
$WM -> WM_ImagePath  =   ' Water.png ' ;
$WM -> WM_LimitParam  =   array ( 100 , 51 , false );
$WM -> ExtrudeWaterMark();

 
if ( $imgpreview == 1 )
 {
      
  
echo   " <br>图片预览:<br> " ;
  
echo   " <img src=" " . $destination . " " width= " . ( $image_size [ 0 ] * $imgpreviewsize ) . "  height= " . ( $image_size [ 1 ] * $imgpreviewsize );
  
echo   "  alt="图片预览: 文件名: " . $destination . " 上传时间:"> " ;
 }

?>
</ body >
</ html >

 


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/325983
推荐阅读
相关标签
  

闽ICP备14008679号