QrcodeUtil.php
1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace app\ht\helpers;
use Yii;
use common\helpers\QrcodeHelper;
use QRcode;
use function file_exists;
use function mkdir;
class QrcodeUtil
{
const ROOT_DIR = './download/qrcode/'; // 二维码根目录(批次生成和压缩包导出存放的根目录)
const PREVIEW_DIR = '/download/qrcode/preview/'; // 二维码临时预览
const DOWNLOAD_DIR = '/download/qrcode/download/'; // 二维码下载
const CACHE_FILE_DAYS = 7; // 二维码临时文件缓存天数(超过天数的文件允许被清除)
/**
* 生成预览二维码
*/
public static function previewQrcodeImage($filename,$qrcodeNumber)
{
include Yii::getAlias('@QrcodeLib');
// 确保文件夹已创建
$path = Yii::getAlias('@webroot') . self::PREVIEW_DIR;
$file = $path . $filename;
$baseAssetsUrl = Yii::$app->request->baseUrl;
$img_path = $baseAssetsUrl . self::PREVIEW_DIR . $filename;
if (file_exists($file)) {
return $img_path;
}
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
$qrcode = new QRcode();
$qrcodeUrl = QrcodeHelper::getQrcodeUrl($qrcodeNumber);
$qrcode::png($qrcodeUrl, $file, "H", 7, 2);
return $img_path;
}
}