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