Passed
Push — master ( 514bca...891b04 )
by smiley
02:57
created

QRImagick::drawImage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 20
rs 9.8666
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
13
namespace chillerlan\QRCode\Output;
14
15
use Imagick, ImagickDraw, ImagickPixel;
16
17
use function is_string;
18
19
/**
20
 * ImageMagick output module
21
 * requires ext-imagick
22
 * @link http://php.net/manual/book.imagick.php
23
 * @link http://phpimagick.com
24
 */
25
class QRImagick extends QROutputAbstract{
26
27
	/**
28
	 * @inheritDoc
29
	 */
30
	protected function setModuleValues():void{
31
32
		foreach($this::DEFAULT_MODULE_VALUES as $type => $defaultValue){
33
			$v = $this->options->moduleValues[$type] ?? null;
34
35
			if(!is_string($v)){
36
				$this->moduleValues[$type] = $defaultValue
37
					? new ImagickPixel($this->options->markupDark)
38
					: new ImagickPixel($this->options->markupLight);
39
			}
40
			else{
41
				$this->moduleValues[$type] = new ImagickPixel($v);
42
			}
43
		}
44
	}
45
46
	/**
47
	 * @inheritDoc
48
	 */
49
	public function dump(string $file = null):string{
50
		$file ??= $this->options->cachefile;
51
		$imagick = new Imagick;
52
53
		$imagick->newImage(
54
			$this->length,
55
			$this->length,
56
			new ImagickPixel($this->options->imagickBG ?? 'transparent'),
57
			$this->options->imagickFormat
58
		);
59
60
		$imageData = $this->drawImage($imagick);
61
62
		if($file !== null){
63
			$this->saveToFile($imageData, $file);
64
		}
65
66
		return $imageData;
67
	}
68
69
	/**
70
	 *
71
	 */
72
	protected function drawImage(Imagick $imagick):string{
73
		$draw = new ImagickDraw;
74
75
		foreach($this->matrix->matrix() as $y => $row){
76
			foreach($row as $x => $M_TYPE){
77
				$draw->setStrokeColor($this->moduleValues[$M_TYPE]);
78
				$draw->setFillColor($this->moduleValues[$M_TYPE]);
79
				$draw->rectangle(
80
					$x * $this->scale,
81
					$y * $this->scale,
82
					($x + 1) * $this->scale,
83
					($y + 1) * $this->scale
84
				);
85
86
			}
87
		}
88
89
		$imagick->drawImage($draw);
90
91
		return (string)$imagick;
92
	}
93
94
}
95