1、下载:

composer require endroid/qr-code

php: >=7.2

2、新建Qrcodes.php二维码生成类

use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Response\QrCodeResponse;
use think\Controller;/*** 二维码生成类* Class Qrcodes* @package app\api\controller*/
class Qrcodes extends Controller
{protected $param;public function __construct(Request $request = null){parent::__construct($request);$this->param = ['setSize' => 300,//设置二维码尺寸'setWriterByName' => 'png','setMargin' => 5,//设置二维码边界'setEncoding' => 'UTF-8',//设置编码'setErrorCorrectionLevel' => ErrorCorrectionLevel::HIGH(),//设置容错等级 等级越高识别度越高'setLabelStatus' => false,//是否开启二维码标题'setLabel' => '这是二维码标题',//设置二维码标题'setLogoPathStatus' => false,//是否开启二维码中间logo'setLogoPath' => 'logo.png',//设置二维码中间logo'setLogoSizeW' => 100,//设置二维码中间logo宽度'setLogoSizeH' => 100,//设置二维码中间logo高度];}//生成二维码 --直接输出二维码public function returnQrcodeImg($content = '这是二维码内容',$param = []){// Create a basic QR code创建一个基本的二维码$qrCode = new QrCode($content);//设置二维码尺寸$qrCode->setSize(isset($param['setSize']) ? $param['setSize'] : $this->param['setSize']);// Set advanced options设置高级选项$qrCode->setWriterByName(isset($param['setWriterByName']) ? $param['setWriterByName'] : $this->param['setWriterByName']);//设置二维码边界$qrCode->setMargin(isset($param['setMargin']) ? $param['setMargin'] : $this->param['setMargin']);//设置编码$qrCode->setEncoding(isset($param['setEncoding']) ? $param['setEncoding'] : $this->param['setEncoding']);//设置容错等级 等级越高识别度越高$qrCode->setErrorCorrectionLevel(isset($param['setErrorCorrectionLevel']) ? $param['setErrorCorrectionLevel'] : $this->param['setErrorCorrectionLevel']);//设置二维码颜色$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);//设置二维码背景颜色$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);//设置二维码标题 在图片下方显示文字//$qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER());if (isset($param['setLabelStatus']) ? $param['setLabelStatus'] : $this->param['setLabelStatus']) {$qrCode->setLabel(isset($param['setLabel']) ? $param['setLabel'] : $this->param['setLabel'], 16, null, LabelAlignment::CENTER());}//设置二维码中间logoif (isset($param['setLogoPathStatus']) ? $param['setLogoPathStatus'] : $this->param['setLogoPathStatus']) {$logo = isset($param['setLogoPath']) ? $param['setLogoPath'] : $this->param['setLogoPath'];$qrCode->setLogoPath(__DIR__.'/../../../public/static/images/'.$logo);//logo尺寸$setLogoSizeW = isset($param['setLogoSizeW']) ? $param['setLogoSizeW'] : $this->param['setLogoSizeW'];$setLogoSizeH = isset($param['setLogoSizeH']) ? $param['setLogoSizeH'] : $this->param['setLogoSizeH'];$qrCode->setLogoSize($setLogoSizeW, $setLogoSizeH);}//设置二维码内边距,true表示有内边距  false表示没有$qrCode->setRoundBlockSize(true);//启用内置的验证读取器(默认情况下禁用)$qrCode->setValidateResult(false);//排除xml声明$qrCode->setWriterOptions(['exclude_xml_declaration' => true]);// Directly output the QR code直接输出二维码header('Content-Type: '.$qrCode->getContentType());echo $qrCode->writeString();// Create a response object创建一个响应对象//$response = new QrCodeResponse($qrCode);}//生成二维码--保存图片并返回路径public function returnQrcodePath($content = '这是二维码内容',$filename = 'qrlogo.png',$param = []){// Create a basic QR code创建一个基本的二维码$qrCode = new QrCode($content);//设置二维码尺寸$qrCode->setSize(isset($param['setSize']) ? $param['setSize'] : $this->param['setSize']);// Set advanced options设置高级选项$qrCode->setWriterByName(isset($param['setWriterByName']) ? $param['setWriterByName'] : $this->param['setWriterByName']);//设置二维码边界$qrCode->setMargin(isset($param['setMargin']) ? $param['setMargin'] : $this->param['setMargin']);//设置编码$qrCode->setEncoding(isset($param['setEncoding']) ? $param['setEncoding'] : $this->param['setEncoding']);//设置容错等级 等级越高识别度越高$qrCode->setErrorCorrectionLevel(isset($param['setErrorCorrectionLevel']) ? $param['setErrorCorrectionLevel'] : $this->param['setErrorCorrectionLevel']);//设置二维码颜色$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);//设置二维码背景颜色$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);//设置二维码标题 在图片下方显示文字//$qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER());if (isset($param['setLabelStatus']) ? $param['setLabelStatus'] : $this->param['setLabelStatus']) {$qrCode->setLabel(isset($param['setLabel']) ? $param['setLabel'] : $this->param['setLabel'], 16, null, LabelAlignment::CENTER());}//设置二维码中间logoif (isset($param['setLogoPathStatus']) ? $param['setLogoPathStatus'] : $this->param['setLogoPathStatus']) {$logo = isset($param['setLogoPath']) ? $param['setLogoPath'] : $this->param['setLogoPath'];$qrCode->setLogoPath(__DIR__.'/../../../public/static/images/'.$logo);//logo尺寸$setLogoSizeW = isset($param['setLogoSizeW']) ? $param['setLogoSizeW'] : $this->param['setLogoSizeW'];$setLogoSizeH = isset($param['setLogoSizeH']) ? $param['setLogoSizeH'] : $this->param['setLogoSizeH'];$qrCode->setLogoSize($setLogoSizeW, $setLogoSizeH);}//设置二维码内边距,true表示有内边距  false表示没有$qrCode->setRoundBlockSize(true);//启用内置的验证读取器(默认情况下禁用)$qrCode->setValidateResult(false);//排除xml声明$qrCode->setWriterOptions(['exclude_xml_declaration' => true]);// Directly output the QR code直接输出二维码//header('Content-Type: '.$qrCode->getContentType());//echo $qrCode->writeString();// Save it to a file保存到文件中$qrCode->writeFile(__DIR__.'/../../../public/qrcode/'.$filename);return $_SERVER['SERVER_ADDR'].'/public/qrcode/'.$filename;// Create a response object创建一个响应对象//$response = new QrCodeResponse($qrCode);}
}

3、调用:

HTML中:

<img src="{:url('Qrcodes/outputQrcodeImg')}" alt="">

//直接输出二维码
    public function outputQrcodeImg()
    {
        $qrcode = new \app\common\controller\Qrcodes();
        $param = [
            'setLogoPathStatus' => true,
        ];
        //直接输出二维码
        $qrcode->returnQrcodeImg('https://www.baidu.com/',$param);
    }

//返回二维码路径
    public function returnQrcodePath()
    {
        $qrcode = new \app\common\controller\Qrcodes();
        $param = [
            'setLogoPathStatus' => true,
        ];
        $filename = mt_rand(1,100000);
        //返回二维码路径
        $path = $qrcode->returnQrcodePath('https://www.baidu.com/',$filename.'.png',$param);
        return $path;

}

qr-code 生成二维码相关推荐

  1. QR Code生成二维码快速入门

    1.QR Code的介绍 QR Code,是由Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大.可靠性高.可表示汉字及图象多种文字信息.保密防伪性 ...

  2. URL 转为QR code(二维码)

    总结几种把网页url转为二维码的方法. 1. Chrome浏览器 最快的一种方法就是用chrome自带的QR code分享. 这种方法的缺点就是不能自定义二维码的格式(颜色.logo之类的),都是默认 ...

  3. C++/QT生成二维码和扫瞄二维码

    C++生成二维码和扫瞄二维码 一. 创建工程项目 二 .QRCODE库 三. 添加生成二维码库 四. 生成二维码和复制二维码实现 五. 添加扫码二维码库 六. 扫码二维码代码实现 七. 测试 八. 打 ...

  4. SpringCloud 生成二维码技术

    文章目录 SpringCloud 生成二维码技术 1)引入依赖 2)编写生成二维码方法 3)测试方法 SpringCloud 生成二维码技术 ZXing是一个开源的,用Java编写的多格式的1D / ...

  5. 使用IDEA创建一个通过url链接生成二维码的java程序|自动生成二维码

    平时使用了那么多的二维码,今天我们自己做一个二维码.今天刚安装了IDEA,学会了用法,就来通过IDEA做一个生成二维码的程序. 首先新建一个项目 设置项目名称 点击Next之后,出现下图所示页面:   ...

  6. thinkphp6项目使用phpqrcode生成二维码

    首先下载phpqrcode扩展文件 phpqrcode下载链接 下载完之后把文件解压到根目录下的vendor中 需要用到的参数: 第一个参数$text,就是代码里的URL网址参数, 第二个参数$out ...

  7. thinkphp6下使用phpqrcode生成二维码

    系!首先你要有果只库! 实在稳矛到你就@我,我发比你.放在thinkphp6的这个文件夹内extend.然后控制器写只方法就得爹. /*** 生成二维码* @return string*/public ...

  8. 用ABAP 生成二维码 QR Code

    除了使用我的这篇blogStep by step to create QRCode in ABAP Webdynpro提到的使用ABAP webdynpro生成二维码之外,也可以通过使用二维码在线生成 ...

  9. java通过QR生成二维码 (QRCodeUtils工具类)(HUTOOL生成二维码)

    1.简述 二维码生成关键是QR,QR码属于矩阵式二维码中的一个种类,由DENSO(日本电装)公司开发,由JIS和ISO将其标准化 详情简介请看 2.工具类 package com.lingxu.bas ...

  10. C#中生成二维码(QR码)与读取二维码内容

    使用开源类库ZXing.dll可以在C#中生成二维码和解析二维码为指定的字符串(含url) 新建windows窗体应用程序QRCodeDemo,.net 4.5,将默认的Form1重命名为FormQu ...

最新文章

  1. 2018年,关于CAP最精彩的小故事!
  2. TiDB 源码阅读系列文章(十九)tikv-client(下)
  3. JSP中response,session,cookie,application的作用及区别
  4. VTK:可编程字形过滤器用法实战
  5. 抓取html文件swf,如何把网页上的flash动画保存为swf格式文件(缓存提取)
  6. 前后端分离 基于SpringBoot+mybatis+Java Mail+Lay UI+Ajax 的班级管理系统(webapp +安卓应用)
  7. Froala editor 2.9.5 使用
  8. panasonic打印机驱动下载
  9. 收集整理的一些windows好用的工具(持续更新)
  10. 春招面经总结(获携程Offer)
  11. 联想z5 Android 9.0,联想Z5开启Android 9.0内测,新增人脸识别!
  12. 恶意软件分析实战02-分析3个恶意程序
  13. 孩子,你在家乡还好妈
  14. 摘抄“学习笔记”时想到的
  15. 电脑重启后自带键盘失灵而外接键盘有用的一种情况
  16. 逐梦C++之五:string类型
  17. 移动端:M站和APP的区别
  18. Qt图片绘图类 QPixmap
  19. 12【C语言 趣味算法】存钱问题(四层for循环,if判断)
  20. 【机器学习笔记】可解释机器学习-学习笔记 Interpretable Machine Learning (Deep Learning)

热门文章

  1. 投身物联网创业6个月,我是如何选择 IoT 物联网平台的?
  2. 方正ES2007快速开发平台 Java版本
  3. 一页纸搞定项目管理及操作步骤(文末附可编辑模板)PMP项目管理可用
  4. 01.NetLogo-命令学习
  5. 线性位置检测 - CHI612替代TLE4997、MLX90251
  6. SSCOM 串口校验
  7. 快递助手 V1.2.2.0
  8. 浅谈ERP实施应用的流程步骤
  9. 用java建立多项式logit模型_请问logit模型和logistic模型是不是一样的?
  10. Spring入门到精通:系列文章 - 导读