blob: 1118ea7b73b5c8bf5732a204fe34e56d2314c8f6 [file] [log] [blame]
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01001<?php
2
3namespace RobThree\Auth\Providers\Qr;
4
5// https://developers.google.com/chart/infographics/docs/qr_codes
6class 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}