| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- /**
- * Class QRImagick
- *
- * @created 04.07.2018
- * @author smiley <smiley@chillerlan.net>
- * @copyright 2018 smiley
- * @license MIT
- *
- * @noinspection PhpComposerExtensionStubsInspection
- */
- namespace chillerlan\QRCode\Output;
- use chillerlan\QRCode\Data\QRMatrix;
- use chillerlan\Settings\SettingsContainerInterface;
- use finfo, Imagick, ImagickDraw, ImagickPixel;
- use function extension_loaded, is_string;
- use const FILEINFO_MIME_TYPE;
- /**
- * ImageMagick output module (requires ext-imagick)
- *
- * @see http://php.net/manual/book.imagick.php
- * @see http://phpimagick.com
- */
- class QRImagick extends QROutputAbstract{
- protected Imagick $imagick;
- protected ImagickDraw $imagickDraw;
- /**
- * @inheritDoc
- *
- * @throws \chillerlan\QRCode\Output\QRCodeOutputException
- */
- public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
- if(!extension_loaded('imagick')){
- throw new QRCodeOutputException('ext-imagick not loaded'); // @codeCoverageIgnore
- }
- if(!extension_loaded('fileinfo')){
- throw new QRCodeOutputException('ext-fileinfo not loaded'); // @codeCoverageIgnore
- }
- parent::__construct($options, $matrix);
- }
- /**
- * @inheritDoc
- */
- protected function moduleValueIsValid($value):bool{
- return is_string($value);
- }
- /**
- * @inheritDoc
- */
- protected function getModuleValue($value):ImagickPixel{
- return new ImagickPixel($value);
- }
- /**
- * @inheritDoc
- */
- protected function getDefaultModuleValue(bool $isDark):ImagickPixel{
- return new ImagickPixel(($isDark) ? $this->options->markupDark : $this->options->markupLight);
- }
- /**
- * @inheritDoc
- *
- * @return string|\Imagick
- */
- public function dump(string $file = null){
- $this->imagick = new Imagick;
- $bgColor = ($this->options->imageTransparent) ? 'transparent' : 'white';
- // keep the imagickBG property for now (until v6)
- if($this->moduleValueIsValid($this->options->bgColor ?? $this->options->imagickBG)){
- $bgColor = ($this->options->bgColor ?? $this->options->imagickBG);
- }
- $this->imagick->newImage($this->length, $this->length, new ImagickPixel($bgColor), $this->options->imagickFormat);
- $this->drawImage();
- if($this->options->returnResource){
- return $this->imagick;
- }
- $imageData = $this->imagick->getImageBlob();
- $this->imagick->destroy();
- $this->saveToFile($imageData, $file);
- if($this->options->imageBase64){
- $imageData = $this->toBase64DataURI($imageData, (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData));
- }
- return $imageData;
- }
- /**
- * Creates the QR image via ImagickDraw
- */
- protected function drawImage():void{
- $this->imagickDraw = new ImagickDraw;
- $this->imagickDraw->setStrokeWidth(0);
- foreach($this->matrix->matrix() as $y => $row){
- foreach($row as $x => $M_TYPE){
- $this->setPixel($x, $y, $M_TYPE);
- }
- }
- $this->imagick->drawImage($this->imagickDraw);
- }
- /**
- * draws a single pixel at the given position
- */
- protected function setPixel(int $x, int $y, int $M_TYPE):void{
- if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
- return;
- }
- $this->imagickDraw->setFillColor($this->moduleValues[$M_TYPE]);
- $this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)
- ? $this->imagickDraw->circle(
- (($x + 0.5) * $this->scale),
- (($y + 0.5) * $this->scale),
- (($x + 0.5 + $this->options->circleRadius) * $this->scale),
- (($y + 0.5) * $this->scale)
- )
- : $this->imagickDraw->rectangle(
- ($x * $this->scale),
- ($y * $this->scale),
- (($x + 1) * $this->scale),
- (($y + 1) * $this->scale)
- );
- }
- }
|