1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait QROptionsTrait |
4
|
|
|
* |
5
|
|
|
* @filesource QROptionsTrait.php |
6
|
|
|
* @created 10.03.2018 |
7
|
|
|
* @package chillerlan\QRCode |
8
|
|
|
* @author smiley <[email protected]> |
9
|
|
|
* @copyright 2018 smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\QRCode; |
14
|
|
|
|
15
|
|
|
use function array_values, count, in_array, is_array, is_numeric, max, min, sprintf, strtolower; |
16
|
|
|
|
17
|
|
|
trait QROptionsTrait{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* QR Code version number |
21
|
|
|
* |
22
|
|
|
* [1 ... 40] or QRCode::VERSION_AUTO |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $version = QRCode::VERSION_AUTO; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Minimum QR version (if $version = QRCode::VERSION_AUTO) |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $versionMin = 1; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Maximum QR version |
37
|
|
|
* |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
protected $versionMax = 40; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Error correct level |
44
|
|
|
* |
45
|
|
|
* QRCode::ECC_X where X is |
46
|
|
|
* L => 7% |
47
|
|
|
* M => 15% |
48
|
|
|
* Q => 25% |
49
|
|
|
* H => 30% |
50
|
|
|
* |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $eccLevel = QRCode::ECC_L; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Mask Pattern to use |
57
|
|
|
* |
58
|
|
|
* [0...7] or QRCode::MASK_PATTERN_AUTO |
59
|
|
|
* |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
protected $maskPattern = QRCode::MASK_PATTERN_AUTO; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Add a "quiet zone" (margin) according to the QR code spec |
66
|
|
|
* |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
protected $addQuietzone = true; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Size of the quiet zone |
73
|
|
|
* |
74
|
|
|
* internally clamped to [0 ... $moduleCount / 2], defaults to 4 modules |
75
|
|
|
* |
76
|
|
|
* @var int |
77
|
|
|
*/ |
78
|
|
|
protected $quietzoneSize = 4; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Use this to circumvent the data mode detection and force the usage of the given mode. |
82
|
|
|
* valid modes are: Number, AlphaNum, Kanji, Byte |
83
|
|
|
* |
84
|
|
|
* @see https://github.com/chillerlan/php-qrcode/issues/39 |
85
|
|
|
* |
86
|
|
|
* @var string|null |
87
|
|
|
*/ |
88
|
|
|
protected $dataMode = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* QRCode::OUTPUT_MARKUP_XXXX where XXXX = HTML, SVG |
92
|
|
|
* QRCode::OUTPUT_IMAGE_XXX where XXX = PNG, GIF, JPG |
93
|
|
|
* QRCode::OUTPUT_STRING_XXXX where XXXX = TEXT, JSON |
94
|
|
|
* QRCode::OUTPUT_CUSTOM |
95
|
|
|
* |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
protected $outputType = QRCode::OUTPUT_IMAGE_PNG; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM |
102
|
|
|
* |
103
|
|
|
* @var string|null |
104
|
|
|
*/ |
105
|
|
|
protected $outputInterface = null; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* /path/to/cache.file |
109
|
|
|
* |
110
|
|
|
* @var string|null |
111
|
|
|
*/ |
112
|
|
|
protected $cachefile = null; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* newline string [HTML, SVG, TEXT] |
116
|
|
|
* |
117
|
|
|
* @var string |
118
|
|
|
*/ |
119
|
|
|
protected $eol = PHP_EOL; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* size of a QR code pixel [SVG, IMAGE_*] |
123
|
|
|
* HTML -> via CSS |
124
|
|
|
* |
125
|
|
|
* @var int |
126
|
|
|
*/ |
127
|
|
|
protected $scale = 5; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* a common css class |
131
|
|
|
* |
132
|
|
|
* @var string |
133
|
|
|
*/ |
134
|
|
|
protected $cssClass = ''; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* SVG opacity |
138
|
|
|
* |
139
|
|
|
* @var float |
140
|
|
|
*/ |
141
|
|
|
protected $svgOpacity = 1.0; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* anything between <defs> |
145
|
|
|
* |
146
|
|
|
* @see https://developer.mozilla.org/docs/Web/SVG/Element/defs |
147
|
|
|
* |
148
|
|
|
* @var string |
149
|
|
|
*/ |
150
|
|
|
protected $svgDefs = '<style>rect{shape-rendering:crispEdges}</style>'; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* SVG viewBox size. a single integer number which defines width/height of the viewBox attribute. |
154
|
|
|
* |
155
|
|
|
* viewBox="0 0 x x" |
156
|
|
|
* |
157
|
|
|
* @see https://css-tricks.com/scale-svg/#article-header-id-3 |
158
|
|
|
* |
159
|
|
|
* @var int|null |
160
|
|
|
*/ |
161
|
|
|
protected $svgViewBoxSize = null; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* string substitute for dark |
165
|
|
|
* |
166
|
|
|
* @var string |
167
|
|
|
*/ |
168
|
|
|
protected $textDark = '🔴'; |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* string substitute for light |
172
|
|
|
* |
173
|
|
|
* @var string |
174
|
|
|
*/ |
175
|
|
|
protected $textLight = '⭕'; |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* markup substitute for dark (CSS value) |
179
|
|
|
* |
180
|
|
|
* @var string |
181
|
|
|
*/ |
182
|
|
|
protected $markupDark = '#000'; |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* markup substitute for light (CSS value) |
186
|
|
|
* |
187
|
|
|
* @var string |
188
|
|
|
*/ |
189
|
|
|
protected $markupLight = '#fff'; |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* toggle base64 or raw image data |
193
|
|
|
* |
194
|
|
|
* @var bool |
195
|
|
|
*/ |
196
|
|
|
protected $imageBase64 = true; |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* toggle transparency, not supported by jpg |
200
|
|
|
* |
201
|
|
|
* @var bool |
202
|
|
|
*/ |
203
|
|
|
protected $imageTransparent = true; |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @see imagecolortransparent() |
207
|
|
|
* |
208
|
|
|
* @var array [R, G, B] |
209
|
|
|
*/ |
210
|
|
|
protected $imageTransparencyBG = [255, 255, 255]; |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @see imagepng() |
214
|
|
|
* |
215
|
|
|
* @var int |
216
|
|
|
*/ |
217
|
|
|
protected $pngCompression = -1; |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @see imagejpeg() |
221
|
|
|
* |
222
|
|
|
* @var int |
223
|
|
|
*/ |
224
|
|
|
protected $jpegQuality = 85; |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Imagick output format |
228
|
|
|
* |
229
|
|
|
* @see Imagick::setType() |
230
|
|
|
* |
231
|
|
|
* @var string |
232
|
|
|
*/ |
233
|
|
|
protected $imagickFormat = 'png'; |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Imagick background color (defaults to "transparent") |
237
|
|
|
* |
238
|
|
|
* @see \ImagickPixel::__construct() |
239
|
|
|
* |
240
|
|
|
* @var string|null |
241
|
|
|
*/ |
242
|
|
|
protected $imagickBG = null; |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Measurement unit for FPDF output: pt, mm, cm, in (defaults to "pt") |
246
|
|
|
* |
247
|
|
|
* @see \FPDF::__construct() |
248
|
|
|
*/ |
249
|
|
|
protected $fpdfMeasureUnit = 'pt'; |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Module values map |
253
|
|
|
* |
254
|
|
|
* HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()... |
255
|
|
|
* IMAGE: [63, 127, 255] // R, G, B |
256
|
|
|
* |
257
|
|
|
* @var array|null |
258
|
|
|
*/ |
259
|
|
|
protected $moduleValues = null; |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* clamp min/max version number |
263
|
|
|
* |
264
|
|
|
* @param int $versionMin |
265
|
|
|
* @param int $versionMax |
266
|
|
|
* |
267
|
|
|
* @return void |
268
|
|
|
*/ |
269
|
|
|
protected function setMinMaxVersion(int $versionMin, int $versionMax):void{ |
270
|
|
|
$min = max(1, min(40, $versionMin)); |
271
|
|
|
$max = max(1, min(40, $versionMax)); |
272
|
|
|
|
273
|
|
|
$this->versionMin = min($min, $max); |
274
|
|
|
$this->versionMax = max($min, $max); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* sets the minimum version number |
279
|
|
|
* |
280
|
|
|
* @param int $version |
281
|
|
|
* |
282
|
|
|
* @return void |
283
|
|
|
*/ |
284
|
|
|
protected function set_versionMin(int $version):void{ |
285
|
|
|
$this->setMinMaxVersion($version, $this->versionMax); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* sets the maximum version number |
290
|
|
|
* |
291
|
|
|
* @param int $version |
292
|
|
|
* |
293
|
|
|
* @return void |
294
|
|
|
*/ |
295
|
|
|
protected function set_versionMax(int $version):void{ |
296
|
|
|
$this->setMinMaxVersion($this->versionMin, $version); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* sets the error correction level |
301
|
|
|
* |
302
|
|
|
* @param int $eccLevel |
303
|
|
|
* |
304
|
|
|
* @return void |
305
|
|
|
* @throws \chillerlan\QRCode\QRCodeException |
306
|
|
|
*/ |
307
|
|
|
protected function set_eccLevel(int $eccLevel):void{ |
308
|
|
|
|
309
|
|
|
if(!isset(QRCode::ECC_MODES[$eccLevel])){ |
310
|
|
|
throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel)); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
$this->eccLevel = $eccLevel; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* sets/clamps the mask pattern |
318
|
|
|
* |
319
|
|
|
* @param int $maskPattern |
320
|
|
|
* |
321
|
|
|
* @return void |
322
|
|
|
*/ |
323
|
|
|
protected function set_maskPattern(int $maskPattern):void{ |
324
|
|
|
|
325
|
|
|
if($maskPattern !== QRCode::MASK_PATTERN_AUTO){ |
326
|
|
|
$this->maskPattern = max(0, min(7, $maskPattern)); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* sets the transparency background color |
333
|
|
|
* |
334
|
|
|
* @param mixed $imageTransparencyBG |
335
|
|
|
* |
336
|
|
|
* @return void |
337
|
|
|
* @throws \chillerlan\QRCode\QRCodeException |
338
|
|
|
*/ |
339
|
|
|
protected function set_imageTransparencyBG($imageTransparencyBG):void{ |
340
|
|
|
|
341
|
|
|
// invalid value - set to white as default |
342
|
|
|
if(!is_array($imageTransparencyBG) || count($imageTransparencyBG) < 3){ |
343
|
|
|
$this->imageTransparencyBG = [255, 255, 255]; |
344
|
|
|
|
345
|
|
|
return; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
foreach($imageTransparencyBG as $k => $v){ |
349
|
|
|
|
350
|
|
|
if(!is_numeric($v)){ |
351
|
|
|
throw new QRCodeException('Invalid RGB value.'); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
// clamp the values |
355
|
|
|
$this->imageTransparencyBG[$k] = max(0, min(255, (int)$v)); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
// use the array values to not run into errors with the spread operator (...$arr) |
359
|
|
|
$this->imageTransparencyBG = array_values($this->imageTransparencyBG); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* sets/clamps the version number |
364
|
|
|
* |
365
|
|
|
* @param int $version |
366
|
|
|
* |
367
|
|
|
* @return void |
368
|
|
|
*/ |
369
|
|
|
protected function set_version(int $version):void{ |
370
|
|
|
|
371
|
|
|
if($version !== QRCode::VERSION_AUTO){ |
372
|
|
|
$this->version = max(1, min(40, $version)); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* sets the FPDF measurement unit |
379
|
|
|
* |
380
|
|
|
* @codeCoverageIgnore |
381
|
|
|
*/ |
382
|
|
|
protected function set_fpdfMeasureUnit(string $unit):void{ |
383
|
|
|
$unit = strtolower($unit); |
384
|
|
|
|
385
|
|
|
if(in_array($unit, ['cm', 'in', 'mm', 'pt'], true)){ |
386
|
|
|
$this->fpdfMeasureUnit = $unit; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
// @todo throw or ignore silently? |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
} |
393
|
|
|
|