Skip to content

Commit f0136c9

Browse files
authored
Merge pull request #40 from SavageCore/feature/endroidqrcode
Implement `endroid/qr-code` support
2 parents de411a2 + 957128b commit f0136c9

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
namespace RobThree\Auth\Providers\Qr;
3+
4+
use Endroid\QrCode\ErrorCorrectionLevel;
5+
use Endroid\QrCode\QrCode;
6+
7+
class 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+
$qrCode = new QrCode($qrtext);
30+
$qrCode->setSize($size);
31+
32+
$qrCode->setErrorCorrectionLevel($this->errorcorrectionlevel);
33+
$qrCode->setMargin($this->margin);
34+
$qrCode->setBackgroundColor($this->bgcolor);
35+
$qrCode->setForegroundColor($this->color);
36+
37+
return $qrCode->writeString();
38+
}
39+
40+
private function handleColor($color)
41+
{
42+
$split = str_split($color, 2);
43+
$r = hexdec($split[0]);
44+
$g = hexdec($split[1]);
45+
$b = hexdec($split[2]);
46+
47+
return ['r' => $r, 'g' => $g, 'b' => $b, 'a' => 0];
48+
}
49+
50+
private function handleErrorCorrectionLevel($level)
51+
{
52+
switch ($level) {
53+
case 'L':
54+
return ErrorCorrectionLevel::LOW();
55+
case 'M':
56+
return ErrorCorrectionLevel::MEDIUM();
57+
case 'Q':
58+
return ErrorCorrectionLevel::QUARTILE();
59+
case 'H':
60+
return ErrorCorrectionLevel::HIGH();
61+
default:
62+
return ErrorCorrectionLevel::HIGH();
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)