Просмотр исходного кода

:shower: output module cleanup

codemasher 4 лет назад
Родитель
Сommit
f224587574

+ 4 - 4
src/Output/QRImage.php

@@ -101,7 +101,7 @@ class QRImage extends QROutputAbstract{
 		/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
 		$background = imagecolorallocate($this->image, ...$tbg);
 
-		if((bool)$this->options->imageTransparent && in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
+		if($this->options->imageTransparent && in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
 			imagecolortransparent($this->image, $background);
 		}
 
@@ -109,7 +109,7 @@ class QRImage extends QROutputAbstract{
 
 		foreach($this->matrix->matrix() as $y => $row){
 			foreach($row as $x => $M_TYPE){
-				$this->setPixel($x, $y, $this->moduleValues[$M_TYPE]);
+				$this->setPixel($x, $y, $M_TYPE);
 			}
 		}
 
@@ -133,7 +133,7 @@ class QRImage extends QROutputAbstract{
 	/**
 	 * Creates a single QR pixel with the given settings
 	 */
-	protected function setPixel(int $x, int $y, array $rgb):void{
+	protected function setPixel(int $x, int $y, int $M_TYPE):void{
 		imagefilledrectangle(
 			$this->image,
 			$x * $this->scale,
@@ -141,7 +141,7 @@ class QRImage extends QROutputAbstract{
 			($x + 1) * $this->scale,
 			($y + 1) * $this->scale,
 			/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
-			imagecolorallocate($this->image, ...$rgb)
+			imagecolorallocate($this->image, ...$this->moduleValues[$M_TYPE])
 		);
 	}
 

+ 20 - 11
src/Output/QRImagick.php

@@ -27,6 +27,7 @@ use function extension_loaded, is_string;
 class QRImagick extends QROutputAbstract{
 
 	protected Imagick $imagick;
+	protected ImagickDraw $imagickDraw;
 
 	/**
 	 * @inheritDoc
@@ -85,6 +86,8 @@ class QRImagick extends QROutputAbstract{
 
 		$imageData = $this->imagick->getImageBlob();
 
+		$this->imagick->destroy();
+
 		if($file !== null){
 			$this->saveToFile($imageData, $file);
 		}
@@ -96,23 +99,29 @@ class QRImagick extends QROutputAbstract{
 	 * Creates the QR image via ImagickDraw
 	 */
 	protected function drawImage():void{
-		$draw = new ImagickDraw;
+		$this->imagickDraw = new ImagickDraw;
 
 		foreach($this->matrix->matrix() as $y => $row){
 			foreach($row as $x => $M_TYPE){
-				$draw->setStrokeColor($this->moduleValues[$M_TYPE]);
-				$draw->setFillColor($this->moduleValues[$M_TYPE]);
-				$draw->rectangle(
-					$x * $this->scale,
-					$y * $this->scale,
-					($x + 1) * $this->scale,
-					($y + 1) * $this->scale
-				);
-
+				$this->drawPixel($x, $y, $M_TYPE);
 			}
 		}
 
-		$this->imagick->drawImage($draw);
+		$this->imagick->drawImage($this->imagickDraw);
+	}
+
+	/**
+	 * draws a single pixel at the given position
+	 */
+	protected function drawPixel(int $x, int $y, int $M_TYPE):void{
+		$this->imagickDraw->setStrokeColor($this->moduleValues[$M_TYPE]);
+		$this->imagickDraw->setFillColor($this->moduleValues[$M_TYPE]);
+		$this->imagickDraw->rectangle(
+			$x * $this->scale,
+			$y * $this->scale,
+			($x + 1) * $this->scale,
+			($y + 1) * $this->scale
+		);
 	}
 
 }

+ 6 - 5
src/Output/QRMarkup.php

@@ -50,7 +50,7 @@ class QRMarkup extends QROutputAbstract{
 
 		$html = empty($this->options->cssClass)
 			? '<div>'
-			: '<div class="'.$this->options->cssClass.'">';
+			: sprintf('<div class="%s">', $this->options->cssClass);
 
 		$html .= $this->options->eol;
 
@@ -58,7 +58,7 @@ class QRMarkup extends QROutputAbstract{
 			$html .= '<div>';
 
 			foreach($row as $M_TYPE){
-				$html .= '<span style="background: '.$this->moduleValues[$M_TYPE].';"></span>';
+				$html .= sprintf('<span style="background: %s;"></span>', $this->moduleValues[$M_TYPE]);
 			}
 
 			$html .= '</div>'.$this->options->eol;
@@ -67,9 +67,10 @@ class QRMarkup extends QROutputAbstract{
 		$html .= '</div>'.$this->options->eol;
 
 		if($file !== null){
-			return '<!DOCTYPE html>'.
-			       '<head><meta charset="UTF-8"><title>QR Code</title></head>'.
-			       '<body>'.$this->options->eol.$html.'</body>';
+			return sprintf(
+				'<!DOCTYPE html><head><meta charset="UTF-8"><title>QR Code</title></head><body>%s</body>',
+				$this->options->eol.$html
+			);
 		}
 
 		return $html;

+ 3 - 4
src/Output/QROutputAbstract.php

@@ -12,7 +12,7 @@ namespace chillerlan\QRCode\Output;
 
 use chillerlan\QRCode\{Data\QRMatrix, QRCode};
 use chillerlan\Settings\SettingsContainerInterface;
-use function base64_encode, call_user_func_array, dirname, file_put_contents, get_called_class, in_array, is_writable, sprintf;
+use function base64_encode, dirname, file_put_contents, get_called_class, in_array, is_writable, sprintf;
 
 /**
  * common output abstract
@@ -74,8 +74,7 @@ abstract class QROutputAbstract implements QROutputInterface{
 		$this->moduleCount = $this->matrix->size();
 		$this->scale       = $this->options->scale;
 		$this->length      = $this->moduleCount * $this->scale;
-
-		$class = get_called_class();
+		$class             = get_called_class();
 
 		if(isset(QRCode::OUTPUT_MODES[$class]) && in_array($this->options->outputType, QRCode::OUTPUT_MODES[$class])){
 			$this->outputMode = $this->options->outputType;
@@ -121,7 +120,7 @@ abstract class QROutputAbstract implements QROutputInterface{
 
 		// call the built-in output method with the optional file path as parameter
 		// to make the called method aware if a cache file was given
-		$data = call_user_func_array([$this, $this->outputMode ?? $this->defaultMode], [$file]);
+		$data = $this->{$this->outputMode ?? $this->defaultMode}($file);
 
 		if($file !== null){
 			$this->saveToFile($data, $file);

+ 2 - 3
tests/Output/QROutputTestAbstract.php

@@ -103,9 +103,8 @@ abstract class QROutputTestAbstract extends TestCase{
 		$this->options->outputType  = $type;
 		$this->options->cachefile   = $this->builddir.'/test.'.$type;
 		$this->options->imageBase64 = false;
-
-		$this->outputInterface     = $this->getOutputInterface($this->options);
-		$data                      = $this->outputInterface->dump(); // creates the cache file
+		$this->outputInterface      = $this->getOutputInterface($this->options);
+		$data                       = $this->outputInterface->dump(); // creates the cache file
 
 		$this::assertSame($data, file_get_contents($this->options->cachefile));
 	}