PHP面向对象简易验证码类如何实现

PHP面向对象简易验证码类如何实现?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!

专注于为中小企业提供网站建设、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业涞源免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

 0) {
            $string = array_merge(range('a', 'z'), range(0, 9), range('A', 'Z'), range(0, 9));
            for ($i = 1; $i <= $num; $i++) {
                shuffle($string);
                $this->auth_code_str .= array_pop($string);
            }
        }
        return $this;
    }
    
     #创建验证码
    public function createAuthCode(&$codeStr = false)
    {
        if (!$this->auth_code_str) {
            $this->randomStr($this->strLen);
        }
        if ($codeStr !== false && empty($codeStr)) {
            $codeStr = $this->auth_code_str;
        } else if (!empty($codeStr) && $codeStr !== false) {
            $this->auth_code_str = $codeStr;
        }
        $this->imgResult = imagecreatetruecolor($this->width, $this->height);
        $background = $this->randomColor($this->imgResult, 200);
        imagefilledrectangle($this->imgResult, 0, 0, $this->width, $this->height, $background);
        $y = ($this->height - $this->fontSize);
        $string = str_split($this->auth_code_str, 1);
        for ($i = 0; $i < count($string); $i++) {
            $frontColor = $this->randomColor($this->imgResult, 0, 200);
            imagefttext($this->imgResult, $this->fontSize, rand(0, 10), ($this->fontSize + 2) * $i + 10, $y, $frontColor, $this->font, $string[$i]);
        }
        return $this;
    }
    
      #生成线
    public function line($line = 3)
    {
        $line = $line ?: 3;
        for ($i = 1; $i <= $line; $i++) {
            $lineColor = $this->randomColor($this->imgResult, 0, 200);
            imageline($this->imgResult, rand(0, $this->width / 5), rand(5, $this->height - 5), rand($this->width / 1.3, $this->width), rand(5, $this->height - 5), $lineColor);
        }
        return $this;
    }
    
    #噪点
    public function pixel($num = 50){
        $num = $num ?: 3;
        for ($i = 1; $i <= $num; $i++) {
            $lineColor = $this->randomColor($this->imgResult, 0, 100);
            imagesetpixel($this->imgResult, rand(0, $this->width), rand(0, $this->height), $lineColor);
        }
        return $this;
    }
    
     #设置大小
    public function size($width = null, $height = null)
    {
        $this->width = $width ?: 120;
        $this->height = $height ?: 40;
        return $this;
    }
    
    #设置字体大小
    public function fontSize($fontsize = 14)
    {
        $this->fontSize = $fontsize ?: 14;
        return $this;
    }
    
    #设置字体
    public function font($file = null)
    {
        if (is_null($file) === true) {
            $this->font = 'font/elephant.ttf';
        } else {
            $this->font = $file;
        }
        return $this;
    }
    
    #设置长度
    public function strlen($num = null)
    {
        $this->strLen = $num ?: 6;
        return $this;
    }
    
    public function display()
    {
        ob_end_flush();
        header("content-type:image/jpeg");
        imagejpeg($this->imgResult, null, 100);
        imagedestroy($this->imgResult);
        exit;
    }
}

#简单调用方法
authCode::img()->createAuthCode()->display();
/*
#指定字符串调用
$string = 'abc123';
authCode::img()->createAuthCode($string)->display();
 
#设置图片大小、字数、字体大小
authCode::img()->strlen(8)->size(300,100)->fontSize(30)->createAuthCode()->display();
 
#添加噪点
authCode::img()->createAuthCode()->line()->pixel()->display();
*/
    ?>

感谢各位的阅读!看完上述内容,你们对PHP面向对象简易验证码类如何实现大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注创新互联行业资讯频道。


本文题目:PHP面向对象简易验证码类如何实现
网页路径:http://pwwzsj.com/article/pjjjpp.html