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