php的驗證碼類

來源:互聯網
上載者:User

標籤:驗證碼類

class Verify
{
    //寬
    protected $width;
    //高
    protected $height;
    //圖片類型
    protected $imgType;
    //文字類型
    protected $codeType;
    //文字的個數
    protected $num;
    //儲存的驗證碼字串
    protected $verifyCode;
    //儲存驗證碼資源的一個成員屬性
    protected $res;
    
    
    //初始化成員
    //上面這些參數
    public function __construct($width = 100, $height = 50, $imgType = ‘png‘, $codeType = 3, $num = 4)
    {
        $this->width = $width;
        $this->height = $height;
        $this->imgType = $imgType;
        $this->codeType = $codeType;
        $this->num = $num;
        $this->verifyCode = $this->createVerifyCode();
        
    }
    
    protected function  createVerifyCode()
    {
        $string = ‘‘;
        
        switch ($this->codeType) {
            case 1:
                $string = implode(‘‘,array_rand(range(0,9),$this->num));
                break;
            case 2:
                $string = join(‘‘,array_rand(array_flip(range(‘a‘,‘z‘)),4));
                break;
            case 3:
                /*
                for ($i = 0; $i < $this->num; $i++) {
                    $r= mt_rand(0,2);
                    switch ($r) {
                        case 0:
                            $ascii = mt_rand(48,57);
                            break;
                        case 1:
                            $ascii = mt_rand(65,90);
                            break;
                        case 2:
                            $ascii = mt_rand(97,122);
                            break;
                    }
                    $string .= chr($ascii);
                    
                }
                */
                $str = ‘abcdefghijkmnpqrstuvwxzABCDEFGHJKLMNPQRSTUVWXYZ23456789‘;
                $string = substr(str_shuffle($str),0,$this->num);
                break;
        }
        
        return $string;
    }
    
    
    
    //調驗證碼顯示的一個方法 output
    //1.畫圖
    //2.分配顏色(寫兩個成員方法,調的時候直接調對應的成員方法即可)
    //3.背景填充
    //4.畫幹擾點
    //5.畫幹擾線
    //6.寫字
    //7. 輸出類型
    //8. 輸出圖片
    public function outImg()
    {
        $this->createImg();
        $this->fillBgColor();
        $this->fillPix();
        $this->fillArc();
        $this->writeFont();
        $this->output();
    }
    
    protected function output()
    {
        //imagepng
        $func = ‘image‘.$this->imgType;
        $mime = ‘Content-type:image/‘.$this->imgType;
        header($mime);
        $func($this->res);
    }
    
    protected function writeFont()
    {
        for ($i = 0; $i < $this->num; $i++) {
            
            $width = ceil($this->width / $this->num);
            $x = $width * $i;
            $y= mt_rand(5,$this->height - 10);
            $c = $this->verifyCode[$i];
            imagechar($this->res,5,$x,$y,$c,$this->darkColor());
        }
        
    }
    
    protected function fillArc()
    {
        for($i = 0; $i < 10; $i++) {
            imagearc($this->res,
                    mt_rand(10,$this->width - 10),
                    mt_rand(10,$this->height - 10),
                    mt_rand(0,$this->width),
                    mt_rand(0,$this->height),
                    mt_rand(0,180),
                    mt_rand(181,360),
                    $this->lightColor()
                    );
        }
    }
    
    protected function fillPix()
    {
        $num = $this->pixNum();
        for ($i = 0; $i < $num; $i++) {
            
            imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->darkColor());
        }
    }
    
    protected function pixNum()
    {
        $area = ceil(($this->width * $this->height) / 20);
        return $area;
    }
    
    
    protected function fillBgColor()
    {
        imagefill($this->res,0,0,$this->lightColor());
    }
    
    
    protected function lightColor()
    {
        return imagecolorallocate($this->res,
                           mt_rand(130,255),
                           mt_rand(130,255),
                           mt_rand(130,255)
                            );
    }
    
    protected function darkColor()
    {
        return imagecolorallocate($this->res,
                           mt_rand(0,120),
                           mt_rand(0,120),
                           mt_rand(0,120)
                            );
    }
    
    protected function createImg()
    {
        $this->res = imagecreatetruecolor($this->width,$this->height);
    }
    
    
    //9 .銷毀圖片資源
    public function __destruct()
    {
        //imagedestroy($this->res);
    }
    
    //可以做一個魔術方法__get專門用於得到驗證碼字串
    public function __get($key)
    {
        if ($key == ‘verifyCode‘) {
            return $this->$key;
        }
        
        return false;
    }
    
    
}

本文出自 “web開發” 部落格,轉載請與作者聯絡!

php的驗證碼類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.