randNumber.class.php 3.93 KB
<?php
class Util_Randnumber_Randnumber
{
    private $mWidth;
    private $mHeight;
    private $mNum;
    private $mImageType = '';
    private $mRandNumber; //产生的验证码
    private $mImage; //验证码图片
    private $mDisturbPix; //干扰像素

    /**
     * 初始化函数
     */
    public function __construct($imageType = 'gif',$width = '50', $height = '20', $num = '4') 
    {
        $this->mWidth = $width;
        $this->mHeight = $height;
        $this->mNum = $num;
        $this->mImageType = $imageType;
        $this->createCode();
    }
    
    /**
     * 产生验证码
     */
    public function createCode() 
    {
        $this->mRandNumber = strtoupper(substr(md5(rand()), 0, $this->mNum));
    }
    
    
    /**
     * 获取验证码
     * 
     * @return string
     */
    public function getCode()
    {
        return $this->mRandNumber;
    }
    
    /**
     * 产生图片
     */
    private function createImage() 
    {
        $this->mImage = @imagecreate($this->mWidth, $this->mHeight);
        //底色颜色
        $bg = imagecolorallocate($this->mImage, 255, 255, 255);
        //边框颜色
        $border = imagecolorallocate($this->mImage, 0, 0, 0);
        //设置底色
        imagefilledrectangle($this->mImage, 0, 0, $this->mWidth - 1, $this->mHeight - 1, $bg); 
        //设置边框
        imagerectangle($this->mImage, 0, 0, $this->mWidth - 1, $this->mHeight - 1, $border); 
    }
    
    /**
     * 设置干扰像素
     */
    private function setDisturbPix() 
    {
        for($i = 0; $i <= 100; $i++) 
        {
            $this->mDisturbPix = imagecolorallocate($this->mImage, rand(0, 255), rand(0, 255), rand(0, 255));
            imagesetpixel($this->mImage, rand(2, $this->mWidth), rand(2, $this->mHeight), $this->mDisturbPix);
        }
    }
    
    /**
     * 往图片上写验证码
     */
    private function writeCkCodeToImage() 
    {
        for($i = 0; $i <= $this->mNum; $i++) 
        {
            $bg_color = imagecolorallocate($this->mImage, rand(0, 255), rand(0, 128), rand(0, 255));
            $x = floor($this->mWidth / $this->mNum) * $i + 3;
            $y = rand(0, $this->mHeight - 15);
            $font = dirname(__FILE__).'/arial.ttf'; 
            imagettftext($this->mImage,15,0, 5, 23,$bg_color, $font,$this->mRandNumber);
            //imagechar($this->mImage, rand(5, 8), $x, $y, $this->mRandNumber[$i], $bg_color);
        }
        //增加图片的掺杂特性 
        srand((double)microtime()*1000000);
        $linenums = mt_rand(20, 30); 
        for($i=0; $i <= $linenums ; $i++) 
        { 
            $linecolor = imagecolorallocate($this->mImage, 0, 0, 0); 
            $linex = mt_rand(5, $this->mWidth-5); 
            $liney = mt_rand(5, $this->mHeight/2); 
            imageline($this->mImage, $linex, $liney, $linex + mt_rand(0, 10) - 2, $liney + mt_rand(0, 10) - 2, $linecolor); 
        }
        header("Pragma:no-cache\r\n");
        header("Cache-Control:no-cache\r\n");
        header("Expires:0\r\n");
        /* 输出图片格式为 gif,jpg ,png*/
        header("Content-type: image/".$this->mImageType);
        $ImageFun = 'image'.$this->mImageType;
        $ImageFun($this->mImage);
        ImageDestroy($this->mImage);
    }
    
   /**
    * 设置验证码
   */
    public function setRandNumber($randNumber)
    {
        $this->mRandNumber = $randNumber;
    }
    
    public function getNum()
    {
        return $this->Num;
    }
    
    /**
     * 生成验证码图片
     */
    public function expImg() 
    {
        if(empty($this->mRandNumber))
        {
            $this->createCode(); //产生验证码
        }
        $this->createImage(); //产生图片
        $this->setDisturbPix(); //设置干扰像素
        $this->writeCkCodeToImage(); //往图片上写验证码
    }
    
    /**
     * 析构函数
     */
    public function __destruct()
     {
        unset($this->mWidth, $this->mHeight,$this->mImageType,$this->mNum);
     }
}
?>