个人技术作品 - PHP 验证码生成类

cls.ValidateNumber.php 
<?php
/**
 * @copyright Crimson Civilization Spread LTD.Co Release
 * @version 1.7.0 BATE 3 , Build 3021
 * @name CMS.IMGCODE.PHP
 * @author YanJingNing
 * 
 * 使用实例
    $Img = new ImgCode(80,40);
    $Img->OnlyKey = false;
    $Img->OnlyKeyWay = 1;
    $Img->OnlyKeySeed = 'sha1';
    $Img->Border = true;
    $Img->DotNoise = true;
    $Img->DotNoisePix = 200;
    $Img->LineNoise = true;
    $Img->LineNoisePix = 10;
    $Img->arlFont = array('1.TTF');
    $Img->Build();
 
*/
class ImgCode
{
    
/**
     * 设置背景图片
     
*/
    
public $BackGround = false;
    
    
/**
     * $BackColor 设定背景颜色设定
     
*/
    
public $BackColor = '#FFFFFF';
    
    
/**
     * 唯一验证码技术 $OnlyKeyWay = 0 为全部小写 , 1全部大写 ,2为大小写
     * $OnlyKeySeed = 'MD5' 为MD5方式加密 sha1 为 sha1方式加密;
     
*/
    
public $OnlyKey = false;
    
public $OnlyKeyWay = 0;
    
public $OnlyKeySeed = 'MD5';
    
    
/**
     * 验证码输出加密 , 防止利用验证码漏洞 , 验证端必须用同样方法验证加密
     * 加密方法: md5加密字符串后,倒转字符串,再使用sha1加密字符串
     * 
     * @abstract sha1(strrev(md5(string)))
     
*/
    
public $KeyOutPutEncrypt = false;
    
    
/**
     * 输出控制 0 输出原文 , 1 输出密文 , 2 输出一个原文 + 密文的数组
     
*/
    
public $OutPutWay = 0;
    
    
/**
     * $SID 保存当前SESSIONID
     
*/
    
public $SID = '';
    
public $OutPutText = '';
    
    
/**
     * $arlFont 字体库保存数组
     
*/
    
public $arlFont = array('1.ttf','2.ttf','3.ttf','4.ttf');
    
    
/**
     * $Image 保存当前图象信息
     
*/
    
private $Image = '';
    
    
/**
     * 干扰信息出现在字体信息上 false 为底,true 为顶
     
*/
    
public $NoisePosition = false;
    
    
/**
     * $FontSize 保存字体大小,如果在构造类前已重设字体大小,此默认值无效
     * $BuildLen 生产验证码的长度
     
*/
    
public $FontSize = 0;
    
public $BuildLen = 4;
    
    
/**
     * $ImgWidth 验证码图片的宽度
     * $ImgHeight 验证码图片的高度
     
*/
    
public $ImgWidth = 100;
    
public $ImgHeight = 30;
    
    
/**
     * $DotNoise 为 true 开启
     * $DotNoiseWay 为 auto 则自动混色干扰,为颜色函数,则固色干扰
     * $DotNoiseCol_r/_b/_g 为混色RBG的范围 array(下限,上限) , $DotNoiseWay 为 auto 时有效
     * $DotNoisePix 像索化点状干扰数量
     
*/
    
public $DotNoise = false;
    
public $DotNoiseWay = 'auto';
    
public $DotNoiseCol_r = array(100,255);
    
public $DotNoiseCol_b = array(100,255);
    
public $DotNoiseCol_g = array(100,255);
    
public $DotNoisePix = 300;
    
    
/**
     * $LineNoise 为 true 开启
     * $LineNoiseWay 为 auto 则自动混色干扰,为颜色函数,则固色干扰
     * $LineNoiseCol_r/_b/_g 为混色RBG的范围 array(下限,上限) , $LineNoiseWay 为 auto 时有效
     * $LineNoisePix 像索化点状干扰数量
     
*/
    
public $LineNoise = false;
    
public $LineNoiseWay = 'auto';
    
public $LineNoiseCol_r = array(0,255);
    
public $LineNoiseCol_b = array(0,255);
    
public $LineNoiseCol_g = array(0,255);
    
public $LineNoisePix = 300;
    
    
/**
     * $Border 为 true 开启
     * $BorderWay 为 auto 则自动混色边框,为颜色函数,则固色边框
     * $BorderCol_r/_b/_g 为混色RBG的范围 array(下限,上限) , $BorderWay 为 auto 时有效
     
*/
    
public $Border = false;
    
public $BorderWay = 'auto';
    
public $BorderCol_r = array(0,150);
    
public $BorderCol_b = array(0,150);
    
public $BorderCol_g = array(0,150);
    
    
    
/**
     * 设置验证码内容颜色变化比例
     
*/
    
public $RandTextWay = 'auto';
    
public $RandTextCol_r = array(0,150);
    
public $RandTextCol_b = array(0,150);
    
public $RandTextCol_g = array(0,150);
    
    
/**
     * 设置验证码内容字体大小不同变动比例
     
*/
    
public $FontSizeFloat = 10;
    
    
/**
     * 设置字体基本点
     
*/
    
public $FontBE = 15;
    
    
/**
     * 微调字体偏向位置 数越大,越向右;数越小,越向左
     
*/
    
public $FontBX = 5;
    
    
/**
     * 设置字体倾斜度保险位置
     
*/
    
public $FontLean = 18;
    
    
/**
     * 微调字体大小
     
*/
    
public $Toothful = 2;
    
    
/**
     * 创建图象方式 false 为 普通模式, true 为 真彩色
     
*/
    
public $VerilyTine = true;
    
    
/**
     * $Gather 验证码内容数据集设定项
     * 0 为大写字母
     * 1 为小写字母
     * 2 为纯数字
     * 3 为特殊符号
     
*/
    
public $Gather = 0;
    
public $GatherTeam = array(
    
0=>array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'),
    
1=>array('a','b','c','d','e','f','g','h','i','j','j','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'),
    
2=>array('0','1','2','3','4','5','6','7','8','9'));
    
    
/**
     * 构造函数
     *
     * @param int $img_w
     * @param int $img_h
     * @param int $f_size
     * @param int $b_len
     * @return ImgCode
     
*/
    
public function ImgCode($img_w = 100,$img_h = 30,$f_size = 0,$bg = false,$b_len = 4)
    {
        
if(!$bg)
        {
            
$this->ImgWidth = $img_w;    // 宽度赋值
            $this->ImgHeight = $img_h;   // 高度赋值
        } else {
            
if(!file_exists($bg))die('Find not BackGround File 1');
            
$ImgSize = GetImageSize($bg);
            
$this->ImgWidth = $ImgSize[0];    // 宽度赋值
            $this->ImgHeight = $ImgSize[1];   // 高度赋值
        }
        
        
$this->BuildLen = $b_len;     // 验证码长度赋值

        
/* 字体大小计算,如果 $f_size 为 0 时,则默认图片宽度除验证码长度 , 否则自定字体大小 */
        
if($f_size !== 0)
        {
            
$this->FontSize = $f_size;
        } 
else {
            
$this->FontSize = $img_w / $b_len;
        }
        
        
/* 开始 SESSION 生产 */
        
session_start();
        @
session_unregister("SESSION_KEY"); // 若之前已存在一个 SESSION 会话,将其关闭
        @session_unregister("ENCRYPT_KEY"); // 
        
        
session_register("ENCRYPT_KEY");
        
session_register("SESSION_KEY");    // 注册 SESSION_KEY
        
        
$this->SID = session_id();            // 给予 SESSIONID
        if(!session_is_registered("SESSION_KEY"))
        {
            
die("session registered lost");
        }
    }
    
    
/**
     * 产生验证码
     
*/
    
public function Build()
    {
        
/* 保存验证码 */
        
$m_Cade = '';
        
        
/* 字体微调 */
        
$this->FontSize = $this->FontSize - $this->Toothful;
        
        
/* 创建一副图片 */
        
if($this->BackGround)
        {
            
if(!file_exists($this->BackGround))die('Find not BackGround File 2');
            
$ImgSize = GetImageSize($this->BackGround);
            
$this->ImgWidth = $ImgSize[0];    // 宽度赋值
            $this->ImgHeight = $ImgSize[1];   // 高度赋值
            
            
switch($ImgSize[2])
            {
                
case 1:
                    
$this->Image = @imagecreatefromgif($this->BackGround);
                      
break;
                
case 2:
                    
$this->Image = @imagecreatefromjpeg($this->BackGround);
                      
break;
                
case 3:
                    
$this->Image = @imagecreatefrompng($this->BackGround);
                      
break;
            }
        } 
else {
            
if(!$VerilyTine)
            {
                
$this->Image = @imagecreate($this->ImgWidth,$this->ImgHeight);
            } 
else {
                
$this->Image = @imagecreatetruecolor($this->ImgWidth,$this->ImgHeight);
            }
        }
        
        
if(!$this->Image)die("Cannot Initialize new GD image stream");  // 是否创建成功
        if(!$this->BackGround)
        {
            
$FunBackColor = $this->FetchColor($this->BackColor);
            imageFilledRectangle(
$this->Image,0,0,$this->ImgWidth,$this->ImgHeight,$FunBackColor); // 创建一个矩型并填充
        }
        
        
// 如果字体超出高度则重设字体大小等于高度 - 2 , 防止超出图形区
        if($this->FontSize > $this->ImgHeight) $this->FontSize = $this->ImgHeight;
        
        
// 设置字体生成的左边界
        $FunLeft = ($this->ImgWidth - $this->BuildLen * ($this->FontSize + $this->FontSize / 10)) / $this->FontSize;
        
        
// 干扰信息出现在底部的判断
        if($this->NoisePosition == false)$this->setNoise();
        
        
for($i = 0;$i < $this->BuildLen;$i++)
        {
            
if(!$this->OnlyKey)
            {
                
$UseKey = array_reverse($this->GatherTeam[$this->Gather]);   // 如果为内定内容数据集,确定使用内容数据集,并反转数组
                $RandText = $UseKey[mt_rand(0,count($UseKey- 1)];  // 扰乱计算本循环的字符
                $m_Cade .= $RandText;  // 保存验证码信息
            } else {
                
// 是否区分大小写生成唯一验证码序列
                /* 唯一验证码解释,将截得 microtime() 函数的微秒数,使用MD5加密,
                    再把加密串分割为数组,rank来随机挑选 
*/
                
if($this->OnlyKeyWay == 0)
                {
                    
if($this->OnlyKeySeed == 'MD5')  // 加密方式判断
                    {
                        
$Rand = str_split(strtolower(md5(microtime())));
                    } 
else {
                        
$Rand = str_split(strtolower(sha1(microtime())));
                    }
                } 
else {
                    
if($this->OnlyKeySeed == 'MD5')  // 加密方式判断
                    {
                        
$Rand = str_split(strtoupper(md5(microtime())));
                    } 
else {
                        
$Rand = str_split(strtoupper(sha1(microtime())));
                    }
                }
                
                
$Rand = array_reverse($Rand);
                
$RandText = $Rand[mt_rand(0,count($Rand- 1)];
                
$m_Cade .= $RandText;  // 保存验证码信息
            }
            
            
// 设置字体颜色
            if($this->RandTextWay == 'auto')
            {
                
$FunFontColor = imageColorAllocate($this->Image, mt_rand($this->RandTextCol_r[0], $this->RandTextCol_r[1]), 
                                                                  
mt_rand($this->RandTextCol_b[0], $this->RandTextCol_b[1]), 
                                                                  
mt_rand($this->RandTextCol_g[0], $this->RandTextCol_g[1]));
            } 
else {
                
$FunFontColor = $this->FetchColor($this->RandTextWay);
            }
            
            
            
// 设置随机字体信息
            $FontType = $this->arlFont[mt_rand(0,count($this->arlFont) - 1)];
            
            
// 设置循环每个字符时的不同大小
            $RandSize = mt_rand($this->FontSize - $this->FontSize / $this->FontSizeFloat,  // 下限
                                $this->FontSize + $this->FontSize / $this->FontSizeFloat); // 上限
            
            // 设置字体基本点 x (左右控制)

            $Location_x = $FunLeft + ($i * $this->FontSize + $this->FontSize / $this->FontBE) + $this->FontBX;
            
            
// 设置字体基本点 y (上下控制)
            $Location_y = mt_rand($this->FontSize - $this->FontSize / $this->FontBE, 
                                  
$this->FontSize + $this->FontSize / $this->FontBE);
                                  
            imagettftext(
$this->Image, $RandSize, mt_rand(-$this->FontLean,$this->FontLean), $Location_x, $Location_y, 
                         
$FunFontColor, $FontType, iconv('gb2312','UTF-8',$RandText));
        }
        
        imageantialias(
$this->Image,true);
        
        
$_SESSION["SESSION_ID"= $this->SID;
        
        
if($this->OutPutWay == 0)
        {
            
$_SESSION["SESSION_KEY"= $m_Cade;
            
$this->OutPutText = $m_Cade;
        } 
else if($this->OutPutWay == 1) {
            
$EnText = sha1(strrev(md5($m_Cade)));
            
$_SESSION["ENCRYPT_KEY"= $EnText;
            
$this->OutPutText = sha1(strrev(md5($m_Cade)));
        } 
else {
            
$EnText = sha1(strrev(md5($m_Cade)));
            
$_SESSION["ENCRYPT_KEY"= $EnText;
            
$_SESSION["SESSION_KEY"= $m_Cade;
            
$this->OutPutText = array(0=>$m_Cade,1=>$EnText);
        }
        
        
// 干扰信息出现在第一层的判断
        if($this->NoisePosition == true)$this->setNoise();
        
        
header("Content-type: image/png");
        imagePng(
$this->Image);
        imagedestroy(
$this->Image);
    }
    
    
private function setNoise()
    {
        
// 安排边框
        $this->setBorser();
        
// 安排点性干扰
        $this->setDotNoise();
        
// 安排线性干扰
        $this->setLineNoise();
    }
    
    
/**
     * 设置边框
     * 
     * @return booleam
     
*/
    
private function setBorser()
    {
        
if($this->Border)
        {
            
if($this->BorderWay == 'auto')
            {
                
$BorderColor = imageColorAllocate($this->Image, mt_rand($this->BorderCol_r[0], $this->BorderCol_r[1]), 
                                                                
mt_rand($this->BorderCol_b[0], $this->BorderCol_b[1]), 
                                                                
mt_rand($this->BorderCol_g[0], $this->BorderCol_g[1]));
                imageRectangle(
$this->Image, 0, 0, $this->ImgWidth - 1, $this->ImgHeight - 1, $BorderColor);
            } 
else {
                
$BorderColor = $this->FetchColor($this->BorderWay);
                imageRectangle(
$this->Image, 0, 0, $this->ImgWidth - 1, $this->ImgHeight - 1, $BorderColor);
            }
            
return true;
        } 
else {
            
return false;
        }
    }
    
    
    
/**
     * 设置点状干扰
     *
     * @return booleam
     
*/
    
private function setDotNoise()
    {
        
if($this->DotNoise)
        {
            
if($this->DotNoiseWay == 'auto')
            {
                
for($i = 0$i < $this->DotNoisePix; $i++){
                    
$randColor = imageColorAllocate($this->Image, mt_rand($this->DotNoiseCol_r[0], $this->DotNoiseCol_r[1]), 
                                                                  
mt_rand($this->DotNoiseCol_b[0], $this->DotNoiseCol_b[1]), 
                                                                  
mt_rand($this->DotNoiseCol_g[0], $this->DotNoiseCol_g[1]));  
                    imageSetPixel(
$this->Image, mt_rand(0, $this->ImgWidth), mt_rand(0, $this->ImgHeight), $randColor);
                }
            } 
else {
                
$DotColor = $this->FetchColor($this->DotNoiseWay);
                
for($i = 0$i < $this->DotNoisePix; $i++){
                    imageSetPixel(
$this->Image, mt_rand(0, $this->ImgWidth), mt_rand(0, $this->ImgHeight), $DotColor);
                }
            }
            
return true;
        } 
else {
            
return false;
        }
    }
    
    
/**
     * 安排线性干扰
     
*/
    
private function setLineNoise()
    {
        
if($this->LineNoise)
        {
            
if($this->LineNoiseWay == 'auto')
            {
                
for($i=0;$i < $this->LineNoisePix;$i++)
                {
                    
$LineColor = imageColorAllocate($this->Image, mt_rand($this->LineNoiseCol_r[0], $this->LineNoiseCol_r[1]), 
                                                                    
mt_rand($this->LineNoiseCol_b[0], $this->LineNoiseCol_b[1]), 
                                                                    
mt_rand($this->LineNoiseCol_g[0], $this->LineNoiseCol_g[1]));
                    imageline(
$this->Image,mt_rand(0,$this->ImgWidth),mt_rand(0,$this->ImgHeight),
                                              
mt_rand(0,$this->ImgWidth),mt_rand(0,$this->ImgHeight),$LineColor);
                }
            } 
else {
                
$LineColor = $this->FetchColor($this->LineNoiseWay);
                
for($i=0;$i < $this->LineNoisePix;$i++)
                {
                    imageline(
$this->Image,mt_rand(0,$this->ImgWidth),mt_rand(0,$this->ImgHeight),
                                              
mt_rand(0,$this->ImgWidth),mt_rand(0,$this->ImgHeight),$LineColor);
                }
            }
        }
    }
    
    
/**
     * 取色,十进值转二进制函数
     *
     * @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->Image, $r, $b, $g);
        
return $Color;
    }
}
?>

必须设置好,所需要的字体文件及其他资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值