diff --git a/CHANGELOG.md b/CHANGELOG.md index 300d5c0..212e9fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.0.0] - 2024-12-05 + +### Updated +- Upgraded the QR code generation library to the latest version of endroid/qr-code. + +### Changed +- Refactored the implementation of QR code generation to align with the updated library. +- Breaking change: Applications using this library must update their QR generation code to ensure compatibility with the new implementation. + ## [0.1.1] - 2021-06-02 ### Changed diff --git a/composer.json b/composer.json index 794bb5f..7589a8e 100644 --- a/composer.json +++ b/composer.json @@ -15,20 +15,21 @@ } ], "require": { - "php": "^8.0", - "endroid/qr-code": "^3.2", + "php": "^8.1", + "endroid/qr-code": "5.0.9", "ext-gd": "*" }, "require-dev": { "reyesoft/ci": "2.0.1", "phpunit/phpunit": "^9.1.5", + "khanamiryan/qrcode-detector-decoder": "^2.0", "codedungeon/phpunit-result-printer": "0.30.0" }, "autoload": { - "classmap": [ + "classmap": [ "src/" - ], - "psr-4": { + ], + "psr-4": { "CryptoQr\\": "src/" } }, diff --git a/src/CryptoQr.php b/src/CryptoQr.php index 1218706..5d76ad0 100644 --- a/src/CryptoQr.php +++ b/src/CryptoQr.php @@ -61,7 +61,7 @@ protected function updateText(): void $this->getParam('label', $this->getLabel()) . $this->getParam('message', $this->getMessage()); - $this->getQrCode()->setText( + $this->getQrCode()->setData( $uri . ($params !== '' ? '?' . substr($params, 1) : '') ); } diff --git a/src/Qr.php b/src/Qr.php index bc4ba4b..06f7bb7 100644 --- a/src/Qr.php +++ b/src/Qr.php @@ -10,7 +10,10 @@ namespace CryptoQr; +use Endroid\QrCode\Logo\LogoInterface; use Endroid\QrCode\QrCode; +use Endroid\QrCode\Writer\PngWriter; +use Endroid\QrCode\Writer\Result\ResultInterface; class Qr { @@ -18,7 +21,14 @@ class Qr * @var string */ protected $address = ''; - + /** + * @var PngWriter + */ + protected $writer; + /** + * @var LogoInterface|null + */ + protected $logo; /** * @var QrCode */ @@ -26,8 +36,9 @@ class Qr public function __construct(string $address = '') { - $this->qr_code = new QrCode(); + $this->qr_code = new QrCode($address); $this->setAddress($address); + $this->writer = new PngWriter(); } public function setAddress(string $address): void @@ -40,7 +51,7 @@ protected function updateText(): void { $uri = $this->getAddress(); - $this->getQrCode()->setText($uri); + $this->getQrCode()->setData($uri); } public function getAddress(): string @@ -48,8 +59,33 @@ public function getAddress(): string return $this->address; } + public function setLogo(LogoInterface $logo): void + { + $this->logo = $logo; + } + public function getQrCode(): QrCode { return $this->qr_code; } + + private function writerResult(): ResultInterface + { + return $this->writer->write($this->getQrCode(), $this->logo); + } + + public function getString(): string + { + return $this->writerResult()->getString(); + } + + public function getDataUri(): string + { + return $this->writerResult()->getDataUri(); + } + + public function writeFile(string $filename): void + { + $this->writerResult()->saveToFile($filename); + } } diff --git a/tests/BitcoinQrTest.php b/tests/BitcoinQrTest.php index 2f30bb0..d493200 100644 --- a/tests/BitcoinQrTest.php +++ b/tests/BitcoinQrTest.php @@ -25,7 +25,7 @@ final class BitcoinQrTest extends TestCase public function testBitcoinQrAddress(): void { $qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame('bitcoin:34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY', $reader->text()); @@ -36,7 +36,7 @@ public function testBitcoinQrWithLabel(): void $address = '34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'; $qr = new BitcoinQr($address); $qr->setLabel('Caritas'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame('bitcoin:' . $address . @@ -48,7 +48,7 @@ public function testBitcoinQrWithRequestBtc(): void $qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); $qr->setAmount(20.3); $qr->setLabel('Caritas'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame( @@ -64,7 +64,7 @@ public function testBitcoinQrWithRequestAndMessage(): void $qr = new BitcoinQr($address); $qr->setAmount(0.000023456789); $qr->setMessage($message); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame( @@ -79,7 +79,7 @@ public function testBitcoinQrWithMessage(): void $message = 'Donation for project xyz'; $qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); $qr->setMessage($message); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame( @@ -97,7 +97,7 @@ public function testWriteBitcoinQrFile(): void $qr->setAmount(80); $qr->setLabel('Caritas'); $qr->setMessage('Donation for project xyz'); - $qr->getQrCode()->writeFile($filename); + $qr->writeFile($filename); $image = imagecreatefromstring((string) file_get_contents($filename)); diff --git a/tests/CryptoQrTest.php b/tests/CryptoQrTest.php index 2cde423..e766af2 100644 --- a/tests/CryptoQrTest.php +++ b/tests/CryptoQrTest.php @@ -25,7 +25,7 @@ final class CryptoQrTest extends TestCase public function testBitcoinQrAddress(): void { $qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame('bitcoin:34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY', $reader->text()); @@ -35,7 +35,7 @@ public function testQrAddressWithoutProtocol(): void { $qr = new CryptoQr('TLPF4HgzmJgQc2oDmR8Msxq4LeUfkA4n4W'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame('TLPF4HgzmJgQc2oDmR8Msxq4LeUfkA4n4W', $reader->text()); @@ -46,7 +46,7 @@ public function testBitcoinQrWithLabel(): void $address = '34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'; $qr = new BitcoinQr($address); $qr->setLabel('Caritas'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame('bitcoin:' . $address . @@ -58,7 +58,7 @@ public function testBitcoinQrWithRequestBtc(): void $qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); $qr->setAmount(20.3); $qr->setLabel('Caritas'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame( @@ -74,7 +74,7 @@ public function testBitcoinQrWithRequestAndMessage(): void $qr = new BitcoinQr($address); $qr->setAmount(0.000023456789); $qr->setMessage($message); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame( @@ -89,7 +89,7 @@ public function testBitcoinQrWithMessage(): void $message = 'Donation for project xyz'; $qr = new BitcoinQr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); $qr->setMessage($message); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame( @@ -107,7 +107,7 @@ public function testWriteBitcoinQrFile(): void $qr->setAmount(80); $qr->setLabel('Caritas'); $qr->setMessage('Donation for project xyz'); - $qr->getQrCode()->writeFile($filename); + $qr->writeFile($filename); $image = imagecreatefromstring((string) file_get_contents($filename)); diff --git a/tests/EthereumQrTest.php b/tests/EthereumQrTest.php index 3528056..ae11962 100644 --- a/tests/EthereumQrTest.php +++ b/tests/EthereumQrTest.php @@ -24,7 +24,7 @@ final class EthereumQrTest extends TestCase public function testEthereumQrAddress(): void { $qr = new EthereumQr('0xe8ecDFacE0b274042aAD072149eEc3e232586499'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame('ethereum:0xe8ecDFacE0b274042aAD072149eEc3e232586499', $reader->text()); @@ -35,7 +35,7 @@ public function testEthereumQrWithLabel(): void $address = '0xe8ecDFacE0b274042aAD072149eEc3e232586499'; $qr = new EthereumQr($address); $qr->setLabel('Caritas'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame('ethereum:' . $address . @@ -47,7 +47,7 @@ public function testEthereumQrWithRequestEth(): void $qr = new EthereumQr('0xe8ecDFacE0b274042aAD072149eEc3e232586499'); $qr->setAmount(20.3); $qr->setLabel('Caritas'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame( @@ -63,7 +63,7 @@ public function testEthereumQrWithRequestAndMessage(): void $qr = new EthereumQr($address); $qr->setAmount(0.000023456789); $qr->setMessage($message); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame( @@ -78,7 +78,7 @@ public function testEthereumQrWithMessage(): void $message = 'Donation for project xyz'; $qr = new EthereumQr('0xe8ecDFacE0b274042aAD072149eEc3e232586499'); $qr->setMessage($message); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame( @@ -96,7 +96,7 @@ public function testWriteEthereumQrFile(): void $qr->setAmount(80); $qr->setLabel('Caritas'); $qr->setMessage('Donation for project xyz'); - $qr->getQrCode()->writeFile($filename); + $qr->writeFile($filename); $image = imagecreatefromstring((string) file_get_contents($filename)); diff --git a/tests/QrTest.php b/tests/QrTest.php index 605af32..5e7a39d 100644 --- a/tests/QrTest.php +++ b/tests/QrTest.php @@ -11,6 +11,7 @@ namespace CryptoQr\Tests; use CryptoQr\Qr; +use Endroid\QrCode\Logo\Logo; use PHPUnit\Framework\TestCase; use Zxing\QrReader; @@ -24,9 +25,38 @@ final class QrTest extends TestCase public function testQrAddress(): void { $qr = new Qr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); - $pngData = $qr->getQrCode()->writeString(); + $pngData = $qr->getString(); $reader = new QrReader($pngData, QrReader::SOURCE_TYPE_BLOB); $this->assertSame('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY', $reader->text()); } + + public function testQrUri(): void + { + $qr = new Qr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); + $pngUri = $qr->getDataUri(); + + $this->assertNotEmpty($pngUri); + } + + public function testSetQrLogo(): void + { + $qr = new Qr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); + $qr->setLogo(new Logo(__DIR__ . '/resources/img/BTC.png')); + $pngUri = $qr->getDataUri(); + + $this->assertNotEmpty($pngUri); + } + + public function testWriteQrFile(): void + { + $filename = sys_get_temp_dir() . '/qr-code.png'; + + $qr = new Qr('34ZwZ4cYiwZnYquM4KW67sqT7vY88215CY'); + $qr->writeFile($filename); + + $image = imagecreatefromstring((string) file_get_contents($filename)); + + $this->assertNotFalse($image); + } } diff --git a/tests/resources/img/BTC.png b/tests/resources/img/BTC.png new file mode 100644 index 0000000..3a783e6 Binary files /dev/null and b/tests/resources/img/BTC.png differ