文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>php验证码

php验证码

时间:2007-06-07  来源:liuxingyuyuni


?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();
    }
?>


相关阅读 更多 +
排行榜 更多 +
jojo的奇妙冒险手机版下载

jojo的奇妙冒险手机版下载

飞行射击 下载
雪糕工厂 v9.87.13.02 安卓版

雪糕工厂 v9.87.13.02 安卓版

休闲益智 下载
雪糕工厂 v9.87.13.02 安卓版

雪糕工厂 v9.87.13.02 安卓版

休闲益智 下载