免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 758 | 回复: 0
打印 上一主题 下一主题

php验证码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-07 03:11 |只看该作者 |倒序浏览

?php
    /**
     * @name     验证码类
     * @author     BianJiang
     * @since     2007-06-07
     * @link     liuxingyuyuni.cublog.cn
     * @history
     * */
    class IndentifyCode
    {
        private $img;
        //验证码图像类型
        private $imgType;
        //验证码图片宽度
        private $imgWidth;
        //验证码图片高度
        private $imgHeight;
        //验证码字符串
        private $randChar;
        /**
         * @name     构造函数
         * @access     public
         * @param     Integer    $width         图像宽
         * @param     Integer    $height         图像长
         * @param     String     $imgType         图像类型
         * @param     Integer $randCharLen    字符串长度建议默认
         * @return     None
         * */
        public function __construct($width=60, $height=20, $imgType,$randCharLen=4)
        {
            $this->imgWidth = $width;
            $this->imgHeight = $height;
            $this->setImgType($imgType);
            $this->setRandomCode($randCharLen);
            $this->establishImg();
        }
         /**
         * @name     创建验证码图像
         * @access     private
         * @param     None
         * @return     None
         * */
        private function establishImg()
        {
            $this->img = imagecreate($this->imgWidth, $this->imgHeight);
            $bgColor = imagecolorallocate($this->img, 255, 250, 250);
            $textColor = imagecolorallocate($this->img, 0, 0, 255);
            for($i=0;$i50;$i++)//随机加入干扰象素
            {
             $randColor = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));//需要重构
         imagesetpixel($this->img,rand(1, $this->imgWidth), rand(1, $this->imgHeight), $randColor);
            }
            imagestring($this->img, 5, 12, 3, $this->randChar, $textColor);
        }
        /**
         * @name     设置输出图像的格式
         * @access     private
         * @param     String $type
         * @return     Boolean
         * */
        private function setImgType($type)
        {
            switch ($type)
            {
                case "png":
                    $this->imgType = $type;
                    break;
                case "gif":
                    $this->imgType = $type;
                    break;
                case "jpeg":
                    $this->imgType = $type;
                    break;
                default:
                    throw new Exception("不能确定图片格式");
            }
            return true;
        }
        /**
         * @name     设置随机字符串
         * @access     private
         * @param     Integer $length
         * @return     String    substr($randChar,0, $length)
         * */
        private function setRandomCode($length=4)
        {
            $randChar = str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
            $this->randChar = substr($randChar,0, $length);
            session_start();
            $_SESSION['randChar'] = $this->randChar;
            return $_SESSION['randChar'];
        }
        /**
         * @name     输出验证码图像
         * @access     public
         * @param     None
         * @return     None
         * */
        public function output()
        {
            header("Content-type: image/$this->imgType");
            imagepng($this->img);
            imagedestroy($this->img);
        }   
    }
   
    //Example
    try {
        $img = new IndentifyCode(60,20,'gif',4);
        $img->output();
    }
    catch (Exception $e)
    {
        echo $e->getMessage();
    }
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/10599/showart_316391.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP