diff --git a/src/Output/QRImage.php b/src/Output/QRImage.php index f3b700ec8..0677960ff 100644 --- a/src/Output/QRImage.php +++ b/src/Output/QRImage.php @@ -71,7 +71,7 @@ class QRImage extends QROutputAbstract{ */ public function dump(string $file = null):string{ $this->image = imagecreatetruecolor($this->length, $this->length); - $this->background = imagecolorallocate($this->image, ...array_values($this->options->imageTransparencyBG)); + $this->background = imagecolorallocate($this->image, ...$this->options->imageTransparencyBG); if((bool)$this->options->imageTransparent && in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){ imagecolortransparent($this->image, $this->background); diff --git a/src/QROptionsTrait.php b/src/QROptionsTrait.php index a478d38bb..a1283ab71 100644 --- a/src/QROptionsTrait.php +++ b/src/QROptionsTrait.php @@ -12,6 +12,8 @@ namespace chillerlan\QRCode; +use chillerlan\QRCode\Output\QRImage; + trait QROptionsTrait{ /** @@ -240,8 +242,8 @@ trait QROptionsTrait{ throw new QRCodeException('Invalid error correct level: '.$this->eccLevel); } - if(!is_array($this->imageTransparencyBG) || count($this->imageTransparencyBG) < 3){ - $this->imageTransparencyBG = [255, 255, 255]; + if(in_array($this->outputType, QRCode::OUTPUT_MODES[QRImage::class], true)){ + $this->clampRGBValues(); } $this->version = max(1, min(40, (int)$this->version)); @@ -258,4 +260,29 @@ trait QROptionsTrait{ } } + /** + * @throws \chillerlan\QRCode\QRCodeException + */ + protected function clampRGBValues():void{ + + if(!is_array($this->imageTransparencyBG) || count($this->imageTransparencyBG) < 3){ + $this->imageTransparencyBG = [255, 255, 255]; + } + else{ + + foreach($this->imageTransparencyBG as $k => $v){ + + if(!is_numeric($v)){ + throw new QRCodeException('Invalid RGB value.'); + } + + // clamp the values + $this->imageTransparencyBG[$k] = max(0, min(255, (int)$v)); + } + + // use the array values to not run into errors with the spread operator (...$arr) + $this->imageTransparencyBG = array_values($this->imageTransparencyBG); + } + + } }