Debian Codegraph @

vendor/bacon/bacon-qr-code/src/Renderer/Image/ImagickImageBackEnd.php

raw
1 <?php
2 declare(strict_types = 1);
3
4 namespace BaconQrCode\Renderer\Image;
5
6 use BaconQrCode\Exception\RuntimeException;
7 use BaconQrCode\Renderer\Color\Alpha;
8 use BaconQrCode\Renderer\Color\Cmyk;
9 use BaconQrCode\Renderer\Color\ColorInterface;
10 use BaconQrCode\Renderer\Color\Gray;
11 use BaconQrCode\Renderer\Color\Rgb;
12 use BaconQrCode\Renderer\Path\Close;
13 use BaconQrCode\Renderer\Path\Curve;
14 use BaconQrCode\Renderer\Path\EllipticArc;
15 use BaconQrCode\Renderer\Path\Line;
16 use BaconQrCode\Renderer\Path\Move;
17 use BaconQrCode\Renderer\Path\Path;
18 use BaconQrCode\Renderer\RendererStyle\Gradient;
19 use BaconQrCode\Renderer\RendererStyle\GradientType;
20 use Imagick;
21 use ImagickDraw;
22 use ImagickPixel;
23
24 final class ImagickImageBackEnd implements ImageBackEndInterface
25 {
26 private string $imageFormat;
27
28 private int $compressionQuality;
29
30 private ?Imagick $image;
31
32 private ?ImagickDraw $draw;
33
34 private ?int $gradientCount;
35
36 /**
37 * @var TransformationMatrix[]|null
38 */
39 private ?array $matrices;
40
41 private ?int $matrixIndex;
42
43 public function __construct(string $imageFormat = 'png', int $compressionQuality = 100)
44 {
45 if (! class_exists(Imagick::class)) {
46 throw new RuntimeException('You need to install the imagick extension to use this back end');
47 }
48
49 $this->imageFormat = $imageFormat;
50 $this->compressionQuality = $compressionQuality;
51 }
52
53 public function new(int $size, ColorInterface $backgroundColor) : void
54 {
55 $this->image = new Imagick();
56 $this->image->newImage($size, $size, $this->getColorPixel($backgroundColor));
57 $this->image->setImageFormat($this->imageFormat);
58 $this->image->setCompressionQuality($this->compressionQuality);
59 $this->draw = new ImagickDraw();
60 $this->gradientCount = 0;
61 $this->matrices = [new TransformationMatrix()];
62 $this->matrixIndex = 0;
63 }
64
65 public function scale(float $size) : void
66 {
67 if (null === $this->draw) {
68 throw new RuntimeException('No image has been started');
69 }
70
71 $this->draw->scale($size, $size);
72 $this->matrices[$this->matrixIndex] = $this->matrices[$this->matrixIndex]
73 ->multiply(TransformationMatrix::scale($size));
74 }
75
76 public function translate(float $x, float $y) : void
77 {
78 if (null === $this->draw) {
79 throw new RuntimeException('No image has been started');
80 }
81
82 $this->draw->translate($x, $y);
83 $this->matrices[$this->matrixIndex] = $this->matrices[$this->matrixIndex]
84 ->multiply(TransformationMatrix::translate($x, $y));
85 }
86
87 public function rotate(int $degrees) : void
88 {
89 if (null === $this->draw) {
90 throw new RuntimeException('No image has been started');
91 }
92
93 $this->draw->rotate($degrees);
94 $this->matrices[$this->matrixIndex] = $this->matrices[$this->matrixIndex]
95 ->multiply(TransformationMatrix::rotate($degrees));
96 }
97
98 public function push() : void
99 {
100 if (null === $this->draw) {
101 throw new RuntimeException('No image has been started');
102 }
103
104 $this->draw->push();
105 $this->matrices[++$this->matrixIndex] = $this->matrices[$this->matrixIndex - 1];
106 }
107
108 public function pop() : void
109 {
110 if (null === $this->draw) {
111 throw new RuntimeException('No image has been started');
112 }
113
114 $this->draw->pop();
115 unset($this->matrices[$this->matrixIndex--]);
116 }
117
118 public function drawPathWithColor(Path $path, ColorInterface $color) : void
119 {
120 if (null === $this->draw) {
121 throw new RuntimeException('No image has been started');
122 }
123
124 $this->draw->setFillColor($this->getColorPixel($color));
125 $this->drawPath($path);
126 }
127
128 public function drawPathWithGradient(
129 Path $path,
130 Gradient $gradient,
131 float $x,
132 float $y,
133 float $width,
134 float $height
135 ) : void {
136 if (null === $this->draw) {
137 throw new RuntimeException('No image has been started');
138 }
139
140 $this->draw->setFillPatternURL('#' . $this->createGradientFill($gradient, $x, $y, $width, $height));
141 $this->drawPath($path);
142 }
143
144 public function done() : string
145 {
146 if (null === $this->draw) {
147 throw new RuntimeException('No image has been started');
148 }
149
150 $this->image->drawImage($this->draw);
151 $blob = $this->image->getImageBlob();
152 $this->draw->clear();
153 $this->image->clear();
154 $this->draw = null;
155 $this->image = null;
156 $this->gradientCount = null;
157
158 return $blob;
159 }
160
161 private function drawPath(Path $path) : void
162 {
163 $this->draw->pathStart();
164
165 foreach ($path as $op) {
166 switch (true) {
167 case $op instanceof Move:
168 $this->draw->pathMoveToAbsolute($op->getX(), $op->getY());
169 break;
170
171 case $op instanceof Line:
172 $this->draw->pathLineToAbsolute($op->getX(), $op->getY());
173 break;
174
175 case $op instanceof EllipticArc:
176 $this->draw->pathEllipticArcAbsolute(
177 $op->getXRadius(),
178 $op->getYRadius(),
179 $op->getXAxisAngle(),
180 $op->isLargeArc(),
181 $op->isSweep(),
182 $op->getX(),
183 $op->getY()
184 );
185 break;
186
187 case $op instanceof Curve:
188 $this->draw->pathCurveToAbsolute(
189 $op->getX1(),
190 $op->getY1(),
191 $op->getX2(),
192 $op->getY2(),
193 $op->getX3(),
194 $op->getY3()
195 );
196 break;
197
198 case $op instanceof Close:
199 $this->draw->pathClose();
200 break;
201
202 default:
203 throw new RuntimeException('Unexpected draw operation: ' . get_class($op));
204 }
205 }
206
207 $this->draw->pathFinish();
208 }
209
210 private function createGradientFill(Gradient $gradient, float $x, float $y, float $width, float $height) : string
211 {
212 list($width, $height) = $this->matrices[$this->matrixIndex]->apply($width, $height);
213
214 $startColor = $this->getColorPixel($gradient->getStartColor())->getColorAsString();
215 $endColor = $this->getColorPixel($gradient->getEndColor())->getColorAsString();
216 $gradientImage = new Imagick();
217
218 switch ($gradient->getType()) {
219 case GradientType::HORIZONTAL():
220 $gradientImage->newPseudoImage((int) $height, (int) $width, sprintf(
221 'gradient:%s-%s',
222 $startColor,
223 $endColor
224 ));
225 $gradientImage->rotateImage('transparent', -90);
226 break;
227
228 case GradientType::VERTICAL():
229 $gradientImage->newPseudoImage((int) $width, (int) $height, sprintf(
230 'gradient:%s-%s',
231 $startColor,
232 $endColor
233 ));
234 break;
235
236 case GradientType::DIAGONAL():
237 case GradientType::INVERSE_DIAGONAL():
238 $gradientImage->newPseudoImage((int) ($width * sqrt(2)), (int) ($height * sqrt(2)), sprintf(
239 'gradient:%s-%s',
240 $startColor,
241 $endColor
242 ));
243
244 if (GradientType::DIAGONAL() === $gradient->getType()) {
245 $gradientImage->rotateImage('transparent', -45);
246 } else {
247 $gradientImage->rotateImage('transparent', -135);
248 }
249
250 $rotatedWidth = $gradientImage->getImageWidth();
251 $rotatedHeight = $gradientImage->getImageHeight();
252
253 $gradientImage->setImagePage($rotatedWidth, $rotatedHeight, 0, 0);
254 $gradientImage->cropImage(
255 intdiv($rotatedWidth, 2) - 2,
256 intdiv($rotatedHeight, 2) - 2,
257 intdiv($rotatedWidth, 4) + 1,
258 intdiv($rotatedWidth, 4) + 1
259 );
260 break;
261
262 case GradientType::RADIAL():
263 $gradientImage->newPseudoImage((int) $width, (int) $height, sprintf(
264 'radial-gradient:%s-%s',
265 $startColor,
266 $endColor
267 ));
268 break;
269 }
270
271 $id = sprintf('g%d', ++$this->gradientCount);
272 $this->draw->pushPattern($id, 0, 0, $width, $height);
273 $this->draw->composite(Imagick::COMPOSITE_COPY, 0, 0, $width, $height, $gradientImage);
274 $this->draw->popPattern();
275 return $id;
276 }
277
278 private function getColorPixel(ColorInterface $color) : ImagickPixel
279 {
280 $alpha = 100;
281
282 if ($color instanceof Alpha) {
283 $alpha = $color->getAlpha();
284 $color = $color->getBaseColor();
285 }
286
287 if ($color instanceof Rgb) {
288 return new ImagickPixel(sprintf(
289 'rgba(%d, %d, %d, %F)',
290 $color->getRed(),
291 $color->getGreen(),
292 $color->getBlue(),
293 $alpha / 100
294 ));
295 }
296
297 if ($color instanceof Cmyk) {
298 return new ImagickPixel(sprintf(
299 'cmyka(%d, %d, %d, %d, %F)',
300 $color->getCyan(),
301 $color->getMagenta(),
302 $color->getYellow(),
303 $color->getBlack(),
304 $alpha / 100
305 ));
306 }
307
308 if ($color instanceof Gray) {
309 return new ImagickPixel(sprintf(
310 'graya(%d%%, %F)',
311 $color->getGray(),
312 $alpha / 100
313 ));
314 }
315
316 return $this->getColorPixel(new Alpha($alpha, $color->toRgb()));
317 }
318 }