Skip to content

Commit 1aee1e9

Browse files
committed
:octocat: QRMatrix: ignore logo w/h 0, throw on invalid dimensions
1 parent ae69a5a commit 1aee1e9

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Data/QRMatrix.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,19 @@ public function setLogoSpace(int $width, int $height, int $startX = null, int $s
477477
throw new QRCodeDataException('ECC level "H" required to add logo space');
478478
}
479479

480+
// if width and height happen to be exactly 0 (default value), just return - nothing to do
481+
if($width === 0 || $height === 0){
482+
return $this;
483+
}
484+
485+
// $this->moduleCount includes the quiet zone (if created), we need the QR size here
486+
$length = $this->version->getDimension();
487+
488+
// throw if the size is negative or exceeds the qrcode size
489+
if($width < 0 || $height < 0 || $width > $length || $height > $length){
490+
throw new QRCodeDataException('invalid logo dimensions');
491+
}
492+
480493
// we need uneven sizes to center the logo space, adjust if needed
481494
if($startX === null && ($width % 2) === 0){
482495
$width++;
@@ -486,9 +499,6 @@ public function setLogoSpace(int $width, int $height, int $startX = null, int $s
486499
$height++;
487500
}
488501

489-
// $this->moduleCount includes the quiet zone (if created), we need the QR size here
490-
$length = $this->version->getDimension();
491-
492502
// throw if the logo space exceeds the maximum error correction capacity
493503
if($width * $height > floor($length * $length * 0.2)){
494504
throw new QRCodeDataException('logo space exceeds the maximum error correction capacity');

tests/Data/QRMatrixTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function testSetLogoSpaceMaxSizeException():void{
344344
$o->version = 5;
345345
$o->eccLevel = EccLevel::H;
346346

347-
(new QRCode($o))->addByteSegment('testdata')->getMatrix()->setLogoSpace(50, 50);
347+
(new QRCode($o))->addByteSegment('testdata')->getMatrix()->setLogoSpace(37, 37);
348348
}
349349

350350
/**

0 commit comments

Comments
 (0)