Test Failed
Push — master ( 1c3827...8aff3a )
by smiley
03:00
created

QRImagick::setModuleValues()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 15
rs 9.7666
c 0
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
/**
18
 * ImageMagick output module
19
 * requires ext-imagick
20
 * @link http://php.net/manual/book.imagick.php
21
 * @link http://phpimagick.com
22
 */
23
class QRImagick extends QROutputAbstract{
24
25
	/**
26
	 * @return void
27
	 */
28
	protected function setModuleValues():void{
29
30
		foreach($this::DEFAULT_MODULE_VALUES as $type => $defaultValue){
31
			$v = $this->options->moduleValues[$type] ?? null;
32
33
			if(!is_string($v)){
34
				$this->moduleValues[$type] = $defaultValue
35
					? new ImagickPixel($this->options->markupDark)
36
					: new ImagickPixel($this->options->markupLight);
37
			}
38
			else{
39
				$this->moduleValues[$type] = new ImagickPixel($v);
40
			}
41
		}
42
	}
43
44
	/**
45
	 * @param string|null $file
46
	 *
47
	 * @return string
48
	 */
49
	public function dump(string $file = null):string{
50
		$file    = $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
	 * @param \Imagick $imagick
71
	 *
72
	 * @return string
73
	 */
74
	protected function drawImage(Imagick $imagick):string{
75
		$draw = new ImagickDraw;
76
77
		foreach($this->matrix->matrix() as $y => $row){
78
			foreach($row as $x => $M_TYPE){
79
				$draw->setStrokeColor($this->moduleValues[$M_TYPE]);
80
				$draw->setFillColor($this->moduleValues[$M_TYPE]);
81
				$draw->rectangle(
82
					$x * $this->scale,
83
					$y * $this->scale,
84
					($x + 1) * $this->scale,
85
					($y + 1) * $this->scale
86
				);
87
88
			}
89
		}
90
91
		$imagick->drawImage($draw);
92
93
		return (string)$imagick;
94
	}
95
96
}
97