首页 > php图像处理 > php 验证码图片程序

php 验证码图片程序

<?php
/*
 *文件名:class.safeCode.php
 *类名:safeCode
 *目的:生成web应用时所需的验证码图片
 *当前版本:1.0.2
 *作者:NoAngels
 *联系方式:flare_1023@163.com QQ:82535599 MSN:atizu@hotmail.com
 *更新内容:
 *版本1.0.2
 *1.更新了对linux主机支持,设置GDFONTPATH环境变量,
 * putenv('GDFONTPATH=' . realpath('.'));
 *2.对不支持antialias函数的时候避免采用此方法,避免低版本的GD库
 *版本1.0.1
 *1.更新了绘制指定类型图片,支持类型有:png,jpeg,gif.默认为png(此效果最佳)
 *2.优化了部分代码
*/
class safeCode {
    function __construct() {
        //设置默认safe安全保护码
        $this->__safe = substr(md5(time()) , 0, 4);
    }
    function __destruct() {
        //释放资源
        unset($this->__img, $this->__canvasWidth, $this->__canvasHeight, $this->__codeLength, $this->__spacePerChar, $this->__code, $this->__safe, $this->__background, $this->__borderColor, $this->__colors, $this->__font, $this->__sessionName);
    }
    function setCanvas($width = 200, $height = 60) {
        //设置画布宽度以及高度.有默认值
        $this->__canvasWidth = $width;
        $this->__canvasHeight = $height;
        $this->__img = imagecreatetruecolor($width, $height);
        return;
    }
    function setSafe($char) {
        //设置安全码,不调用系统会自己设置
        $this->__safe = $char;
        return;
    }
    function setCodeLength($num = 8) {
        //设置验证码长度
        $this->__codeLength = $num;
    }
    function setFont($fontName) {
        //设置绘图时所用的字体,字体文件必须与类文件同目录
        $this->__font = $fontName;
        return;
    }
    function setBackground($arg1 = 255, $arg2 = 255, $arg3 = 255) {
        //设置背景颜色
        $this->__background = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
        return;
    }
    function setBorderColor($arg1 = 128, $arg2 = 128, $arg3 = 128) {
        //设置边框颜色
        $this->__borderColor = imagecolorallocate($this->__img, $arg1, $arg2, $arg3);
    }
    function setFontColor($arr) {
        //设置绘图时所用颜色,参数为颜色集合数组
        foreach ($arr as $color) {
            $this->__colors[] = imagecolorallocate($this->__img, $color[0], $color[1], $color[2]);
        }
        return;
    }
    function setSession($sessionName = 'safeCode') {
        //设置session变量名,默认为$_SESSION['safeCode']
        $this->__sessionName = $sessionName;
        //$_SESSION[$sessionName] = $this->__code;
        return;
    }
    function display($type = 'png') {
        //显示图片,可指定显示类型,目前支持png(默认),jpeg,gif
        $this->__setSpacePerChar();
        imagefilledrectangle($this->__img, 1, 1, $this->__canvasWidth - 2, $this->__canvasHeight - 2, $this->__background);
        imagerectangle($this->__img, 0, 0, $this->__canvasWidth - 1, $this->__canvasHeight - 1, $this->__borderColor);
        $this->__drawText($this->__colors, $this->__getCode() , $this->__font);
        //修正linux以及低版本的GD库问题
        if (function_exists(imageantialias)) {
            imageantialias($this->__img, true);
        }
        $_SESSION[$this->__sessionName] = $this->__code;
        switch ($type) {
            case 'png':
                header('Content-type:image/png');
                imagepng($this->__img);
            case 'jpeg':
                header('Content-type:image/jpeg');
                imagejpeg($this->__img, '', 75);
            case 'gif':
                header('Content-type:image/gif');
                imagegif($this->__img);
            default:
                header('Content-type:image/png');
                imagepng($this->__img);
        }
        imagegif($this->__img);
    }
    private function __setSpacePerChar() {
        //私有函数,设置字符间隔
        $this->__spacePerChar = $this->__canvasWidth / $this->__codeLength;
        return;
    }
    private function __getCode() {
        //获取验证码
        $this->__code = substr(md5(time() . $this->__safe) , rand(0, 24) , $this->__codeLength);
        return $this->__code;
    }
    private function __drawText($colors, $code, $fontName) {
        //开始绘图
        //设置GBFONTPATH为当前目录,不然linux环境下会报错
        putenv('GDFONTPATH=' . realpath('.'));
        for ($i = 0; $i < strlen($code); $i++) {
            $color = $colors[$i % count($colors) ];
            imagettftext($this->__img, 24 + rand(0, 8) , -20 + rand(0, 40) , ($i + 0.3) * $this->__spacePerChar, $this->__canvasHeight - 20 + rand(0, 20) , $color, $fontName, $code{$i});
        }
        //绘制随机实线
        for ($i = 0; $i < 400; $i++) {
            $x1 = rand(5, $this->__canvasWidth - 5);
            $y1 = rand(5, $this->__canvasHeight - 5);
            $x2 = $x1 - 4 + rand(0, 8);
            $y2 = $y1 - 4 + rand(0, 8);
            imageline($this->__img, $x1, $y1, $x2, $y2, $this->__colors[rand(0, count($this->__colors) - 1) ]);
        }
        return;
    } //开源代码phprm.com
    private $__img = NULL;
    private $__canvasWidth = 120;
    private $__canvasHeight = 80;
    private $__codeLength = 8;
    private $__spacePerChar = 0;
    private $__code = 12345678;
    private $__safe = 2008;
    private $__background = NULL;
    private $__borderColor = NULL;
    private $__colors = array();
    private $__font = 'arial.ttf';
    private $__sessionName = '';
}
?>

使用方法,代码如下:

<?php
//测试文件
include_once ('class.safeCode.php');
session_start();
$safeCode = new safeCode;
$safeCode->setCanvas(200, 50);
$safeCode->setCodeLength(8);
$safeCode->setSafe('2008'); //设置安全码,不设置有默认值,动态生成
$safeCode->setBackground();
$safeCode->setBorderColor();
$safeCode->setFont('arial.ttf'); //设置字体.必须同目录,字体文件
$colors = array(array(128, 64, 192), array(192, 64, 128), array(108, 192, 64));
$safeCode->setFontColor($colors);
$safeCode->setSession();
$safeCode->display('jpeg');


本文地址:http://www.phprm.com/tuxiang/fs4506.html

转载随意,但请附上文章地址:-)

标签:php验证码程序 php验证码图片

相关文章

发表留言