首页 > 作文

PHP 简单面向对象 验证码类(静态实例对象调用)

更新时间:2023-04-07 07:26:24 阅读: 评论:0

没事写了一个简单的面向对象验证码类,可以直接使用(替换一下字体路径)

<?phpclass authcode{    private static $instance = null;     #实例对象    private $width = 120;                #图片宽度    private $height = 40;                #图片高度    private $font = 'font/elephant.ttf'; #字体文件路径    private $fontsize = 14;              #字体大小    private $strlen = 6;                 #字符个数    private $auth_code_str = null;       #验证码结果    private $imgresult = null;           #图片资源    #入口文件 静态方法调用 实例化对象 可用 对象方法调用    public static function img()    {        if (!(lf::$instance instanceof lf)) {            lf::$instance = new lf();        }        return lf::$instance;    }    #随机颜色    private function randomcolor($img = null, $min = 0, $max = 255)    {        $rgb = [];        for ($i = 1; $i <= 3; $i++) {            $rgb[] = str高考卷2021_pad(rand($min, $max), 3, 0, str_pad_left);        }        return imagecolorallocate($img, $rgb[0], $rgb[1], $rgb[2]);    }    #随机字符串    private function randomstr($num = 4)    {        if ($num > 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 = fal)    {        if (!$this->auth_code_str) {          建筑论文  $this->randomstr($this->strlen);        }        if ($codestr !== fal && empty($codestr)) {            $codestr = $this->auth_code_str;        } el if (!empty($codestr) && $codestr !== fal) {            $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);           歌颂母亲 imagetpixel($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';        } el {            $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();*/

  

本文发布于:2023-04-07 07:26:22,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/7c9242628cb0b91de261e6d886db48dd.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP 简单面向对象 验证码类(静态实例对象调用).doc

本文 PDF 下载地址:PHP 简单面向对象 验证码类(静态实例对象调用).pdf

标签:字体大小   验证码   图片   字体
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图