From 3bd9eba64fde56b1d53bb23fdb1fbee8aa0f6af6 Mon Sep 17 00:00:00 2001 From: codemasher Date: Wed, 18 Nov 2020 16:57:34 +0100 Subject: [PATCH] :octocat: allow returning the image resource --- src/Output/QRFpdf.php | 8 +++++++- src/Output/QRImage.php | 8 +++++++- src/Output/QRImagick.php | 33 +++++++++++++++++++++------------ src/QROptions.php | 1 + src/QROptionsTrait.php | 16 ++++++++++++++++ tests/Output/QRFpdfTest.php | 8 ++++++++ tests/Output/QRImageTest.php | 8 ++++++++ tests/Output/QRImagickTest.php | 9 +++++++++ 8 files changed, 77 insertions(+), 14 deletions(-) diff --git a/src/Output/QRFpdf.php b/src/Output/QRFpdf.php index 4cd972148..54c51a807 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 = $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 09156f5ff..6263568ee 100644 --- a/src/Output/QRImage.php +++ b/src/Output/QRImage.php @@ -65,8 +65,10 @@ class QRImage extends QROutputAbstract{ /** * @inheritDoc + * + * @return string|resource */ - public function dump(string $file = null):string{ + public function dump(string $file = null){ $this->image = imagecreatetruecolor($this->length, $this->length); // avoid: Indirect modification of overloaded property $imageTransparencyBG has no effect @@ -86,6 +88,10 @@ class QRImage extends QROutputAbstract{ } } + if($this->options->returnResource){ + return $this->image; + } + $imageData = $this->dumpImage($file); if((bool)$this->options->imageBase64){ diff --git a/src/Output/QRImagick.php b/src/Output/QRImagick.php index bd4507dfa..4c2a8c3b7 100644 --- a/src/Output/QRImagick.php +++ b/src/Output/QRImagick.php @@ -24,6 +24,11 @@ use function is_string; */ class QRImagick extends QROutputAbstract{ + /** + * @var \Imagick + */ + protected $imagick; + /** * @inheritDoc */ @@ -45,19 +50,27 @@ class QRImagick extends QROutputAbstract{ /** * @inheritDoc + * + * @return string|\Imagick */ - public function dump(string $file = null):string{ - $file = $file ?? $this->options->cachefile; - $imagick = new Imagick; + public function dump(string $file = null){ + $file = $file ?? $this->options->cachefile; + $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); @@ -67,11 +80,9 @@ class QRImagick extends QROutputAbstract{ } /** - * @param \Imagick $imagick - * - * @return string + * @return void */ - protected function drawImage(Imagick $imagick):string{ + protected function drawImage():void{ $draw = new ImagickDraw; foreach($this->matrix->matrix() as $y => $row){ @@ -88,9 +99,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 ab4e938f5..778ae0407 100644 --- a/src/QROptions.php +++ b/src/QROptions.php @@ -42,6 +42,7 @@ use chillerlan\Settings\SettingsContainerAbstract; * @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 a4a7acdc6..45d4cb415 100644 --- a/src/QROptionsTrait.php +++ b/src/QROptionsTrait.php @@ -188,6 +188,22 @@ trait QROptionsTrait{ */ protected $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 + * - QRImagick: Imagick + * - QRFpdf: FPDF + * + * @see \chillerlan\QRCode\Output\QROutputInterface::dump() + * + * @var bool + */ + protected $returnResource = false; + /** * toggle base64 or raw image data * diff --git a/tests/Output/QRFpdfTest.php b/tests/Output/QRFpdfTest.php index 921bd78e2..4d01bc704 100644 --- a/tests/Output/QRFpdfTest.php +++ b/tests/Output/QRFpdfTest.php @@ -71,4 +71,12 @@ class QRFpdfTest extends QROutputTestAbstract{ $this::assertSame($expected, $actual); } + public function testOutputGetResource():void{ + $this->options->returnResource = true; + + $this->setOutputInterface(); + + $this::assertInstanceOf(FPDF::class, $this->outputInterface->dump()); + } + } diff --git a/tests/Output/QRImageTest.php b/tests/Output/QRImageTest.php index bc2d9685b..34ecf4f91 100644 --- a/tests/Output/QRImageTest.php +++ b/tests/Output/QRImageTest.php @@ -58,4 +58,12 @@ class QRImageTest extends QROutputTestAbstract{ $this->assertTrue(true); // tricking the code coverage } + public function testOutputGetResource():void{ + $this->options->returnResource = true; + + $this->setOutputInterface(); + + $this::assertIsResource($this->outputInterface->dump()); + } + } diff --git a/tests/Output/QRImagickTest.php b/tests/Output/QRImagickTest.php index 224fcc69f..5374182b6 100644 --- a/tests/Output/QRImagickTest.php +++ b/tests/Output/QRImagickTest.php @@ -12,6 +12,7 @@ namespace chillerlan\QRCodeTest\Output; +use Imagick; use chillerlan\QRCode\{QRCode, Output\QRImagick}; class QRImagickTest extends QROutputTestAbstract{ @@ -52,4 +53,12 @@ class QRImagickTest extends QROutputTestAbstract{ $this->assertTrue(true); // tricking the code coverage } + public function testOutputGetResource():void{ + $this->options->returnResource = true; + + $this->setOutputInterface(); + + $this::assertInstanceOf(Imagick::class, $this->outputInterface->dump()); + } + }