1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait QROptionsTrait |
4
|
|
|
* |
5
|
|
|
* @created 10.03.2018 |
6
|
|
|
* @author smiley <[email protected]> |
7
|
|
|
* @copyright 2018 smiley |
8
|
|
|
* @license MIT |
9
|
|
|
* |
10
|
|
|
* @noinspection PhpUnused |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\QRCode; |
14
|
|
|
|
15
|
|
|
use chillerlan\QRCode\Output\QROutputInterface; |
16
|
|
|
use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version}; |
17
|
|
|
use function array_values, count, extension_loaded, in_array, is_numeric, max, min, sprintf, strtolower; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The QRCode plug-in settings & setter functionality |
21
|
|
|
*/ |
22
|
|
|
trait QROptionsTrait{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* QR Code version number |
26
|
|
|
* |
27
|
|
|
* [1 ... 40] or Version::AUTO |
28
|
|
|
*/ |
29
|
|
|
protected int $version = Version::AUTO; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Minimum QR version |
33
|
|
|
* |
34
|
|
|
* if $version = QRCode::VERSION_AUTO |
35
|
|
|
*/ |
36
|
|
|
protected int $versionMin = 1; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Maximum QR version |
40
|
|
|
*/ |
41
|
|
|
protected int $versionMax = 40; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Error correct level |
45
|
|
|
* |
46
|
|
|
* QRCode::ECC_X where X is: |
47
|
|
|
* |
48
|
|
|
* - L => 7% |
49
|
|
|
* - M => 15% |
50
|
|
|
* - Q => 25% |
51
|
|
|
* - H => 30% |
52
|
|
|
*/ |
53
|
|
|
protected int $eccLevel = EccLevel::L; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Mask Pattern to use (no value in using, mostly for unit testing purposes) |
57
|
|
|
* |
58
|
|
|
* [0...7] or MaskPattern::PATTERN_AUTO |
59
|
|
|
*/ |
60
|
|
|
protected int $maskPattern = MaskPattern::AUTO; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add a "quiet zone" (margin) according to the QR code spec |
64
|
|
|
* |
65
|
|
|
* @see https://www.qrcode.com/en/howto/code.html |
66
|
|
|
*/ |
67
|
|
|
protected bool $addQuietzone = true; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Size of the quiet zone |
71
|
|
|
* |
72
|
|
|
* internally clamped to [0 ... $moduleCount / 2], defaults to 4 modules |
73
|
|
|
*/ |
74
|
|
|
protected int $quietzoneSize = 4; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The output type |
78
|
|
|
* |
79
|
|
|
* - QROutputInterface::MARKUP_XXXX where XXXX = HTML, SVG |
80
|
|
|
* - QROutputInterface::GDIMAGE_XXX where XXX = PNG, GIF, JPG |
81
|
|
|
* - QROutputInterface::STRING_XXXX where XXXX = TEXT, JSON |
82
|
|
|
* - QROutputInterface::IMAGICK |
83
|
|
|
* - QROutputInterface::EPS |
84
|
|
|
* - QROutputInterface::FPDF |
85
|
|
|
* - QROutputInterface::CUSTOM |
86
|
|
|
*/ |
87
|
|
|
protected string $outputType = QROutputInterface::MARKUP_SVG; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM |
91
|
|
|
*/ |
92
|
|
|
protected ?string $outputInterface = null; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* /path/to/cache.file |
96
|
|
|
*/ |
97
|
|
|
protected ?string $cachefile = null; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* newline string [HTML, SVG, TEXT] |
101
|
|
|
*/ |
102
|
|
|
protected string $eol = PHP_EOL; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* size of a QR code pixel [SVG, IMAGE_*], HTML via CSS |
106
|
|
|
*/ |
107
|
|
|
protected int $scale = 5; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* a common css class |
111
|
|
|
*/ |
112
|
|
|
protected string $cssClass = 'qrcode'; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* SVG opacity |
116
|
|
|
*/ |
117
|
|
|
protected float $svgOpacity = 1.0; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* anything between <defs> |
121
|
|
|
* |
122
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs |
123
|
|
|
*/ |
124
|
|
|
protected string $svgDefs = ''; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* SVG viewBox size. a single integer number which defines width/height of the viewBox attribute. |
128
|
|
|
* |
129
|
|
|
* viewBox="0 0 x x" |
130
|
|
|
* |
131
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox |
132
|
|
|
* @see https://css-tricks.com/scale-svg/#article-header-id-3 |
133
|
|
|
*/ |
134
|
|
|
protected ?int $svgViewBoxSize = null; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio |
138
|
|
|
*/ |
139
|
|
|
protected string $svgPreserveAspectRatio = 'xMidYMid'; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* optional "width" attribute with the specified value (note that the value is not checked!) |
143
|
|
|
* |
144
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/width |
145
|
|
|
*/ |
146
|
|
|
protected ?string $svgWidth = null; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* optional "height" attribute with the specified value (note that the value is not checked!) |
150
|
|
|
* |
151
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/height |
152
|
|
|
*/ |
153
|
|
|
protected ?string $svgHeight = null; |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* whether to connect the paths for the several module types to avoid weird glitches when using gradients etc. |
157
|
|
|
* |
158
|
|
|
* @see https://github.com/chillerlan/php-qrcode/issues/57 |
159
|
|
|
*/ |
160
|
|
|
protected bool $connectPaths = false; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* specify which paths/patterns to exclude from connecting if $svgConnectPaths is set to true |
164
|
|
|
*/ |
165
|
|
|
protected array $excludeFromConnect = []; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* specify whether to draw the modules as filled circles |
169
|
|
|
* |
170
|
|
|
* a note for GDImage output: |
171
|
|
|
* |
172
|
|
|
* if QROptions::$scale is less or equal than 20, the image will be upscaled internally, then the modules will be drawn |
173
|
|
|
* using imagefilledellipse() and then scaled back to the expected size using IMG_BICUBIC which in turn produces |
174
|
|
|
* unexpected outcomes in combination with transparency - to avoid this, set scale to a value greater than 20. |
175
|
|
|
* |
176
|
|
|
* @see https://github.com/chillerlan/php-qrcode/issues/23 |
177
|
|
|
* @see https://github.com/chillerlan/php-qrcode/discussions/122 |
178
|
|
|
*/ |
179
|
|
|
protected bool $drawCircularModules = false; |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* specifies the radius of the modules when $svgDrawCircularModules is set to true |
183
|
|
|
*/ |
184
|
|
|
protected float $circleRadius = 0.45; |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* specifies which module types to exclude when $svgDrawCircularModules is set to true |
188
|
|
|
*/ |
189
|
|
|
protected array $keepAsSquare = []; |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* string substitute for dark |
193
|
|
|
*/ |
194
|
|
|
protected string $textDark = '🔴'; |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* string substitute for light |
198
|
|
|
*/ |
199
|
|
|
protected string $textLight = '⭕'; |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* markup substitute for dark (CSS value) |
203
|
|
|
*/ |
204
|
|
|
protected string $markupDark = '#000'; |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* markup substitute for light (CSS value) |
208
|
|
|
*/ |
209
|
|
|
protected string $markupLight = '#fff'; |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Return the image resource instead of a render if applicable. |
213
|
|
|
* This option overrides other output options, such as $cachefile and $imageBase64. |
214
|
|
|
* |
215
|
|
|
* Supported by the following modules: |
216
|
|
|
* |
217
|
|
|
* - QRImage: resource (PHP < 8), GdImage |
218
|
|
|
* - QRImagick: Imagick |
219
|
|
|
* - QRFpdf: FPDF |
220
|
|
|
* |
221
|
|
|
* @see \chillerlan\QRCode\Output\QROutputInterface::dump() |
222
|
|
|
* |
223
|
|
|
* @var bool |
224
|
|
|
*/ |
225
|
|
|
protected bool $returnResource = false; |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* toggle base64 or raw image data |
229
|
|
|
*/ |
230
|
|
|
protected bool $imageBase64 = true; |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* toggle background transparency |
234
|
|
|
* |
235
|
|
|
* - In GdImage mode (png, gif) it sets imagecolortransparent() with QROptions::$imageTransparencyBG. |
236
|
|
|
* It also sets the "normal" background color without transparency switch. |
237
|
|
|
* |
238
|
|
|
* - In SVG mode (as of v5), it won't render the "light" modules, |
239
|
|
|
* as opacity/transparency can easily be set with css properties. |
240
|
|
|
* |
241
|
|
|
* - It has no effect in the FPDF and Imagick output modules. |
242
|
|
|
* |
243
|
|
|
* @see \chillerlan\QRCode\QROptions::$imageTransparencyBG |
244
|
|
|
* @see https://github.com/chillerlan/php-qrcode/discussions/121 |
245
|
|
|
*/ |
246
|
|
|
protected bool $imageTransparent = true; |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Sets the background color in GD mode. |
250
|
|
|
* |
251
|
|
|
* When QROptions::$imageTransparent is set to true, this color is set as transparent in imagecolortransparent() |
252
|
|
|
* |
253
|
|
|
* @see \chillerlan\QRCode\Output\QRGdImage |
254
|
|
|
* @see \chillerlan\QRCode\QROptions::$imageTransparent |
255
|
|
|
* @see imagecolortransparent() |
256
|
|
|
* |
257
|
|
|
* [R, G, B] |
258
|
|
|
*/ |
259
|
|
|
protected array $imageTransparencyBG = [255, 255, 255]; |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @see imagepng() |
263
|
|
|
*/ |
264
|
|
|
protected int $pngCompression = -1; |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @see imagejpeg() |
268
|
|
|
*/ |
269
|
|
|
protected int $jpegQuality = 85; |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Imagick output format |
273
|
|
|
* |
274
|
|
|
* @see \Imagick::setImageFormat() |
275
|
|
|
* @see https://www.imagemagick.org/script/formats.php |
276
|
|
|
*/ |
277
|
|
|
protected string $imagickFormat = 'png32'; |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Imagick background color (defaults to "transparent") |
281
|
|
|
* |
282
|
|
|
* @see \ImagickPixel::__construct() |
283
|
|
|
*/ |
284
|
|
|
protected ?string $imagickBG = null; |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Measurement unit for FPDF output: pt, mm, cm, in (defaults to "pt") |
288
|
|
|
* |
289
|
|
|
* @see \FPDF::__construct() |
290
|
|
|
*/ |
291
|
|
|
protected string $fpdfMeasureUnit = 'pt'; |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Module values map |
295
|
|
|
* |
296
|
|
|
* - HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()... |
297
|
|
|
* - IMAGE: [63, 127, 255] // R, G, B |
298
|
|
|
*/ |
299
|
|
|
protected ?array $moduleValues = null; |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* use Imaagick (if available) when reading QR Codes |
303
|
|
|
*/ |
304
|
|
|
protected bool $readerUseImagickIfAvailable = false; |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* grayscale the image before reading |
308
|
|
|
*/ |
309
|
|
|
protected bool $readerGrayscale = false; |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* increase the contrast before reading |
313
|
|
|
* |
314
|
|
|
* note that applying contrast works different in GD and Imagick, so mileage may vary |
315
|
|
|
*/ |
316
|
|
|
protected bool $readerIncreaseContrast = false; |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Toggles logo space creation |
320
|
|
|
*/ |
321
|
|
|
protected bool $addLogoSpace = false; |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* width of the logo space |
325
|
|
|
*/ |
326
|
|
|
protected int $logoSpaceWidth = 0; |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* height of the logo space |
330
|
|
|
*/ |
331
|
|
|
protected int $logoSpaceHeight = 0; |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* optional horizontal start position of the logo space (top left corner) |
335
|
|
|
*/ |
336
|
|
|
protected ?int $logoSpaceStartX = null; |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* optional vertical start position of the logo space (top left corner) |
340
|
|
|
*/ |
341
|
|
|
protected ?int $logoSpaceStartY = null; |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* clamp min/max version number |
345
|
|
|
*/ |
346
|
|
|
protected function setMinMaxVersion(int $versionMin, int $versionMax):void{ |
347
|
|
|
$min = max(1, min(40, $versionMin)); |
348
|
|
|
$max = max(1, min(40, $versionMax)); |
349
|
|
|
|
350
|
|
|
$this->versionMin = min($min, $max); |
351
|
|
|
$this->versionMax = max($min, $max); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* sets the minimum version number |
356
|
|
|
*/ |
357
|
|
|
protected function set_versionMin(int $version):void{ |
358
|
|
|
$this->setMinMaxVersion($version, $this->versionMax); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* sets the maximum version number |
363
|
|
|
*/ |
364
|
|
|
protected function set_versionMax(int $version):void{ |
365
|
|
|
$this->setMinMaxVersion($this->versionMin, $version); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* sets/clamps the version number |
370
|
|
|
*/ |
371
|
|
|
protected function set_version(int $version):void{ |
372
|
|
|
$this->version = $version !== Version::AUTO ? max(1, min(40, $version)) : Version::AUTO; // @todo |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* sets the error correction level |
377
|
|
|
* |
378
|
|
|
* @throws \chillerlan\QRCode\QRCodeException |
379
|
|
|
*/ |
380
|
|
|
protected function set_eccLevel(int $eccLevel):void{ |
381
|
|
|
|
382
|
|
|
if(!in_array($eccLevel, [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H], true)){ |
383
|
|
|
throw new QRCodeException(sprintf('Invalid error correct level: %s', $eccLevel)); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$this->eccLevel = $eccLevel; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* sets/clamps the mask pattern |
391
|
|
|
*/ |
392
|
|
|
protected function set_maskPattern(int $maskPattern):void{ |
393
|
|
|
|
394
|
|
|
if($maskPattern !== MaskPattern::AUTO){ |
395
|
|
|
$this->maskPattern = max(0, min(7, $maskPattern)); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* sets/clamps the quiet zone size |
402
|
|
|
*/ |
403
|
|
|
protected function set_quietzoneSize(int $quietzoneSize):void{ |
404
|
|
|
$this->quietzoneSize = max(0, min($quietzoneSize, 75)); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* sets the transparency background color |
409
|
|
|
* |
410
|
|
|
* @throws \chillerlan\QRCode\QRCodeException |
411
|
|
|
*/ |
412
|
|
|
protected function set_imageTransparencyBG(array $imageTransparencyBG):void{ |
413
|
|
|
|
414
|
|
|
// invalid value - set to white as default |
415
|
|
|
if(count($imageTransparencyBG) < 3){ |
416
|
|
|
$this->imageTransparencyBG = [255, 255, 255]; |
417
|
|
|
|
418
|
|
|
return; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
foreach($imageTransparencyBG as $k => $v){ |
422
|
|
|
|
423
|
|
|
// cut off exceeding items |
424
|
|
|
if($k > 2){ |
425
|
|
|
break; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
if(!is_numeric($v)){ |
429
|
|
|
throw new QRCodeException('Invalid RGB value.'); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
// clamp the values |
433
|
|
|
$this->imageTransparencyBG[$k] = max(0, min(255, (int)$v)); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
// use the array values to not run into errors with the spread operator (...$arr) |
437
|
|
|
$this->imageTransparencyBG = array_values($this->imageTransparencyBG); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* sets the FPDF measurement unit |
442
|
|
|
* |
443
|
|
|
* @codeCoverageIgnore |
444
|
|
|
*/ |
445
|
|
|
protected function set_fpdfMeasureUnit(string $unit):void{ |
446
|
|
|
$unit = strtolower($unit); |
447
|
|
|
|
448
|
|
|
if(in_array($unit, ['cm', 'in', 'mm', 'pt'], true)){ |
449
|
|
|
$this->fpdfMeasureUnit = $unit; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
// @todo throw or ignore silently? |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* enables Imagick for the QR Code reader if the extension is available |
457
|
|
|
*/ |
458
|
|
|
protected function set_readerUseImagickIfAvailable(bool $useImagickIfAvailable):void{ |
459
|
|
|
$this->readerUseImagickIfAvailable = $useImagickIfAvailable && extension_loaded('imagick'); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* clamp the logo space values between 0 and maximum length (177 modules at version 40) |
464
|
|
|
*/ |
465
|
|
|
protected function clampLogoSpaceValue(int $value):int{ |
466
|
|
|
return (int)max(0, min(177, $value)); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* clamp/set logo space width |
471
|
|
|
*/ |
472
|
|
|
protected function set_logoSpaceWidth(int $value):void{ |
473
|
|
|
$this->logoSpaceWidth = $this->clampLogoSpaceValue($value); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* clamp/set logo space height |
478
|
|
|
*/ |
479
|
|
|
protected function set_logoSpaceHeight(int $value):void{ |
480
|
|
|
$this->logoSpaceHeight = $this->clampLogoSpaceValue($value); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* clamp/set horizontal logo space start |
485
|
|
|
*/ |
486
|
|
|
protected function set_logoSpaceStartX(?int $value):void{ |
487
|
|
|
$this->logoSpaceStartX = $value === null ? null : $this->clampLogoSpaceValue($value); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* clamp/set vertical logo space start |
492
|
|
|
*/ |
493
|
|
|
protected function set_logoSpaceStartY(?int $value):void{ |
494
|
|
|
$this->logoSpaceStartY = $value === null ? null : $this->clampLogoSpaceValue($value); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* clamp/set SVG circle radius |
499
|
|
|
*/ |
500
|
|
|
protected function set_circleRadius(float $circleRadius):void{ |
501
|
|
|
$this->circleRadius = max(0.1, min(0.75, $circleRadius)); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
} |
505
|
|
|
|