wangxinke155 2021-10-18 11:28 采纳率: 0%
浏览 77

php composer dump-autoload 报错 does not comply with psr-4 autoloading standard.

<?php
namespace App\Org\code;
use Session;

class Code{

    //资源
    private $img;
    //画布宽度
    private $width=100;
    //画布高度
    private $height=30;
    //背景颜色
    private $bgColor='#ffffff';
    //验证码
    private $code;
    //验证码的随机种子
    private $codeStr='23456789abcdefghjkmnpqrstuvwsyz';
    //验证码长度
    private $codeLen=4;
    //验证码字体
    private $font;
    //验证码字体大小
    private $fontSize=16;
    //验证码字体颜色
    private $fontColor='';

    public function __construct() {
    }

    //创建验证码
    public function make()
    {
        if(empty($this->font))
        {
            $this->font = __DIR__.'/consola.ttf';
        }
        $this->create();//生成验证码
        header("Content-type:image/png");
        imagepng($this->img);
        imagedestroy($this->img);
        //exit;
    }

    //设置字体文件
    public function font($font)
    {
        $this->font= $font;
        return $this;
    }

    //设置文字大小
    public function fontSize($fontSize)
    {
        $this->fontSize=$fontSize;
        return $this;
    }

    //设置字体颜色
    public function fontColor($fontColor)
    {
        $this->fontColor = $fontColor;
        return $this;
    }

    //验证码数量
    public function num($num)
    {
        $this->codeLen=$num;
        return $this;
    }

    //设置宽度
    public function width($width)
    {
        $this->width = $width;
        return $this;
    }

    //设置高度
    public function height($height)
    {
        $this->height = $height;
        return $this;
    }

    //设置背景颜色
    public function background($color)
    {
        $this->bgColor = $color;
        return $this;
    }

    //返回验证码
    public function get() {
//        return $_SESSION['code'];
        return session('code');
    }

    //生成验证码
    private function createCode() {
        $code = '';
        for ($i = 0; $i < $this->codeLen; $i++) {
            $code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)];
        }
        $this->code = strtoupper($code);
        // dd($this->code);
        Session::put('code',$this->code);
        // session(['code' => $this->code]);
//        $_SESSION['code'] = $this->code;
    }

    //建画布
    private function create() {
        if (!$this->checkGD())
            return false;
        $w = $this->width;
        $h = $this->height;
        $bgColor = $this->bgColor;
        $img = imagecreatetruecolor($w, $h);
        $bgColor = imagecolorallocate($img, hexdec(substr($bgColor, 1, 2)), hexdec(substr($bgColor, 3, 2)), hexdec(substr($bgColor, 5, 2)));
        imagefill($img, 0, 0, $bgColor);
        $this->img = $img;
        $this->createLine();
        $this->createFont();
        $this->createPix();
        $this->createRec();
    }

    //画线
    private function createLine(){
        $w = $this->width;
        $h = $this->height;
        $line_color = "#dcdcdc";
        $color = imagecolorallocate($this->img, hexdec(substr($line_color, 1, 2)), hexdec(substr($line_color, 3, 2)), hexdec(substr($line_color, 5, 2)));
        $l = $h/5;
        for($i=1;$i<$l;$i++){
            $step =$i*5;
            imageline($this->img, 0, $step, $w,$step, $color);
        }
        $l= $w/10;
        for($i=1;$i<$l;$i++){
            $step =$i*10;
            imageline($this->img, $step, 0, $step,$h, $color);
        }
    }

    //画矩形边框
    private function createRec() {
        //imagerectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->fontColor);
    }

    //写入验证码文字
    private function createFont() {
        $this->createCode();
        $color = $this->fontColor;
        if (!empty($color)) {
            $fontColor = imagecolorallocate($this->img, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
        }
        $x = ($this->width - 10) / $this->codeLen;
        for ($i = 0; $i < $this->codeLen; $i++) {
            if (empty($color)) {
                $fontColor = imagecolorallocate($this->img, mt_rand(50, 155), mt_rand(50, 155), mt_rand(50, 155));
            }
            imagettftext($this->img, $this->fontSize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontColor, $this->font, $this->code [$i]);
        }
        $this->fontColor = $fontColor;
    }

    //画线
    private function createPix() {
        $pix_color = $this->fontColor;
        for ($i = 0; $i < 50; $i++) {
            imagesetpixel($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
        }

        for ($i = 0; $i < 2; $i++) {
            imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
        }
        //画圆弧
        for ($i = 0; $i < 1; $i++) {
            // 设置画线宽度
            imagearc($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height)
                , mt_rand(0, 160), mt_rand(0, 200), $pix_color);
        }
        imagesetthickness($this->img, 1);
    }

    //验证GD库
    private function checkGD() {
        return extension_loaded('gd') && function_exists("imagepng");
    }

}

img

  • 写回答

1条回答 默认 最新

  • hbxncjs 2021-10-18 12:10
    关注

    啥意思

    评论

报告相同问题?

问题事件

  • 创建了问题 10月18日

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)