|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class QRCode |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource QRCode.php |
|
6
|
|
|
* @created 26.11.2015 |
|
7
|
|
|
* @package chillerlan\QRCode |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2015 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\QRCode; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\QRCode\Output\QROutputInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @link https://github.com/kazuhikoarase/qrcode-generator/tree/master/php |
|
19
|
|
|
* @link http://www.thonky.com/qr-code-tutorial/ |
|
20
|
|
|
*/ |
|
21
|
|
|
class QRCode{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* API constants |
|
25
|
|
|
*/ |
|
26
|
|
|
const OUTPUT_STRING_TEXT = 'txt'; |
|
27
|
|
|
const OUTPUT_STRING_JSON = 'json'; |
|
28
|
|
|
|
|
29
|
|
|
const OUTPUT_MARKUP_HTML = 'html'; |
|
30
|
|
|
const OUTPUT_MARKUP_SVG = 'svg'; |
|
31
|
|
|
# const OUTPUT_MARKUP_XML = 'xml'; // anyone? |
|
32
|
|
|
|
|
33
|
|
|
const OUTPUT_IMAGE_PNG = 'png'; |
|
34
|
|
|
const OUTPUT_IMAGE_JPG = 'jpg'; |
|
35
|
|
|
const OUTPUT_IMAGE_GIF = 'gif'; |
|
36
|
|
|
|
|
37
|
|
|
const ERROR_CORRECT_LEVEL_L = 1; // 7%. |
|
38
|
|
|
const ERROR_CORRECT_LEVEL_M = 0; // 15%. |
|
39
|
|
|
const ERROR_CORRECT_LEVEL_Q = 3; // 25%. |
|
40
|
|
|
const ERROR_CORRECT_LEVEL_H = 2; // 30%. |
|
41
|
|
|
|
|
42
|
|
|
// max bits @ ec level L:07 M:15 Q:25 H:30 % |
|
43
|
|
|
const TYPE_01 = 1; // 152 128 104 72 |
|
44
|
|
|
const TYPE_02 = 2; // 272 224 176 128 |
|
45
|
|
|
const TYPE_03 = 3; // 440 352 272 208 |
|
46
|
|
|
const TYPE_04 = 4; // 640 512 384 288 |
|
47
|
|
|
const TYPE_05 = 5; // 864 688 496 368 |
|
48
|
|
|
const TYPE_06 = 6; // 1088 864 608 480 |
|
49
|
|
|
const TYPE_07 = 7; // 1248 992 704 528 |
|
50
|
|
|
const TYPE_08 = 8; // 1552 1232 880 688 |
|
51
|
|
|
const TYPE_09 = 9; // 1856 1456 1056 800 |
|
52
|
|
|
const TYPE_10 = 10; // 2192 1728 1232 976 |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var int |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $typeNumber; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var int |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $errorCorrectLevel; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var \chillerlan\QRCode\Output\QROutputInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $qrOutputInterface; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $data; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* QRCode constructor. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $data |
|
78
|
|
|
* @param \chillerlan\QRCode\Output\QROutputInterface $output |
|
79
|
|
|
* @param \chillerlan\QRCode\QROptions|null $options |
|
80
|
|
|
*/ |
|
81
|
|
|
public function __construct($data, QROutputInterface $output, QROptions $options = null){ |
|
82
|
|
|
$this->qrOutputInterface = $output; |
|
83
|
|
|
|
|
84
|
|
|
$this->setData($data, $options); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $data |
|
89
|
|
|
* @param \chillerlan\QRCode\QROptions|null $options |
|
90
|
|
|
* |
|
91
|
|
|
* @return \chillerlan\QRCode\QRCode |
|
92
|
|
|
* @throws \chillerlan\QRCode\QRCodeException |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setData(string $data, QROptions $options = null):QRCode { |
|
95
|
|
|
|
|
96
|
|
|
if(!$options instanceof QROptions){ |
|
97
|
|
|
$options = new QROptions; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->data = trim($data); |
|
101
|
|
|
$this->errorCorrectLevel = (int)$options->errorCorrectLevel; |
|
102
|
|
|
$this->typeNumber = (int)$options->typeNumber; |
|
103
|
|
|
|
|
104
|
|
|
if(empty($this->data)){ |
|
105
|
|
|
throw new QRCodeException('No data given.'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if(!in_array($this->errorCorrectLevel, range(0, 3), true)){ |
|
109
|
|
|
throw new QRCodeException('Invalid error correct level: '.$this->errorCorrectLevel); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return mixed |
|
117
|
|
|
*/ |
|
118
|
|
|
public function output(){ |
|
119
|
|
|
$this->qrOutputInterface->setMatrix($this->getRawData()); |
|
120
|
|
|
|
|
121
|
|
|
return $this->qrOutputInterface->dump(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getRawData():array { |
|
128
|
|
|
return (new QRDataGenerator($this->data, $this->typeNumber, $this->errorCorrectLevel))->getRawData(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|