123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
- namespace app\common\service;
- use Endroid\QrCode\Color\Color;
- use Endroid\QrCode\Encoding\Encoding;
- use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
- use Endroid\QrCode\QrCode;
- use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
- use Endroid\QrCode\Writer\PngWriter;
- class QrcodeService
- {
- public static function getQrcode($file_name, $content, $size = 150, $margin = 20)
- {
- //创建文件夹
- $path = public_path() . '/storage/qrcode';
- if (!file_exists($path)) {
- mkdir($path);
- }
- $file_path = $path . '/' . $file_name;
- if (!file_exists($file_path)) {
- $writer = new PngWriter();
- $qrCode = QrCode::create($content)
- ->setEncoding(new Encoding('UTF-8'))
- ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
- ->setSize($size)//大小
- ->setMargin($margin)//边距
- ->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
- ->setForegroundColor(new Color(0, 0, 0))
- ->setBackgroundColor(new Color(255, 255, 255));
- $result = $writer->write($qrCode);
- $result->getString();
- $result->saveToFile($file_path);
- }
- $file_url = url('/') . '/storage/qrcode/' . $file_name;
- return $file_url;
- }
- }
|