QROptionsTrait::set_fpdfMeasureUnit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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