一、使用composer下载扩展包
composer require endroid/qr-code
二、实现代码
use Endroid\QrCode\Color\Color; use Endroid\QrCode\Encoding\Encoding; use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow; use Endroid\QrCode\QrCode; use Endroid\QrCode\Label\Label; use Endroid\QrCode\Logo\Logo; use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Endroid\QrCode\Writer\PngWriter; class QR_Code { private $logo = null; private $label = null; private $size = 200; public function __construct() { // } /** * 设置二维码LOGO */ public function setLogo($logo_path) { $this->logo = Logo::create($logo_path) //logo的照片路径 ->setResizeToWidth(20); //logo的大小 } /** * 设置二维码下方文字 */ public function setLabel($text) { $this->label = Label::create($text) //二维码下面的文字 ->setTextColor(new Color(0, 0, 0)); //文字的颜色 } /** * 生成二维码 * @param string $url [链接] * @return string [type] [返回图片path] */ public function create(string $url) { $writer = new PngWriter(); $qrCode = QrCode::create($url)//跳转的url地址 ->setEncoding(new Encoding('UTF-8')) //设置编码格式 ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow()) //设置纠错级别为低 ->setSize($this->size) //大小 ->setMargin(20) //边距 ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin()) //设置圆轮大小边距 ->setForegroundColor(new Color(0, 0, 0)) //前景色 ->setBackgroundColor(new Color(255, 255, 255)); //背景色 $result = $writer->write($qrCode, $this->logo, $this->label); $result->getString(); $file_path = runtime_path() . "/temp/qrcode/"; if (!is_dir($file_path)) { mkdir($file_path, 0755, true); } $qrcode = time() . mt_rand(0, 9999). '.png'; $result->saveToFile($file_path . $qrcode); return $file_path . $qrcode; } }
标签:use,webman,new,Endroid,path,QrCode,logo From: https://www.cnblogs.com/liyong2019/p/17272992.html