diff --git a/.github/workflows/test-endroid.yml b/.github/workflows/test-endroid.yml new file mode 100644 index 0000000..48b6e74 --- /dev/null +++ b/.github/workflows/test-endroid.yml @@ -0,0 +1,46 @@ +name: Test Endroid QR Code Provider + +on: + push: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + php-version: ['8.0', '8.1'] + endroid-version: ["^4"] + include: + - php-version: 5.6 + # earliest supported version + endroid-version: 2.2.1 + - php-version: 7.0 + endroid-version: 2.5.1 + - php-version: 7.1 + # this version is 7.1+ + endroid-version: 3.0.0 + - php-version: 7.2 + # all later versions are 7.3+ + endroid-version: 3.5.9 + - php-version: 7.3 + endroid-version: 3.9.7 + - php-version: 7.4 + endroid-version: 4.0.0 + + steps: + - uses: actions/checkout@v2 + + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + tools: composer + coverage: xdebug + ini-values: error_reporting=E_ALL + + - uses: ramsey/composer-install@v1 + + - run: composer require endroid/qrcode:${{ matrix.endroid-version }} + + - run: composer test testsDependency/EndroidQRCodeTest.php diff --git a/lib/Providers/Qr/EndroidQrCodeProvider.php b/lib/Providers/Qr/EndroidQrCodeProvider.php index 810aa9b..23fad9c 100755 --- a/lib/Providers/Qr/EndroidQrCodeProvider.php +++ b/lib/Providers/Qr/EndroidQrCodeProvider.php @@ -1,8 +1,14 @@ endroid4 = method_exists(QrCode::class, 'create'); + $this->bgcolor = $this->handleColor($bgcolor); $this->color = $this->handleColor($color); $this->margin = $margin; @@ -26,7 +36,12 @@ public function getMimeType() public function getQRCodeImage($qrtext, $size) { - return $this->qrCodeInstance($qrtext, $size)->writeString(); + if (!$this->endroid4) { + return $this->qrCodeInstance($qrtext, $size)->writeString(); + } + + $writer = new PngWriter(); + return $writer->write($this->qrCodeInstance($qrtext, $size))->getString(); } protected function qrCodeInstance($qrtext, $size) @@ -49,22 +64,21 @@ private function handleColor($color) $g = hexdec($split[1]); $b = hexdec($split[2]); - return ['r' => $r, 'g' => $g, 'b' => $b, 'a' => 0]; + return $this->endroid4 ? new Color($r, $g, $b, 0) : ['r' => $r, 'g' => $g, 'b' => $b, 'a' => 0]; } private function handleErrorCorrectionLevel($level) { switch ($level) { case 'L': - return ErrorCorrectionLevel::LOW(); + return $this->endroid4 ? new ErrorCorrectionLevelLow() : ErrorCorrectionLevel::LOW(); case 'M': - return ErrorCorrectionLevel::MEDIUM(); + return $this->endroid4 ? new ErrorCorrectionLevelMedium() : ErrorCorrectionLevel::MEDIUM(); case 'Q': - return ErrorCorrectionLevel::QUARTILE(); + return $this->endroid4 ? new ErrorCorrectionLevelQuartile() : ErrorCorrectionLevel::QUARTILE(); case 'H': - return ErrorCorrectionLevel::HIGH(); default: - return ErrorCorrectionLevel::HIGH(); + return $this->endroid4 ? new ErrorCorrectionLevelHigh() : ErrorCorrectionLevel::HIGH(); } } } diff --git a/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php b/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php index ed8cc98..589a88d 100755 --- a/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php +++ b/lib/Providers/Qr/EndroidQrCodeWithLogoProvider.php @@ -1,8 +1,8 @@ logoSize = (array)$size; } + public function getQRCodeImage($qrtext, $size) + { + if (!$this->endroid4) { + return $this->qrCodeInstance($qrtext, $size)->writeString(); + } + + $logo = null; + if ($this->logoPath) { + $logo = Logo::create($this->logoPath); + if ($this->logoSize) { + $logo->setResizeToWidth($this->logoSize[0]); + if (isset($this->logoSize[1])) { + $logo->setResizeToHeight($this->logoSize[1]); + } + } + } + $writer = new PngWriter(); + return $writer->write($this->qrCodeInstance($qrtext, $size), $logo)->getString(); + } + protected function qrCodeInstance($qrtext, $size) { $qrCode = parent::qrCodeInstance($qrtext, $size); - if ($this->logoPath) { + if (!$this->endroid4 && $this->logoPath) { $qrCode->setLogoPath($this->logoPath); if ($this->logoSize) { - $qrCode->setLogoSize($this->logoSize[0], $this->logoSize[1]); + $qrCode->setLogoSize($this->logoSize[0], isset($this->logoSize[1]) ? $this->logoSize[1] : null); } } diff --git a/testsDependency/EndroidQRCodeTest.php b/testsDependency/EndroidQRCodeTest.php new file mode 100644 index 0000000..254138e --- /dev/null +++ b/testsDependency/EndroidQRCodeTest.php @@ -0,0 +1,24 @@ +DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE')); + $this->assertEquals('image/png', $data['mimetype']); + $this->assertEquals('base64', $data['encoding']); + $this->assertNotEmpty($data['data']); + + } +}