Passed
Push — main ( 5d5958...605dc4 )
by smiley
01:54
created

QROptionsTrait::set_quietzoneSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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