Passed
Push — master ( 552d15...d4c1db )
by smiley
02:51
created

QRImagick::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class QRImagick
4
 *
5
 * @filesource   QRImagick.php
6
 * @created      04.07.2018
7
 * @package      chillerlan\QRCode\Output
8
 * @author       smiley <[email protected]>
9
 * @copyright    2018 smiley
10
 * @license      MIT
11
 *
12
 * @noinspection PhpComposerExtensionStubsInspection
13
 */
14
15
namespace chillerlan\QRCode\Output;
16
17
use chillerlan\QRCode\Data\QRMatrix;
0 ignored issues
show
Bug introduced by
The type chillerlan\QRCode\Data\QRMatrix was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use chillerlan\QRCode\QRCodeException;
19
use chillerlan\Settings\SettingsContainerInterface;
20
use Imagick, ImagickDraw, ImagickPixel;
21
22
use function extension_loaded, is_string;
23
24
/**
25
 * ImageMagick output module (requires ext-imagick)
26
 *
27
 * @see http://php.net/manual/book.imagick.php
28
 * @see http://phpimagick.com
29
 */
30
class QRImagick extends QROutputAbstract{
31
32
	/**
33
	 * @inheritDoc
34
	 */
35
	public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
36
37
		if(!extension_loaded('imagick')){
38
			throw new QRCodeException('ext-imagick not loaded'); // @codeCoverageIgnore
39
		}
40
41
		parent::__construct($options, $matrix);
42
	}
43
44
	/**
45
	 * @inheritDoc
46
	 */
47
	protected function setModuleValues():void{
48
49
		foreach($this::DEFAULT_MODULE_VALUES as $type => $defaultValue){
50
			$v = $this->options->moduleValues[$type] ?? null;
51
52
			if(!is_string($v)){
53
				$this->moduleValues[$type] = $defaultValue
54
					? new ImagickPixel($this->options->markupDark)
55
					: new ImagickPixel($this->options->markupLight);
56
			}
57
			else{
58
				$this->moduleValues[$type] = new ImagickPixel($v);
59
			}
60
		}
61
	}
62
63
	/**
64
	 * @inheritDoc
65
	 */
66
	public function dump(string $file = null):string{
67
		$file ??= $this->options->cachefile;
68
		$imagick = new Imagick;
69
70
		$imagick->newImage(
71
			$this->length,
72
			$this->length,
73
			new ImagickPixel($this->options->imagickBG ?? 'transparent'),
74
			$this->options->imagickFormat
75
		);
76
77
		$imageData = $this->drawImage($imagick);
78
79
		if($file !== null){
80
			$this->saveToFile($imageData, $file);
81
		}
82
83
		return $imageData;
84
	}
85
86
	/**
87
	 * Creates the QR image via ImagickDraw
88
	 */
89
	protected function drawImage(Imagick $imagick):string{
90
		$draw = new ImagickDraw;
91
92
		foreach($this->matrix->matrix() as $y => $row){
93
			foreach($row as $x => $M_TYPE){
94
				$draw->setStrokeColor($this->moduleValues[$M_TYPE]);
95
				$draw->setFillColor($this->moduleValues[$M_TYPE]);
96
				$draw->rectangle(
97
					$x * $this->scale,
98
					$y * $this->scale,
99
					($x + 1) * $this->scale,
100
					($y + 1) * $this->scale
101
				);
102
103
			}
104
		}
105
106
		$imagick->drawImage($draw);
107
108
		return (string)$imagick;
109
	}
110
111
}
112