-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 695c402
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Simple QR-code related tools for PHP-magpie |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "xirelogy/magpie-qr-tools", | ||
"description": "QR code related tools", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "JingXiang Tou", | ||
"email": "jx@tou.st" | ||
} | ||
], | ||
"require": { | ||
"ext-gd": "*", | ||
"xirelogy/magpie-core": "^0.1", | ||
"endroid/qr-code": "^4.6" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"MagpieLib\\QrTools\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace MagpieLib\QrTools\Codecs\Parsers; | ||
|
||
use Magpie\Codecs\Parsers\EnumParser; | ||
use MagpieLib\QrTools\QrCorrectionLevel; | ||
|
||
/** | ||
* QrCorrectionLevel parser | ||
* @extends EnumParser<QrCorrectionLevel> | ||
*/ | ||
class QrCorrectionLevelParser extends EnumParser | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
protected static function getEnumClassName() : string | ||
{ | ||
return QrCorrectionLevel::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace MagpieLib\QrTools; | ||
|
||
/** | ||
* QR-code correction level | ||
*/ | ||
enum QrCorrectionLevel : string | ||
{ | ||
/** | ||
* Level L - up to 7% damage correction | ||
*/ | ||
case L = 'l'; | ||
/** | ||
* Level M - up to 15% damage correction | ||
*/ | ||
case M = 'm'; | ||
/** | ||
* Level Q - up to 25% damage correction | ||
*/ | ||
case Q = 'q'; | ||
/** | ||
* Level H - up to 30% damage correction | ||
*/ | ||
case H = 'h'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace MagpieLib\QrTools; | ||
|
||
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh as EndroidErrorCorrectionLevelHigh; | ||
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface as EndroidErrorCorrectionLevelInterface; | ||
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow as EndroidErrorCorrectionLevelLow; | ||
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelMedium as EndroidErrorCorrectionLevelMedium; | ||
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelQuartile as EndroidErrorCorrectionLevelQuartile; | ||
use Endroid\QrCode\QrCode as EndroidQrCode; | ||
use Endroid\QrCode\Writer\PngWriter as EndroidPngWriter; | ||
use Exception; | ||
use Magpie\Exceptions\OperationFailedException; | ||
use Magpie\Exceptions\SafetyCommonException; | ||
use Magpie\General\Concepts\BinaryContentable; | ||
use Magpie\General\Contents\SimpleBinaryContent; | ||
|
||
/** | ||
* QR-code image generator | ||
*/ | ||
class QrImageGenerator | ||
{ | ||
/** | ||
* @var EndroidQrCode Underlying target | ||
*/ | ||
protected readonly EndroidQrCode $target; | ||
|
||
|
||
/** | ||
* Constructor | ||
* @param string $data | ||
*/ | ||
protected function __construct(string $data) | ||
{ | ||
$this->target = EndroidQrCode::create($data); | ||
} | ||
|
||
|
||
/** | ||
* Specify the image size | ||
* @param int $size | ||
* @return $this | ||
*/ | ||
public function withSize(int $size) : static | ||
{ | ||
$this->target->setSize($size); | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Specify the error correction level | ||
* @param QrCorrectionLevel $level | ||
* @return $this | ||
*/ | ||
public function withCorrection(QrCorrectionLevel $level) : static | ||
{ | ||
$this->target->setErrorCorrectionLevel(static::translateQrCorrectionLevel($level)); | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Generate a PNG image output | ||
* @return BinaryContentable | ||
* @throws SafetyCommonException | ||
*/ | ||
public function generatePng() : BinaryContentable | ||
{ | ||
$writer = new EndroidPngWriter(); | ||
|
||
try { | ||
$data = $writer->write($this->target); | ||
} catch (Exception $ex) { | ||
throw new OperationFailedException(previous: $ex); | ||
} | ||
|
||
return SimpleBinaryContent::create($data->getString(), $data->getMimeType()); | ||
} | ||
|
||
|
||
/** | ||
* Create a new generator instance | ||
* @param string $data | ||
* @return static | ||
*/ | ||
public static function for(string $data) : static | ||
{ | ||
return new static($data); | ||
} | ||
|
||
|
||
/** | ||
* Translate the QR-code correction level | ||
* @param QrCorrectionLevel $level | ||
* @return EndroidErrorCorrectionLevelInterface | ||
*/ | ||
protected static function translateQrCorrectionLevel(QrCorrectionLevel $level) : EndroidErrorCorrectionLevelInterface | ||
{ | ||
return match ($level) { | ||
QrCorrectionLevel::L => new EndroidErrorCorrectionLevelLow(), | ||
QrCorrectionLevel::M => new EndroidErrorCorrectionLevelMedium(), | ||
QrCorrectionLevel::Q => new EndroidErrorCorrectionLevelQuartile(), | ||
QrCorrectionLevel::H => new EndroidErrorCorrectionLevelHigh(), | ||
}; | ||
} | ||
} |