From b932038371773310631bfcf7a10a7eacd247487a Mon Sep 17 00:00:00 2001 From: William Hall Date: Sat, 26 Oct 2019 11:01:49 +0100 Subject: [PATCH] integrate bacon qr code --- lib/Providers/Qr/BaconQrCodeProvider.php | 137 +++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 lib/Providers/Qr/BaconQrCodeProvider.php diff --git a/lib/Providers/Qr/BaconQrCodeProvider.php b/lib/Providers/Qr/BaconQrCodeProvider.php new file mode 100644 index 0000000..ef1bd70 --- /dev/null +++ b/lib/Providers/Qr/BaconQrCodeProvider.php @@ -0,0 +1,137 @@ +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); + } +}