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