Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xirelogy committed Dec 28, 2022
0 parents commit 695c402
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Simple QR-code related tools for PHP-magpie
22 changes: 22 additions & 0 deletions composer.json
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"
}
}
}
21 changes: 21 additions & 0 deletions src/Codecs/Parsers/QrCorrectionLevelParser.php
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;
}
}
26 changes: 26 additions & 0 deletions src/QrCorrectionLevel.php
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';
}
107 changes: 107 additions & 0 deletions src/QrImageGenerator.php
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(),
};
}
}

0 comments on commit 695c402

Please sign in to comment.