blob: 810aa9b5bd2d994b9140845721160295bab64df9 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2namespace RobThree\Auth\Providers\Qr;
3
4use Endroid\QrCode\ErrorCorrectionLevel;
5use Endroid\QrCode\QrCode;
6
7class EndroidQrCodeProvider implements IQRCodeProvider
8{
9 public $bgcolor;
10 public $color;
11 public $margin;
12 public $errorcorrectionlevel;
13
14 public function __construct($bgcolor = 'ffffff', $color = '000000', $margin = 0, $errorcorrectionlevel = 'H')
15 {
16 $this->bgcolor = $this->handleColor($bgcolor);
17 $this->color = $this->handleColor($color);
18 $this->margin = $margin;
19 $this->errorcorrectionlevel = $this->handleErrorCorrectionLevel($errorcorrectionlevel);
20 }
21
22 public function getMimeType()
23 {
24 return 'image/png';
25 }
26
27 public function getQRCodeImage($qrtext, $size)
28 {
29 return $this->qrCodeInstance($qrtext, $size)->writeString();
30 }
31
32 protected function qrCodeInstance($qrtext, $size)
33 {
34 $qrCode = new QrCode($qrtext);
35 $qrCode->setSize($size);
36
37 $qrCode->setErrorCorrectionLevel($this->errorcorrectionlevel);
38 $qrCode->setMargin($this->margin);
39 $qrCode->setBackgroundColor($this->bgcolor);
40 $qrCode->setForegroundColor($this->color);
41
42 return $qrCode;
43 }
44
45 private function handleColor($color)
46 {
47 $split = str_split($color, 2);
48 $r = hexdec($split[0]);
49 $g = hexdec($split[1]);
50 $b = hexdec($split[2]);
51
52 return ['r' => $r, 'g' => $g, 'b' => $b, 'a' => 0];
53 }
54
55 private function handleErrorCorrectionLevel($level)
56 {
57 switch ($level) {
58 case 'L':
59 return ErrorCorrectionLevel::LOW();
60 case 'M':
61 return ErrorCorrectionLevel::MEDIUM();
62 case 'Q':
63 return ErrorCorrectionLevel::QUARTILE();
64 case 'H':
65 return ErrorCorrectionLevel::HIGH();
66 default:
67 return ErrorCorrectionLevel::HIGH();
68 }
69 }
70}