Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | namespace RobThree\Auth\Providers\Qr; |
| 4 | |
| 5 | // https://image-charts.com |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 6 | class ImageChartsQRCodeProvider extends BaseHTTPQRCodeProvider |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 7 | { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 8 | /** @var string */ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 9 | public $errorcorrectionlevel; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 10 | |
| 11 | /** @var int */ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 12 | public $margin; |
| 13 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 14 | /** |
| 15 | * @param bool $verifyssl |
| 16 | * @param string $errorcorrectionlevel |
| 17 | * @param int $margin |
| 18 | */ |
| 19 | public function __construct($verifyssl = false, $errorcorrectionlevel = 'L', $margin = 1) |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 20 | { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 21 | if (!is_bool($verifyssl)) { |
| 22 | throw new QRException('VerifySSL must be bool'); |
| 23 | } |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 24 | |
| 25 | $this->verifyssl = $verifyssl; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 26 | |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 27 | $this->errorcorrectionlevel = $errorcorrectionlevel; |
| 28 | $this->margin = $margin; |
| 29 | } |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 30 | |
| 31 | /** |
| 32 | * {@inheritdoc} |
| 33 | */ |
| 34 | public function getMimeType() |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 35 | { |
| 36 | return 'image/png'; |
| 37 | } |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 38 | |
| 39 | /** |
| 40 | * {@inheritdoc} |
| 41 | */ |
| 42 | public function getQRCodeImage($qrtext, $size) |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 43 | { |
| 44 | return $this->getContent($this->getUrl($qrtext, $size)); |
| 45 | } |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 46 | |
| 47 | /** |
| 48 | * @param string $qrtext the value to encode in the QR code |
| 49 | * @param int $size the desired size of the QR code |
| 50 | * |
| 51 | * @return string file contents of the QR code |
| 52 | */ |
| 53 | public function getUrl($qrtext, $size) |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 54 | { |
| 55 | return 'https://image-charts.com/chart?cht=qr' |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 56 | . '&chs=' . ceil($size / 2) . 'x' . ceil($size / 2) |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 57 | . '&chld=' . $this->errorcorrectionlevel . '|' . $this->margin |
| 58 | . '&chl=' . rawurlencode($qrtext); |
| 59 | } |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 60 | } |