使用composer安装和使用endroid/QrCode来生成二维码(4.2.2)
1.使用composer命令安装endroid/QrCode composer require endroid/qr-code 现在版本为4.2.2 2.实现代码
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;
$writer = new PngWriter();
$qrCode = QrCode::create($param['url'])
->setEncoding(new Encoding('UTF-8'))
->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
->setSize(150)
->setMargin(20)
->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
->setForegroundColor(new Color(0, 0, 0))
->setBackgroundColor(new Color(255, 255, 255));
$logo = Logo::create(BASE_PATH.'haha.png')
->setResizeToWidth(5);
$label = Label::create('扫码购买')
->setTextColor(new Color(0, 0, 0));
$result = $writer->write($qrCode, $logo, $label);
$result->getString();
$qrcode=$param['goods_id'].'_'.$param['shop_id'].'_'.mt_rand(111111,999999).'.png';
$result->saveToFile(BASE_PATH.'/storage/qrcode/'.$qrcode);
$dataUri = $result->getDataUri();
3.还不理解可以查看git仓库 地址:https://github.com/endroid/qr-code
|