blob: 928b87b1445e531109366bba843ba02a12cfa3aa [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3namespace RobThree\Auth\Providers\Qr;
4
5// http://goqr.me/api/doc/create-qr-code/
6class QRServerProvider extends BaseHTTPQRCodeProvider
7{
8 public $errorcorrectionlevel;
9 public $margin;
10 public $qzone;
11 public $bgcolor;
12 public $color;
13 public $format;
14
15 function __construct($verifyssl = false, $errorcorrectionlevel = 'L', $margin = 4, $qzone = 1, $bgcolor = 'ffffff', $color = '000000', $format = 'png')
16 {
17 if (!is_bool($verifyssl))
18 throw new QRException('VerifySSL must be bool');
19
20 $this->verifyssl = $verifyssl;
21
22 $this->errorcorrectionlevel = $errorcorrectionlevel;
23 $this->margin = $margin;
24 $this->qzone = $qzone;
25 $this->bgcolor = $bgcolor;
26 $this->color = $color;
27 $this->format = $format;
28 }
29
30 public function getMimeType()
31 {
32 switch (strtolower($this->format))
33 {
34 case 'png':
35 return 'image/png';
36 case 'gif':
37 return 'image/gif';
38 case 'jpg':
39 case 'jpeg':
40 return 'image/jpeg';
41 case 'svg':
42 return 'image/svg+xml';
43 case 'eps':
44 return 'application/postscript';
45 }
46 throw new \QRException(sprintf('Unknown MIME-type: %s', $this->format));
47 }
48
49 public function getQRCodeImage($qrtext, $size)
50 {
51 return $this->getContent($this->getUrl($qrtext, $size));
52 }
53
54 private function decodeColor($value)
55 {
56 return vsprintf('%d-%d-%d', sscanf($value, "%02x%02x%02x"));
57 }
58
59 public function getUrl($qrtext, $size)
60 {
61 return 'https://api.qrserver.com/v1/create-qr-code/'
62 . '?size=' . $size . 'x' . $size
63 . '&ecc=' . strtoupper($this->errorcorrectionlevel)
64 . '&margin=' . $this->margin
65 . '&qzone=' . $this->qzone
66 . '&bgcolor=' . $this->decodeColor($this->bgcolor)
67 . '&color=' . $this->decodeColor($this->color)
68 . '&format=' . strtolower($this->format)
69 . '&data=' . rawurlencode($qrtext);
70 }
71}