From 3fd785654372d493c031d9b541ab33a881023a32 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 26 Feb 2021 19:16:17 +0300 Subject: * switch to composer for qrcode and otp dependencies * move most OTP-related stuff into userhelper * remove old phpqrcode and otphp libraries --- .../chillerlan/php-qrcode/src/Output/QRImagick.php | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 vendor/chillerlan/php-qrcode/src/Output/QRImagick.php (limited to 'vendor/chillerlan/php-qrcode/src/Output/QRImagick.php') diff --git a/vendor/chillerlan/php-qrcode/src/Output/QRImagick.php b/vendor/chillerlan/php-qrcode/src/Output/QRImagick.php new file mode 100644 index 000000000..03886cf3b --- /dev/null +++ b/vendor/chillerlan/php-qrcode/src/Output/QRImagick.php @@ -0,0 +1,123 @@ + + * @copyright 2018 smiley + * @license MIT + * + * @noinspection PhpComposerExtensionStubsInspection + */ + +namespace chillerlan\QRCode\Output; + +use chillerlan\QRCode\Data\QRMatrix; +use chillerlan\QRCode\QRCodeException; +use chillerlan\Settings\SettingsContainerInterface; +use Imagick, ImagickDraw, ImagickPixel; + +use function is_string; + +/** + * ImageMagick output module + * requires ext-imagick + * @link http://php.net/manual/book.imagick.php + * @link http://phpimagick.com + */ +class QRImagick extends QROutputAbstract{ + + /** + * @var \Imagick + */ + protected $imagick; + + /** + * @inheritDoc + * @throws \chillerlan\QRCode\QRCodeException + */ + public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){ + + if(!extension_loaded('imagick')){ + throw new QRCodeException('ext-imagick not loaded'); // @codeCoverageIgnore + } + + parent::__construct($options, $matrix); + } + + /** + * @inheritDoc + */ + protected function setModuleValues():void{ + + foreach($this::DEFAULT_MODULE_VALUES as $type => $defaultValue){ + $v = $this->options->moduleValues[$type] ?? null; + + if(!is_string($v)){ + $this->moduleValues[$type] = $defaultValue + ? new ImagickPixel($this->options->markupDark) + : new ImagickPixel($this->options->markupLight); + } + else{ + $this->moduleValues[$type] = new ImagickPixel($v); + } + } + } + + /** + * @inheritDoc + * + * @return string|\Imagick + */ + public function dump(string $file = null){ + $file = $file ?? $this->options->cachefile; + $this->imagick = new Imagick; + + $this->imagick->newImage( + $this->length, + $this->length, + new ImagickPixel($this->options->imagickBG ?? 'transparent'), + $this->options->imagickFormat + ); + + $this->drawImage(); + + if($this->options->returnResource){ + return $this->imagick; + } + + $imageData = $this->imagick->getImageBlob(); + + if($file !== null){ + $this->saveToFile($imageData, $file); + } + + return $imageData; + } + + /** + * @return void + */ + protected function drawImage():void{ + $draw = new ImagickDraw; + + foreach($this->matrix->matrix() as $y => $row){ + foreach($row as $x => $M_TYPE){ + $draw->setStrokeColor($this->moduleValues[$M_TYPE]); + $draw->setFillColor($this->moduleValues[$M_TYPE]); + $draw->rectangle( + $x * $this->scale, + $y * $this->scale, + ($x + 1) * $this->scale, + ($y + 1) * $this->scale + ); + + } + } + + $this->imagick->drawImage($draw); + } + +} -- cgit v1.2.3