分享一个验证码类

news/2024/7/4 0:53:43 标签: class, 函数
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">
class="language-php"><?php

namespace App\Tool\Validate;

//验证码类
class ValidateCode
{
    private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
    private $code;//验证码
    private $codelen = 4;//验证码长度
    private $width = 130;//宽度
    private $height = 50;//高度
    private $img;//图形资源句柄
    private $font;//指定的字体
    private $fontsize = 20;//指定字体大小
    private $fontcolor;//指定字体颜色

    //构造方法初始化
    public function __construct()
    {
        $this->font = public_path() . '/font/Elephant.ttf';//注意字体路径要写对,否则显示不了图片
        $this->createCode();
    }
    //生成随机码
    private function createCode()
    {
        $_len = strlen($this->charset) - 1;
        for ($i = 0;$i < $this->codelen;++$i) {
            $this->code .= $this->charset[mt_rand(0, $_len)];
        }
    }
    //生成背景
    private function createBg()
    {
        $this->img = imagecreatetruecolor($this->width, $this->height);
        $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
        imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
    }
    //生成文字
    private function createFont()
    {
        $_x = $this->width / $this->codelen;
        for ($i = 0;$i < $this->codelen;++$i) {
            $this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
            imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
        }
    }
    //生成线条、雪花
    private function createLine()
    {
      //线条
      for ($i = 0;$i < 6;++$i) {
          $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
          imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
      }
      //雪花
      for ($i = 0;$i < 100;++$i) {
          $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
          imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
      }
    }
    //输出
    private function outPut()
    {
        header('Content-type:image/png');
        imagepng($this->img);
        imagedestroy($this->img);
    }
    //对外生成
    public function doimg()
    {
        $this->createBg();
        $this->createLine();
        $this->createFont();
        $this->outPut();
    }
    //获取验证码
    public function getCode()
    {
        return strtolower($this->code);
    }
}



调用方法:
class="language-php">public function create()
{
	$validateCode = new ValidateCode();
	request()->session()->put('validate_code', $validateCode->getCode());
	return $validateCode->doimg();
} 



http://www.niftyadmin.cn/n/1429135.html

相关文章

几个自定义的函数,不知道什么时候就可能用到了。。。。

/*** curl发送post请求* access public* param array $url (必须)请求的地址* param array $post_data (可选)要发送的post数组* return data 目标返回值*/ function request_post($url, $post_data array()) {if (empty($url) || empty($post_data))retu…

trait的使用心得

PHP trait 特性在 Laravel 中的使用个人心得 trait 是在PHP5.4中为了方便代码复用的一种实现方式&#xff0c;但目前我在看的的PHP项目中较少看的有程序员去主动使用这个实现方式&#xff0c;在laravel中有很多 trait 的使用&#xff0c;关于trait 在 laravel 的使用请参看 Lar…

docker 报错:driver failed programming external connectivity on endpoint lnmp (2f647b8aba729787bf34f6a8

早上来到公司打开本地lnmp环境,发现docker不能正常启动&#xff0c;报错&#xff1a;driver failed programming external connectivity on endpoint lnmp&#xff0c;后面英文还显示是mysql端口映射到物理机的问题 最后发现就是本地mysql服务自动打开了&#xff0c;与docker的…

Windows下进入到docker容器镜像内部

&#xff08;1&#xff09;查看容器id&#xff08;container ID&#xff09; docker ps -a &#xff08;2&#xff09;进入到容器内部 docker exec -it 6b654059476e /bin/bash PS:-it参数后面是容器的id &#xff08;3&#xff09;进入到mysql数据库成功 mysql -uroot -p,由于…

laravel使用ElasticSearch进行搜索

1.安装elasticsearch和ik插件 ①elasticsearch集成包&#xff08;包括ik中文插件&#xff09;安装地址&#xff1a;https://github.com/medcl/elasticsearch-rtf ②测试安装 启动elasticSearch&#xff1a;bin/elasticSearch -d ③测试是否安装成功 127.0.0.1:92002.ElasticSea…

PHP面向对象多态

PHP在面型对象中&#xff0c;多态&#xff1a;在面向对象中,指某种对象实例的不同表现形态&#xff0c;一个类中的各个子类 举个栗子&#xff1a; <?phpabstract class Person {public abstract function run(); }class FatMan extends Person {public function run(){ech…

PHP面向对象之简单工厂模式

简单工厂模式把创建对象的过程进行了封装&#xff0c;体现了PHP的面向对象&#xff0c;隔离封装的特点&#xff0c;这样就使客户端无论怎么调用&#xff0c;他都是只知道接口&#xff0c;但是并不清楚后端接口内部程序的具体实现&#xff0c;省去了客户端创建对象的过程&#x…

tp5上传图片并生成缩略图

<?php namespace app\common\controller; use app\common\model\Goods; class Tools {public static function upload_goods_img($whereName"", $width"", $height""){// 打开图片的相对路径$imgpath config(img_path);// 绝对路径$imgRoot…