Jelajahi Sumber

:octocat: allow returning the image resource

codemasher 5 tahun lalu
induk
melakukan
3bd9eba64f

+ 7 - 1
src/Output/QRFpdf.php

@@ -65,8 +65,10 @@ class QRFpdf extends QROutputAbstract{
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
+	 *
+	 * @return string|\FPDF
 	 */
 	 */
-	public function dump(string $file = null):string{
+	public function dump(string $file = null){
 		$file = $file ?? $this->options->cachefile;
 		$file = $file ?? $this->options->cachefile;
 
 
 		$fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
 		$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');
 		$pdfData = $fpdf->Output('S');
 
 
 		if($file !== null){
 		if($file !== null){

+ 7 - 1
src/Output/QRImage.php

@@ -65,8 +65,10 @@ class QRImage extends QROutputAbstract{
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
+	 *
+	 * @return string|resource
 	 */
 	 */
-	public function dump(string $file = null):string{
+	public function dump(string $file = null){
 		$this->image = imagecreatetruecolor($this->length, $this->length);
 		$this->image = imagecreatetruecolor($this->length, $this->length);
 
 
 		// avoid: Indirect modification of overloaded property $imageTransparencyBG has no effect
 		// 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);
 		$imageData = $this->dumpImage($file);
 
 
 		if((bool)$this->options->imageBase64){
 		if((bool)$this->options->imageBase64){

+ 21 - 12
src/Output/QRImagick.php

@@ -24,6 +24,11 @@ use function is_string;
  */
  */
 class QRImagick extends QROutputAbstract{
 class QRImagick extends QROutputAbstract{
 
 
+	/**
+	 * @var \Imagick
+	 */
+	protected $imagick;
+
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
@@ -45,19 +50,27 @@ class QRImagick extends QROutputAbstract{
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @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,
 			$this->length,
 			$this->length,
 			new ImagickPixel($this->options->imagickBG ?? 'transparent'),
 			new ImagickPixel($this->options->imagickBG ?? 'transparent'),
 			$this->options->imagickFormat
 			$this->options->imagickFormat
 		);
 		);
 
 
-		$imageData = $this->drawImage($imagick);
+		$this->drawImage();
+
+		if($this->options->returnResource){
+			return $this->imagick;
+		}
+
+		$imageData = $this->imagick->getImageBlob();
 
 
 		if($file !== null){
 		if($file !== null){
 			$this->saveToFile($imageData, $file);
 			$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;
 		$draw = new ImagickDraw;
 
 
 		foreach($this->matrix->matrix() as $y => $row){
 		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);
 	}
 	}
 
 
 }
 }

+ 1 - 0
src/QROptions.php

@@ -42,6 +42,7 @@ use chillerlan\Settings\SettingsContainerAbstract;
  * @property string $markupDark
  * @property string $markupDark
  * @property string $markupLight
  * @property string $markupLight
  *
  *
+ * @property bool   $returnResource
  * @property bool   $imageBase64
  * @property bool   $imageBase64
  * @property bool   $imageTransparent
  * @property bool   $imageTransparent
  * @property array  $imageTransparencyBG
  * @property array  $imageTransparencyBG

+ 16 - 0
src/QROptionsTrait.php

@@ -188,6 +188,22 @@ trait QROptionsTrait{
 	 */
 	 */
 	protected $markupLight = '#fff';
 	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
 	 * toggle base64 or raw image data
 	 *
 	 *

+ 8 - 0
tests/Output/QRFpdfTest.php

@@ -71,4 +71,12 @@ class QRFpdfTest extends QROutputTestAbstract{
 		$this::assertSame($expected, $actual);
 		$this::assertSame($expected, $actual);
 	}
 	}
 
 
+	public function testOutputGetResource():void{
+		$this->options->returnResource = true;
+
+		$this->setOutputInterface();
+
+		$this::assertInstanceOf(FPDF::class, $this->outputInterface->dump());
+	}
+
 }
 }

+ 8 - 0
tests/Output/QRImageTest.php

@@ -58,4 +58,12 @@ class QRImageTest extends QROutputTestAbstract{
 		$this->assertTrue(true); // tricking the code coverage
 		$this->assertTrue(true); // tricking the code coverage
 	}
 	}
 
 
+	public function testOutputGetResource():void{
+		$this->options->returnResource = true;
+
+		$this->setOutputInterface();
+
+		$this::assertIsResource($this->outputInterface->dump());
+	}
+
 }
 }

+ 9 - 0
tests/Output/QRImagickTest.php

@@ -12,6 +12,7 @@
 
 
 namespace chillerlan\QRCodeTest\Output;
 namespace chillerlan\QRCodeTest\Output;
 
 
+use Imagick;
 use chillerlan\QRCode\{QRCode, Output\QRImagick};
 use chillerlan\QRCode\{QRCode, Output\QRImagick};
 
 
 class QRImagickTest extends QROutputTestAbstract{
 class QRImagickTest extends QROutputTestAbstract{
@@ -52,4 +53,12 @@ class QRImagickTest extends QROutputTestAbstract{
 		$this->assertTrue(true); // tricking the code coverage
 		$this->assertTrue(true); // tricking the code coverage
 	}
 	}
 
 
+	public function testOutputGetResource():void{
+		$this->options->returnResource = true;
+
+		$this->setOutputInterface();
+
+		$this::assertInstanceOf(Imagick::class, $this->outputInterface->dump());
+	}
+
 }
 }