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

:octocat: QRGdImage: proper value check/clamp

smiley 3 лет назад
Родитель
Сommit
6e083cec8c
1 измененных файлов с 29 добавлено и 6 удалено
  1. 29 6
      src/Output/QRGdImage.php

+ 29 - 6
src/Output/QRGdImage.php

@@ -15,8 +15,8 @@ namespace chillerlan\QRCode\Output;
 use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\Settings\SettingsContainerInterface;
 use chillerlan\Settings\SettingsContainerInterface;
 use ErrorException, Throwable;
 use ErrorException, Throwable;
-use function array_values, count, extension_loaded, imagecolorallocate, imagecolortransparent, imagecreatetruecolor,
-	imagedestroy, imagefilledellipse, imagefilledrectangle, imagegif, imagejpeg, imagepng, imagescale, is_array,
+use function count, extension_loaded, imagecolorallocate, imagecolortransparent, imagecreatetruecolor,
+	imagedestroy, imagefilledellipse, imagefilledrectangle, imagegif, imagejpeg, imagepng, imagescale, is_array, is_numeric,
 	max, min, ob_end_clean, ob_get_contents, ob_start, restore_error_handler, set_error_handler;
 	max, min, ob_end_clean, ob_get_contents, ob_start, restore_error_handler, set_error_handler;
 use const IMG_BILINEAR_FIXED;
 use const IMG_BILINEAR_FIXED;
 
 
@@ -53,14 +53,33 @@ class QRGdImage extends QROutputAbstract{
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
 	protected function moduleValueIsValid($value):bool{
 	protected function moduleValueIsValid($value):bool{
-		return is_array($value) && count($value) >= 3;
+
+		if(!is_array($value) || count($value) < 3){
+			return false;
+		}
+
+		// check the first 3 values of the array
+		for($i = 0; $i < 3; $i++){
+			if(!is_numeric($value[$i])){
+				return false;
+			}
+		}
+
+		return true;
 	}
 	}
 
 
 	/**
 	/**
 	 * @inheritDoc
 	 * @inheritDoc
 	 */
 	 */
 	protected function getModuleValue($value):array{
 	protected function getModuleValue($value):array{
-		return array_values($value);
+		$v = [];
+
+		for($i = 0; $i < 3; $i++){
+			// clamp value
+			$v[] = (int)max(0, min(255, $value[$i]));
+		}
+
+		return $v;
 	}
 	}
 
 
 	/**
 	/**
@@ -106,8 +125,12 @@ class QRGdImage extends QROutputAbstract{
 		/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
 		/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
 		$background = imagecolorallocate($this->image, ...$bgColor);
 		$background = imagecolorallocate($this->image, ...$bgColor);
 
 
-		if($this->options->imageTransparent && $this->options->outputType !== QROutputInterface::GDIMAGE_JPG){
-			$tbg = $this->options->imageTransparencyBG;
+		if(
+			   $this->options->imageTransparent
+			&& $this->options->outputType !== QROutputInterface::GDIMAGE_JPG
+			&& $this->moduleValueIsValid($this->options->imageTransparencyBG)
+		){
+			$tbg = $this->getModuleValue($this->options->imageTransparencyBG);
 			/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
 			/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
 			imagecolortransparent($this->image, imagecolorallocate($this->image, ...$tbg));
 			imagecolortransparent($this->image, imagecolorallocate($this->image, ...$tbg));
 		}
 		}