Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Output/QRImagick.php

raw
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 declare(strict_types=1);
13
14 namespace chillerlan\QRCode\Output;
15
16 use chillerlan\QRCode\QROptions;
17 use chillerlan\QRCode\Data\QRMatrix;
18 use chillerlan\Settings\SettingsContainerInterface;
19 use Imagick, ImagickDraw, ImagickPixel;
20 use function extension_loaded, in_array, is_string, max, min, preg_match, sprintf, strlen;
21
22 /**
23 * ImageMagick output module (requires ext-imagick)
24 *
25 * @see https://php.net/manual/book.imagick.php
26 * @see https://phpimagick.com
27 */
28 class QRImagick extends QROutputAbstract{
29
30 /**
31 * The main image instance
32 */
33 protected Imagick $imagick;
34
35 /**
36 * The main draw instance
37 */
38 protected ImagickDraw $imagickDraw;
39
40 /**
41 * The allocated background color
42 */
43 protected ImagickPixel $backgroundColor;
44
45 /**
46 * @inheritDoc
47 *
48 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
49 */
50 public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){
51
52 foreach(['fileinfo', 'imagick'] as $ext){
53 if(!extension_loaded($ext)){
54 throw new QRCodeOutputException(sprintf('ext-%s not loaded', $ext)); // @codeCoverageIgnore
55 }
56 }
57
58 parent::__construct($options, $matrix);
59 }
60
61 /**
62 * note: we're not necessarily validating the several values, just checking the general syntax
63 *
64 * @see https://www.php.net/manual/imagickpixel.construct.php
65 * @inheritDoc
66 */
67 public static function moduleValueIsValid(mixed $value):bool{
68
69 if(!is_string($value)){
70 return false;
71 }
72
73 $value = trim($value);
74
75 // hex notation
76 // #rgb(a)
77 // #rrggbb(aa)
78 // #rrrrggggbbbb(aaaa)
79 // ...
80 if(preg_match('/^#[a-f\d]+$/i', $value) && in_array((strlen($value) - 1), [3, 4, 6, 8, 9, 12, 16, 24, 32], true)){
81 return true;
82 }
83
84 // css (-like) func(...values)
85 if(preg_match('#^(graya?|hs(b|la?)|rgba?)\([\d .,%]+\)$#i', $value)){
86 return true;
87 }
88
89 // predefined css color
90 if(preg_match('/^[a-z]+$/i', $value)){
91 return true;
92 }
93
94 return false;
95 }
96
97 protected function prepareModuleValue(mixed $value):ImagickPixel{
98 return new ImagickPixel($value);
99 }
100
101 protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
102 return $this->prepareModuleValue(($isDark) ? '#000' : '#fff');
103 }
104
105 public function dump(string|null $file = null):string|Imagick{
106 $this->setBgColor();
107
108 $this->imagick = $this->createImage();
109
110 $this->drawImage();
111 // set transparency color after all operations
112 $this->setTransparencyColor();
113
114 if($this->options->returnResource){
115 return $this->imagick;
116 }
117
118 $imageData = $this->imagick->getImageBlob();
119
120 $this->imagick->destroy();
121
122 $this->saveToFile($imageData, $file);
123
124 if($this->options->outputBase64){
125 $imageData = $this->toBase64DataURI($imageData);
126 }
127
128 return $imageData;
129 }
130
131 /**
132 * Sets the background color
133 */
134 protected function setBgColor():void{
135
136 if($this::moduleValueIsValid($this->options->bgColor)){
137 $this->backgroundColor = $this->prepareModuleValue($this->options->bgColor);
138
139 return;
140 }
141
142 $this->backgroundColor = $this->prepareModuleValue('white');
143 }
144
145 /**
146 * Creates a new Imagick instance
147 */
148 protected function createImage():Imagick{
149 $imagick = new Imagick;
150 [$width, $height] = $this->getOutputDimensions();
151
152 $imagick->newImage($width, $height, $this->backgroundColor, $this->options->imagickFormat);
153
154 if($this->options->quality > -1){
155 $imagick->setImageCompressionQuality(max(0, min(100, $this->options->quality)));
156 }
157
158 return $imagick;
159 }
160
161 /**
162 * Sets the transparency color
163 */
164 protected function setTransparencyColor():void{
165
166 if(!$this->options->imageTransparent){
167 return;
168 }
169
170 $transparencyColor = $this->backgroundColor;
171
172 if($this::moduleValueIsValid($this->options->transparencyColor)){
173 $transparencyColor = $this->prepareModuleValue($this->options->transparencyColor);
174 }
175
176 $this->imagick->transparentPaintImage($transparencyColor, 0.0, 10, false);
177 }
178
179 /**
180 * Creates the QR image via ImagickDraw
181 */
182 protected function drawImage():void{
183 $this->imagickDraw = new ImagickDraw;
184 $this->imagickDraw->setStrokeWidth(0);
185
186 foreach($this->matrix->getMatrix() as $y => $row){
187 foreach($row as $x => $M_TYPE){
188 $this->module($x, $y, $M_TYPE);
189 }
190 }
191
192 $this->imagick->drawImage($this->imagickDraw);
193 }
194
195 /**
196 * draws a single pixel at the given position
197 */
198 protected function module(int $x, int $y, int $M_TYPE):void{
199
200 if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
201 return;
202 }
203
204 $this->imagickDraw->setFillColor($this->getModuleValue($M_TYPE));
205
206 if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){
207 $this->imagickDraw->circle(
208 (($x + 0.5) * $this->scale),
209 (($y + 0.5) * $this->scale),
210 (($x + 0.5 + $this->circleRadius) * $this->scale),
211 (($y + 0.5) * $this->scale),
212 );
213
214 return;
215 }
216
217 $this->imagickDraw->rectangle(
218 ($x * $this->scale),
219 ($y * $this->scale),
220 ((($x + 1) * $this->scale) - 1),
221 ((($y + 1) * $this->scale) - 1),
222 );
223 }
224
225 }