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