Passed
Push — main ( 709a03...3e1426 )
by smiley
02:31
created

QRImagick::getDefaultModuleValue()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class QRImagick
4
 *
5
 * @created      04.07.2018
6
 * @author       smiley <[email protected]>
7
 * @copyright    2018 smiley
8
 * @license      MIT
9
 *
10
 * @noinspection PhpComposerExtensionStubsInspection
11
 */
12
13
namespace chillerlan\QRCode\Output;
14
15
use chillerlan\QRCode\Data\QRMatrix;
16
use chillerlan\Settings\SettingsContainerInterface;
17
use Imagick, ImagickDraw, ImagickPixel;
18
19
use function extension_loaded, is_string;
20
21
/**
22
 * ImageMagick output module (requires ext-imagick)
23
 *
24
 * @see http://php.net/manual/book.imagick.php
25
 * @see http://phpimagick.com
26
 */
27
class QRImagick extends QROutputAbstract{
28
29
	protected Imagick $imagick;
30
	protected ImagickDraw $imagickDraw;
31
32
	/**
33
	 * @inheritDoc
34
	 *
35
	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
36
	 */
37
	public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
38
39
		if(!extension_loaded('imagick')){
40
			throw new QRCodeOutputException('ext-imagick not loaded'); // @codeCoverageIgnore
41
		}
42
43
		parent::__construct($options, $matrix);
44
	}
45
46
	/**
47
	 * @inheritDoc
48
	 */
49
	protected function moduleValueIsValid($value):bool{
50
		return is_string($value);
51
	}
52
53
	/**
54
	 * @inheritDoc
55
	 */
56
	protected function getModuleValue($value):ImagickPixel{
57
		return new ImagickPixel($value);
58
	}
59
60
	/**
61
	 * @inheritDoc
62
	 */
63
	protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
64
		return new ImagickPixel($isDark ? $this->options->markupDark : $this->options->markupLight);
65
	}
66
67
	/**
68
	 * @inheritDoc
69
	 *
70
	 * @return string|\Imagick
71
	 */
72
	public function dump(string $file = null){
73
		$file          ??= $this->options->cachefile;
74
		$this->imagick = new Imagick;
75
76
		$this->imagick->newImage(
77
			$this->length,
78
			$this->length,
79
			new ImagickPixel($this->options->imagickBG ?? 'transparent'),
80
			$this->options->imagickFormat
81
		);
82
83
		$this->drawImage();
84
85
		if($this->options->returnResource){
86
			return $this->imagick;
87
		}
88
89
		$imageData = $this->imagick->getImageBlob();
90
91
		$this->imagick->destroy();
92
93
		if($file !== null){
94
			$this->saveToFile($imageData, $file);
95
		}
96
97
		return $imageData;
98
	}
99
100
	/**
101
	 * Creates the QR image via ImagickDraw
102
	 */
103
	protected function drawImage():void{
104
		$this->imagickDraw = new ImagickDraw;
105
106
		foreach($this->matrix->matrix() as $y => $row){
107
			foreach($row as $x => $M_TYPE){
108
				$this->setPixel($x, $y, $M_TYPE);
109
			}
110
		}
111
112
		$this->imagick->drawImage($this->imagickDraw);
113
	}
114
115
	/**
116
	 * draws a single pixel at the given position
117
	 */
118
	protected function setPixel(int $x, int $y, int $M_TYPE):void{
119
		$this->imagickDraw->setStrokeColor($this->moduleValues[$M_TYPE]);
120
		$this->imagickDraw->setFillColor($this->moduleValues[$M_TYPE]);
121
122
		$this->options->drawCircularModules && !$this->matrix->checkTypes($x, $y, $this->options->keepAsSquare)
0 ignored issues
show
Bug introduced by
It seems like $this->options->keepAsSquare can also be of type null; however, parameter $M_TYPES of chillerlan\QRCode\Data\QRMatrix::checkTypes() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
		$this->options->drawCircularModules && !$this->matrix->checkTypes($x, $y, /** @scrutinizer ignore-type */ $this->options->keepAsSquare)
Loading history...
123
			? $this->imagickDraw->circle(
124
				($x + 0.5) * $this->scale,
125
				($y + 0.5) * $this->scale,
126
				($x + 0.5 + $this->options->circleRadius) * $this->scale,
127
				($y + 0.5) * $this->scale
128
			)
129
			: $this->imagickDraw->rectangle(
130
				$x * $this->scale,
131
				$y * $this->scale,
132
				($x + 1) * $this->scale,
133
				($y + 1) * $this->scale
134
			);
135
	}
136
137
}
138