QRServerProvider.php 2.1 KB

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