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) |
|
|
|
|
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
|
|
|
|