Explorar o código

:octocat: allow returning the image resource

codemasher %!s(int64=5) %!d(string=hai) anos
pai
achega
e437956429
Modificáronse 5 ficheiros con 49 adicións e 12 borrados
  1. 7 1
      src/Output/QRFpdf.php
  2. 8 2
      src/Output/QRImage.php
  3. 16 8
      src/Output/QRImagick.php
  4. 1 0
      src/QROptions.php
  5. 17 1
      src/QROptionsTrait.php

+ 7 - 1
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){

+ 8 - 2
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){

+ 16 - 8
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);
 	}
 
 }

+ 1 - 0
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

+ 17 - 1
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';