ImageChartsQRCodeProvider.php 1.4 KB

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