Skip to content

Commit

Permalink
integrate bacon qr code
Browse files Browse the repository at this point in the history
  • Loading branch information
willpower232 committed Oct 26, 2019
1 parent 3407c33 commit b932038
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions lib/Providers/Qr/BaconQrCodeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace RobThree\Auth\Providers\Qr;

use BaconQrCode\Writer;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Renderer\RendererStyle\Fill;
use BaconQrCode\Renderer\Color\Rgb;
use BaconQrCode\Renderer\RendererStyle\EyeFill;

use BaconQrCode\Renderer\Image\ImageBackEndInterface;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\Image\SvgImageBackEnd;

class BaconQrCodeProvider implements IQRCodeProvider
{
private $borderWidth = 4; // default from Bacon QR Code
private $backgroundColour;
private $foregroundColour;

/**
* Ensure we using the latest Bacon QR Code
*/
public function __construct()
{
if (! class_exists(ImagickImageBackEnd::class)) {
throw new \RuntimeException('Make sure you are using version 2 of Bacon QR Code');
}
}

/**
* Standard functions from IQRCodeProvider
*/

public function getMimeType()
{
return 'image/png';
}

public function getQRCodeImage($qrText, $size)
{
return $this->getQRCodeByBackend($qrText, $size, new ImagickImageBackEnd);
}

/**
* Also provide SVG
*/
public function getQRCodeSvg($qrText, $size)
{
// strip XML tag to return only SVG
$svg = explode("\n", $this->getQRCodeByBackend($qrText, $size, new SvgImageBackEnd));
return $svg[1];
}

/**
* Abstract QR code generation function
* providing colour changing support
*/
public function getQRCodeByBackend($qrText, $size, ImageBackEndInterface $backend)
{
$rendererStyleArgs = array($size, $this->borderWidth);

if (is_array($this->foregroundColour) && is_array($this->backgroundColour)) {
$rendererStyleArgs = array_merge($rendererStyleArgs, array(
null,
null,
Fill::withForegroundColor(
new Rgb(...$this->backgroundColour),
new Rgb(...$this->foregroundColour),
new EyeFill(null, null),
new EyeFill(null, null),
new EyeFill(null, null)
)
));
}

$writer = new Writer(new ImageRenderer(
new RendererStyle(...$rendererStyleArgs),
$backend
));

return $writer->writeString($qrText);
}

/**
* Ensure colour is an array of three values but also
* accept a string and assume its a 3 or 6 character hex
*/
private function handleColour($colour)
{
if (is_string($colour) && $colour[0] == '#') {
$hexToRGB = function ($input) {
// split the array into three chunks
$split = str_split(trim($input, '#'), strlen($input) / 3);

// cope with three character hex reference
// three characters plus a # = 4
if (strlen($input) == 4) {
array_walk($split, function (&$character) {
$character = str_repeat($character, 2);
});
}

// convert hex to rgb
return array_map('hexdec', $split);
};

return $hexToRGB($colour);
}

if (is_array($colour) && count($colour) == 3) {
return $colour;
}

throw new \RuntimeException('Invalid colour value');
}

/**
* Setters
*/

public function setBackgroundColour($colour)
{
$this->backgroundColour = $this->handleColour($colour);
}

public function setBorderWidth(int $width)
{
$this->borderWidth = $width;
}

public function setForegroundColour($colour)
{
$this->foregroundColour = $this->handleColour($colour);
}
}

0 comments on commit b932038

Please sign in to comment.