基于PHP GD库的验证码类

PHP 2014年08月29日 , ,

在上网的过程中,用户常常要和各种验证码打交道。验证码主要是在为了识别人与机器,防止恶意的注册、暴力破解、交易的认证等方面。当然这只是识别路上的一小环而已。今天furzoom使用PHP的GD库实现了一个验证码verification code的类。

PHP实现验证码类

主要利用php的GD库的绘图API来实现,基本功能实现了,还有一些需要改进的地方。

/* VerificationCode.php - VerificationCode class
 * Copyright (C) 2013-2014 FURZOOM.COM
 *
 * @file VerificationCode.php
 * @brief VerificationCode class
 * @details Generated picture type of verification code, that contains numbers and letters. The verification code stored in session encrypted by md5.
 * @author Yan Ma
 * @version 1.0
 * @date 2014-08
 * @history
 * @website http://furzoom.com/
 */

// class VerificationCode
class VerificationCode {
    private $width; // image width
    private $height; // image height
    private $codeNum; // verification code length
    private $checkCode; // verification code string
    private $image; // verification code image resource
    private $sessionKey; // session variable
    const charWidth = 10; // min width of character

    // construct function
    function __construct($width = 60, $height = 20, $codeNum = 4, $sessionKey = "vc"){
        $this->width = $width;
        $this->height = $height;
        $this->codeNum = $codeNum;
        $this->sessionKey = $sessionKey;

        // check for image width and height
        if($width < ($codeNum * self::charWidth + 10)){
            $this->width = $codeNum * self::charWidth + 10;
        }
        if($height < 20){
        $this->height = 20;
        }
    }

    // output verification code image
    function showImage(){
        // create image
        $this->getCreateImage();
        // draw verification code
        $this->outputText();
        // draw disturb elements
        $this->setDisturbColor();
        // output image
        $this->outputImage();
    }

    // create image
    private function getCreateImage(){
        // create image resource
        $this->image = imagecreate($this->width, $this->height);
        // background color
        $back = imagecolorallocate($this->image, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255));
        // fill background
        imagefill($this->image, 0, 0, $back);
    }

    // draw disturb elements
    private function setDisturbColor(){
        // draw disturb dots
        for($i=0;$i<=100;$i++){
            $color = imagecolorallocate($this->image, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));
            imagesetpixel($this->image, mt_rand(1, $this->width-2), mt_rand(1, $this->height-2), $color);
        }

        // draw disturb lines
        for($i=0; $i<5;$i++){
            $color = imagecolorallocate($this->image, mt_rand(120, 220), mt_rand(120, 220), mt_rand(120, 220));
            imagearc($this->image, mt_rand(-10, $this->width), mt_rand(-10, $this->height), mt_rand(10, $this->width), mt_rand(10, $this->height), 55, 44, $color);
        }

        // border color
        $border = imagecolorallocate($this->image, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50));
        // draw border
        imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
    }

    // generate and draw verification code
    private function outputText(){
        $str = '23456789ABCDEFGHJKLMNPQSTUVWXYZ';
        // generate verification code
        for($i=0;$i<$this->codeNum;$i++){
            $this->checkCode .=$str[mt_rand(0, strlen($str) - 1)];
        }

        // draw verification code
        for($i=0;$i<strlen($this->checkCode);$i++){
            $color = imagecolorallocate($this->image, mt_rand(0,200), mt_rand(0,200), mt_rand(0,200));
            $x = floor(($this->width)/($this->codeNum)) * $i +3;
            $y = mt_rand(0, $this->height - 15);
            imagechar($this->image, 5, $x, $y, $this->checkCode[$i], $color);
        }
    }

    // output image
    private function outputImage(){
        if(imagetypes() & IMG_PNG){
            header('Content-type:image/png');
            imagepng($this->image);
        } else if(imagetypes() & IMG_JPEG){
            header('Content-type:image/jpeg');
            imagejpeg($this->image);
        } else if(imagetypes() & IMG_GIF){
            header('Content->type:image/gif');
            imagegif($this->image);
        } else{
            die("PHP failed to output image");
        }

        // save session
        if(!isset($_SESSION)){
            session_start();
        }

        $_SESSION[$this->sessionKey] = md5(strtolower($this->checkCode));
    }

    function __destruct(){
        imagedestroy($this->image);
    }
}// end VerificationCode class

VerificationCode类的使用

$image = new VerificationCode(100,30,4);
$image->showImage(); 

完整示例请查看PHP实现验证码类测试
代码的说明已经在注释中写的很清楚了。欢迎与大家交流。

如无特别说明,本站文章皆为原创,若要转载,务必请注明以下原文信息:
日志标题:《基于PHP GD库的验证码类》
日志链接:http://furzoom.com/php-gd-verification-class/
博客名称:枫竹梦

发表评论

插入图片

NOTICE1:请申请gravatar头像,没有头像的评论可能不会被回复!

回到顶部