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

:octocat: QRMatrix: ignore logo w/h 0, throw on invalid dimensions

codemasher 4 лет назад
Родитель
Сommit
1aee1e90ca
2 измененных файлов с 14 добавлено и 4 удалено
  1. 13 3
      src/Data/QRMatrix.php
  2. 1 1
      tests/Data/QRMatrixTest.php

+ 13 - 3
src/Data/QRMatrix.php

@@ -477,6 +477,19 @@ final class QRMatrix{
 			throw new QRCodeDataException('ECC level "H" required to add logo space');
 		}
 
+		// if width and height happen to be exactly 0 (default value), just return - nothing to do
+		if($width === 0 || $height === 0){
+			return $this;
+		}
+
+		// $this->moduleCount includes the quiet zone (if created), we need the QR size here
+		$length = $this->version->getDimension();
+
+		// throw if the size is negative or exceeds the qrcode size
+		if($width < 0 || $height < 0 || $width > $length || $height > $length){
+			throw new QRCodeDataException('invalid logo dimensions');
+		}
+
 		// we need uneven sizes to center the logo space, adjust if needed
 		if($startX === null && ($width % 2) === 0){
 			$width++;
@@ -486,9 +499,6 @@ final class QRMatrix{
 			$height++;
 		}
 
-		// $this->moduleCount includes the quiet zone (if created), we need the QR size here
-		$length = $this->version->getDimension();
-
 		// throw if the logo space exceeds the maximum error correction capacity
 		if($width * $height > floor($length * $length * 0.2)){
 			throw new QRCodeDataException('logo space exceeds the maximum error correction capacity');

+ 1 - 1
tests/Data/QRMatrixTest.php

@@ -344,7 +344,7 @@ final class QRMatrixTest extends TestCase{
 		$o->version  = 5;
 		$o->eccLevel = EccLevel::H;
 
-		(new QRCode($o))->addByteSegment('testdata')->getMatrix()->setLogoSpace(50, 50);
+		(new QRCode($o))->addByteSegment('testdata')->getMatrix()->setLogoSpace(37, 37);
 	}
 
 	/**