Service.php
2.2 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace addons\qrcode\library;
use Endroid\QrCode\ErrorCorrectionLevel;
class Service
{
/**
* 生成二维码
* @param $params
* @return \Endroid\QrCode\QrCode
* @throws \Endroid\QrCode\Exception\InvalidPathException
*/
public static function qrcode($params)
{
$config = get_addon_config('qrcode');
$params = is_array($params) ? $params : [$params];
$params = array_merge($config, $params);
$params['labelfontpath'] = isset($params['labelfontpath']) && is_file($params['labelfontpath']) ? $params['labelfontpath'] : ROOT_PATH . 'public' . $config['labelfontpath'];
$params['logopath'] = isset($params['logopath']) && is_file($params['logopath']) ? $params['logopath'] : ROOT_PATH . 'public' . $config['logopath'];
// 前景色
list($r, $g, $b) = sscanf($params['foreground'], "#%02x%02x%02x");
$foregroundcolor = ['r' => $r, 'g' => $g, 'b' => $b];
// 背景色
list($r, $g, $b) = sscanf($params['background'], "#%02x%02x%02x");
$backgroundcolor = ['r' => $r, 'g' => $g, 'b' => $b];
// 创建实例
$qrCode = new \Endroid\QrCode\QrCode($params['text']);
$qrCode->setSize($params['size']);
// 高级选项
$qrCode->setWriterByName($params['format']);
$qrCode->setMargin($params['padding']);
$qrCode->setEncoding('UTF-8');
$qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel($params['errorlevel']));
$qrCode->setForegroundColor($foregroundcolor);
$qrCode->setBackgroundColor($backgroundcolor);
// 设置标签
if (isset($params['label']) && $params['label']) {
$qrCode->setLabel($params['label'], $params['labelfontsize'], $params['labelfontpath'], $params['labelalignment']);
}
// 设置Logo
if (isset($params['logo']) && $params['logo']) {
$qrCode->setLogoPath($params['logopath']);
$qrCode->setLogoSize($params['logosize'], $params['logosize']);
}
$qrCode->setRoundBlockSize(true);
$qrCode->setValidateResult(false);
$qrCode->setWriterOptions(['exclude_xml_declaration' => true]);
return $qrCode;
}
}