PHP做验证码

5-18 4,035 views

点击下载字体文件

<?php 

//PHP验证码

class Code{
    //图像资源
    private $img;
    //宽度
    private $width;
    //高度
    private $height;
    //背景颜色
    private $bgColor;
    //字体大小
    private $fontSize;
    //验证码长度
    private $codeLen;
    //字体文件
    private $fontFile;
    //验证码种子
    private $seed;

    public function __construct($width=150,$height=40,$codeLen=4,$fontSize=8,$bgColor='#ffffff',$seed='1234567890qwertyuiopasdklzxcvbnm'){
        //宽度
        $this->width = $width;
        //高度
        $this->height = $height;
        //背景色
        $this->bgColor = $bgColor;
        //字体大小
        $this->fontSize = $fontSize;
        //验证码长度
        $this->codeLen = $codeLen;
        //字体文件
        $this->fontFile = "./font.ttf";
        //种子
        $this->seed = $seed;
    }
    /**
     * 显示验证码
     */
    public function show(){
        //1.发送头部
        header('Content-type:image/png');
        //2.创建画布,填充画布
        $this->createBg();
        //3.写字
        $this->write();
        //4.干扰
        $this->makeTrouble();
        //5.输出
        imagepng($this->img);
        //6.销毁
        imagedestroy($this->img);
    }

    /**
     * 创建干扰
     */
    private function makeTrouble(){
        for ($i=0; $i < 10; $i++) {
            //随机颜色
            $color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); 
            //线
            imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width),mt_rand(0, $this->height), $color);
        }
    }

    /**
     * 创建画布
     */
    private function createBg(){
        $img = imagecreatetruecolor($this->width, $this->height);
        //把16进制颜色(#ffffff)转为10进制颜色(能被imagefill使用)
        $bgColor = hexdec($this->bgColor);
        imagefill($img, 0, 0, $bgColor);
        $this->img = $img;
    }

    /**
     * 写字
     */
    private function write(){
        for ($i=0; $i < $this->codeLen; $i++) {
            //x坐标
            $x = $i * ($this->width / $this->codeLen) + 10;
            $y = ($this->height + $this->fontSize) / 2;
            //随机颜色
            $color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            //具体字
            $text = $this->seed[mt_rand(0, strlen($this->seed) - 1)];
            //写字 
            imagettftext($this->img, $this->fontSize, mt_rand(-45, 45), $x, $y, $color, $this->fontFile, $text);
        }

    }

}

//显示验证码
//第一个参数传验证码宽度 第二个参数传验证码的高度 
//第三个参数传验证码个数 第四个传验证码字体大小 
//第五个参数传字体颜色 第六个传验证码种子
//参数不传就是默认
$code = new Code(150,40,4,30);
$code->show();

?>

解决 laravel-admin between datetime 假如数据库是时间戳int类型无法筛选。

laravel-admin默认的between->datetime(),查询默认是datetime类型,但是假如数据库是时间戳类型就会报错,又不想改底层文件的话可以试试加自定义筛选功能...

阅读全文

php解析英文语句,自动分解。

参考:https://www.php.net/manual/en/function.str-split.php 最近碰到一个问题,客户的英文地址太长,超出接口api字段长度,所以需要解析下语句分解发送。 ...

阅读全文

记录一个laravel-excel导出表格值为0导出excel显示空的解决方法。

最近在使用laravel-excel导出表格的时候,发现假如字段值为0的情况下,导出的excel中直接显示为空,找到一个方法解决,如下. 在laravel-excel的config配置中...

阅读全文

1 条评论

回复 zhouquan 取消回复