QRServerProvider.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace RobThree\Auth\Providers\Qr;
  3. // http://goqr.me/api/doc/create-qr-code/
  4. class QRServerProvider extends BaseHTTPQRCodeProvider
  5. {
  6. /** @var string */
  7. public $errorcorrectionlevel;
  8. /** @var int */
  9. public $margin;
  10. /** @var int */
  11. public $qzone;
  12. /** @var string */
  13. public $bgcolor;
  14. /** @var string */
  15. public $color;
  16. /** @var string */
  17. public $format;
  18. /**
  19. * @param bool $verifyssl
  20. * @param string $errorcorrectionlevel
  21. * @param int $margin
  22. * @param int $qzone
  23. * @param string $bgcolor
  24. * @param string $color
  25. * @param string $format
  26. */
  27. public function __construct($verifyssl = false, $errorcorrectionlevel = 'L', $margin = 4, $qzone = 1, $bgcolor = 'ffffff', $color = '000000', $format = 'png')
  28. {
  29. if (!is_bool($verifyssl)) {
  30. throw new QRException('VerifySSL must be bool');
  31. }
  32. $this->verifyssl = $verifyssl;
  33. $this->errorcorrectionlevel = $errorcorrectionlevel;
  34. $this->margin = $margin;
  35. $this->qzone = $qzone;
  36. $this->bgcolor = $bgcolor;
  37. $this->color = $color;
  38. $this->format = $format;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getMimeType()
  44. {
  45. switch (strtolower($this->format)) {
  46. case 'png':
  47. return 'image/png';
  48. case 'gif':
  49. return 'image/gif';
  50. case 'jpg':
  51. case 'jpeg':
  52. return 'image/jpeg';
  53. case 'svg':
  54. return 'image/svg+xml';
  55. case 'eps':
  56. return 'application/postscript';
  57. }
  58. throw new QRException(sprintf('Unknown MIME-type: %s', $this->format));
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getQRCodeImage($qrtext, $size)
  64. {
  65. return $this->getContent($this->getUrl($qrtext, $size));
  66. }
  67. /**
  68. * @param string $value
  69. *
  70. * @return string
  71. */
  72. private function decodeColor($value)
  73. {
  74. return vsprintf('%d-%d-%d', sscanf($value, "%02x%02x%02x"));
  75. }
  76. /**
  77. * @param string $qrtext the value to encode in the QR code
  78. * @param int|string $size the desired size of the QR code
  79. *
  80. * @return string file contents of the QR code
  81. */
  82. public function getUrl($qrtext, $size)
  83. {
  84. return 'https://api.qrserver.com/v1/create-qr-code/'
  85. . '?size=' . $size . 'x' . $size
  86. . '&ecc=' . strtoupper($this->errorcorrectionlevel)
  87. . '&margin=' . $this->margin
  88. . '&qzone=' . $this->qzone
  89. . '&bgcolor=' . $this->decodeColor($this->bgcolor)
  90. . '&color=' . $this->decodeColor($this->color)
  91. . '&format=' . strtolower($this->format)
  92. . '&data=' . rawurlencode($qrtext);
  93. }
  94. }