From e437956429326a42eaa2b48ef370241706d495d1 Mon Sep 17 00:00:00 2001 From: codemasher Date: Mon, 16 Nov 2020 14:09:50 +0100 Subject: [PATCH] :octocat: allow returning the image resource --- src/Output/QRFpdf.php | 8 +++++++- src/Output/QRImage.php | 10 ++++++++-- src/Output/QRImagick.php | 24 ++++++++++++++++-------- src/QROptions.php | 1 + src/QROptionsTrait.php | 18 +++++++++++++++++- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/Output/QRFpdf.php b/src/Output/QRFpdf.php index bb5e537ec..fad03de8d 100644 --- a/src/Output/QRFpdf.php +++ b/src/Output/QRFpdf.php @@ -65,8 +65,10 @@ class QRFpdf extends QROutputAbstract{ /** * @inheritDoc + * + * @return string|\FPDF */ - public function dump(string $file = null):string{ + public function dump(string $file = null){ $file ??= $this->options->cachefile; $fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]); @@ -90,6 +92,10 @@ class QRFpdf extends QROutputAbstract{ } + if($this->options->returnResource){ + return $fpdf; + } + $pdfData = $fpdf->Output('S'); if($file !== null){ diff --git a/src/Output/QRImage.php b/src/Output/QRImage.php index d3c9465be..393cb2413 100644 --- a/src/Output/QRImage.php +++ b/src/Output/QRImage.php @@ -47,7 +47,7 @@ class QRImage extends QROutputAbstract{ * The GD image resource * * @see imagecreatetruecolor() - * @var resource + * @var resource|\GdImage */ protected $image; @@ -88,8 +88,10 @@ class QRImage extends QROutputAbstract{ /** * @inheritDoc + * + * @return string|\GdImage */ - public function dump(string $file = null):string{ + public function dump(string $file = null){ $file ??= $this->options->cachefile; $this->image = imagecreatetruecolor($this->length, $this->length); @@ -111,6 +113,10 @@ class QRImage extends QROutputAbstract{ } } + if($this->options->returnResource){ + return $this->image; + } + $imageData = $this->dumpImage(); if($file !== null){ diff --git a/src/Output/QRImagick.php b/src/Output/QRImagick.php index 11b8d95ba..49516d30e 100644 --- a/src/Output/QRImagick.php +++ b/src/Output/QRImagick.php @@ -29,6 +29,8 @@ use function extension_loaded, is_string; */ class QRImagick extends QROutputAbstract{ + protected Imagick $imagick; + /** * @inheritDoc */ @@ -62,19 +64,27 @@ class QRImagick extends QROutputAbstract{ /** * @inheritDoc + * + * @return string|\Imagick */ - public function dump(string $file = null):string{ + public function dump(string $file = null){ $file ??= $this->options->cachefile; - $imagick = new Imagick; + $this->imagick = new Imagick; - $imagick->newImage( + $this->imagick->newImage( $this->length, $this->length, new ImagickPixel($this->options->imagickBG ?? 'transparent'), $this->options->imagickFormat ); - $imageData = $this->drawImage($imagick); + $this->drawImage(); + + if($this->options->returnResource){ + return $this->imagick; + } + + $imageData = $this->imagick->getImageBlob(); if($file !== null){ $this->saveToFile($imageData, $file); @@ -86,7 +96,7 @@ class QRImagick extends QROutputAbstract{ /** * Creates the QR image via ImagickDraw */ - protected function drawImage(Imagick $imagick):string{ + protected function drawImage():void{ $draw = new ImagickDraw; foreach($this->matrix->matrix() as $y => $row){ @@ -103,9 +113,7 @@ class QRImagick extends QROutputAbstract{ } } - $imagick->drawImage($draw); - - return (string)$imagick; + $this->imagick->drawImage($draw); } } diff --git a/src/QROptions.php b/src/QROptions.php index 5dbc1d4a2..e36f6701a 100644 --- a/src/QROptions.php +++ b/src/QROptions.php @@ -38,6 +38,7 @@ use chillerlan\Settings\SettingsContainerAbstract; * @property string $textLight * @property string $markupDark * @property string $markupLight + * @property bool $returnResource * @property bool $imageBase64 * @property bool $imageTransparent * @property array $imageTransparencyBG diff --git a/src/QROptionsTrait.php b/src/QROptionsTrait.php index 75db6ae1a..5ce144aba 100644 --- a/src/QROptionsTrait.php +++ b/src/QROptionsTrait.php @@ -156,6 +156,22 @@ trait QROptionsTrait{ */ protected string $markupLight = '#fff'; + /** + * Return the image resource instead of a render if applicable. + * This option overrides other output options, such as $cachefile and $imageBase64. + * + * Supported by the following modules: + * + * - QRImage: resource (PHP < 8), GdImage + * - QRImagick: Imagick + * - QRFpdf: FPDF + * + * @see \chillerlan\QRCode\Output\QROutputInterface::dump() + * + * @var bool + */ + protected bool $returnResource = false; + /** * toggle base64 or raw image data */ @@ -186,7 +202,7 @@ trait QROptionsTrait{ /** * Imagick output format * - * @see Imagick::setType() + * @see \Imagick::setType() */ protected string $imagickFormat = 'png';