PHP生成图形验证码

<? php

/* *
 * @name CheckNum_Class.php
 * @abstract 生成验证码类,验证码由随机字母和随机数组成
 * @author 感染源
 * @copyright 2007
 
*/

class  CheckNum_Class
{
    
// 初始验证码数组
     private   $VerifyStr   =   array (
    
' Q ' , ' W ' , ' E ' , ' E ' , ' T ' , ' Y ' , ' U ' , ' I ' , ' O ' , ' P ' , ' A ' , ' S ' , ' D ' , ' F ' ,
    
' G ' , ' H ' , ' J ' , ' K ' , ' L ' , ' Z ' , ' X ' , ' C ' , ' V ' , ' B ' , ' N ' , ' M ' , ' 0 ' , ' 1 ' ,
    
' 2 ' , ' 3 ' , ' 4 ' , ' 5 ' , ' 6 ' , ' 7 ' , ' 8 ' , ' 9 ' );
    
private   $ArrayId ;                 // 初始验证码数组下标
     private   $VerifyStrLen ;             // 初始验证码长度
     private   $R_VerifyStr   =   '' ;         // 返回验证码
     private   $R_VerifyStrLen ;         // 返回验证码的长度
    
    
private   $Im ;                     // 图像操作标识
     private   $BgColor ;                 // 图像背景颜色
     private   $ImWidth ;                 // 图像宽度
     private   $ImHeight ;                 // 图像高度
    
    
private   $FontSize ;                 // 验证码大小
     private   $FontX ;                     // 验证码显示起始X位置
     private   $FontY ;                     // 验证码显示起始Y位置
     private   $FontColor ;                 // 验证码颜色
    
    
private   $PixColor ;                 // 干扰符颜色
     private   $PixX ;                     // 干扰符位置
     private   $PixY ;                     // 干扰符位置
     private   $PixAmount ;                 // 干扰度
    
    
    
function  __construct( $pImSet )
    {
        
/*
         * @var array $pImSet
        
*/
        
        
$this -> ImWidth    =   $pImSet [ ' w ' ];     //
         $this -> ImHeight   =   $pImSet [ ' h ' ];     //
         $this -> PixAmount  =   $pImSet [ ' p ' ];      // 干扰度
        
        
$this -> Im  =  imagecreate( $this -> ImWidth , $this -> ImHeight);     // 建立画布
        
        //设置背景色

         $this -> BgColor  =  imagecolorallocate( $this -> Im , $pImSet [ ' r ' ] , $pImSet [ ' g ' ] , $pImSet [ ' b ' ]);
    }
    
    
    
function  __destruct()
    {
        imagedestroy(
$this -> Im);
    }
    
    
    
function  Show_Verify( $pFont )
    {
        
/*
         * @var array $pFont
        
*/
        
        
header ( " Content-type:image/png " );
        
$this -> FontSize   =   $pFont [ ' s ' ];         // 验证码文字大小
        
        //设置验证码显示颜色

         $this -> FontColor  =  imagecolorallocate( $this -> Im , $pFont [ ' r ' ] , $pFont [ ' g ' ] , $pFont [ ' b ' ]);
        
$this -> R_VerifyStrLen  =   $pFont [ ' l ' ]; // 验证码显示位数
         $this -> VerifyStrLen    =   count ( $this -> VerifyStr);
        
        
// 验证码输入位置
         $this -> FontX     =   round ( $this -> FontSize / 2 );
        
$this -> FontY     =   round ( $this -> FontSize / 2 );
        
        
// 生成验证码
         for ( $i = 0 ; $i < $this -> R_VerifyStrLen; $i ++ )
        {
            
$this -> ArrayId  =   rand ( 0 , ( $this -> VerifyStrLen - 1 ));
            
$this -> R_VerifyStr  .=   $this -> VerifyStr[ $this -> ArrayId];
        }
        
        
// 在图片显示验证码
        imagestring( $this -> Im , $this -> FontSize , $this -> FontX , $this -> FontY , $this -> R_VerifyStr , $this -> FontColor);
        
        
        
// 生成干扰点
         for ( $j = 0 ; $j < $this -> PixAmount; $j ++ )
        {
            
$this -> PixX      =   rand ( 0 , $this -> ImWidth);
            
$this -> PixY      =   rand ( 0 , $this -> ImHeight);
            
$this -> PixColor  =  imagecolorallocate( $this -> Im , rand ( 0 , 255 ) , rand ( 0 , 255 ) , rand ( 0 , 255 ));
            imagesetpixel(
$this -> Im , $this -> PixX , $this -> PixY , $this -> PixColor);
        }
        
        imagepng(
$this -> Im);         // 输入验证码图片
    }
    
    
    
function  Get_Verify()
    {
        
return   $this -> R_VerifyStr;     // 返回验证码
    }
}

// TestCode
$pImset   =   array ( ' w ' => 55 , ' h ' => 25 , ' p ' => 140 , ' r ' => 130 , ' g ' => 160 , ' b ' => 180 );
$img   =   new  CheckNum_Class( $pImset );
$pFont   =   array ( ' s ' => 10 , ' r ' => 0 , ' g ' => 0 , ' b ' => 255 , ' l ' => 5 );
$img -> Show_Verify( $pFont );
$img   =   NULL ;
?>
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值