endroid/qr-code生成二维码

composer引用包endroid/qr-code生成二维码

实例类QrCodeService.php


namespace App\Services;

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\QrCode;

class QrCodeService
{
    public function makeQrCode( $path , $size = 230, $margin = 30)
    {
        $qrCode = new QrCode($path);
        // 内容区域宽高
        $qrCode->setSize($size);
        // 外边距大小,默认为10
        $qrCode->setMargin($margin);
        // 设置编码
        $qrCode->setEncoding('UTF-8');
        // 设置容错等级 ErrorCorrectionLevel
        $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
        // 设置二维码颜色,默认为黑色
        $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
        // 设置二维码背景色,默认为白色
        $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);

        $fileName = 'qrcode_'.time() . str_random().'.png';
        $qrCode->writeFile(public_path('codes').'/'. $fileName);

        return config('app.url').'/codes/'. $fileName;
    }
}

Buy me a cup of coffee 🙂

觉得对你有帮助,就给我打赏吧,谢谢!

微信赞赏码链接,点击跳转:

endroid/qr-code生成二维码插图

Tags: