QrReader.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Zxing;
  3. use Zxing\Common\HybridBinarizer;
  4. use Zxing\Qrcode\QRCodeReader;
  5. final class QrReader
  6. {
  7. const SOURCE_TYPE_FILE = 'file';
  8. const SOURCE_TYPE_BLOB = 'blob';
  9. const SOURCE_TYPE_RESOURCE = 'resource';
  10. private $bitmap;
  11. private $reader;
  12. private $result;
  13. public function __construct($imgSource, $sourceType = QrReader::SOURCE_TYPE_FILE, $useImagickIfAvailable = true)
  14. {
  15. if (!in_array($sourceType, [
  16. self::SOURCE_TYPE_FILE,
  17. self::SOURCE_TYPE_BLOB,
  18. self::SOURCE_TYPE_RESOURCE,
  19. ], true)) {
  20. throw new \InvalidArgumentException('Invalid image source.');
  21. }
  22. $im = null;
  23. switch ($sourceType) {
  24. case QrReader::SOURCE_TYPE_FILE:
  25. if ($useImagickIfAvailable && extension_loaded('imagick')) {
  26. $im = new \Imagick();
  27. $im->readImage($imgSource);
  28. } else {
  29. $image = file_get_contents($imgSource);
  30. $im = imagecreatefromstring($image);
  31. }
  32. break;
  33. case QrReader::SOURCE_TYPE_BLOB:
  34. if ($useImagickIfAvailable && extension_loaded('imagick')) {
  35. $im = new \Imagick();
  36. $im->readImageBlob($imgSource);
  37. } else {
  38. $im = imagecreatefromstring($imgSource);
  39. }
  40. break;
  41. case QrReader::SOURCE_TYPE_RESOURCE:
  42. $im = $imgSource;
  43. if ($useImagickIfAvailable && extension_loaded('imagick')) {
  44. $useImagickIfAvailable = true;
  45. } else {
  46. $useImagickIfAvailable = false;
  47. }
  48. break;
  49. }
  50. if ($useImagickIfAvailable && extension_loaded('imagick')) {
  51. if (!$im instanceof \Imagick) {
  52. throw new \InvalidArgumentException('Invalid image source.');
  53. }
  54. $width = $im->getImageWidth();
  55. $height = $im->getImageHeight();
  56. $source = new IMagickLuminanceSource($im, $width, $height);
  57. } else {
  58. if (!is_resource($im)) {
  59. throw new \InvalidArgumentException('Invalid image source.');
  60. }
  61. $width = imagesx($im);
  62. $height = imagesy($im);
  63. $source = new GDLuminanceSource($im, $width, $height);
  64. }
  65. $histo = new HybridBinarizer($source);
  66. $this->bitmap = new BinaryBitmap($histo);
  67. $this->reader = new QRCodeReader();
  68. }
  69. public function decode()
  70. {
  71. try {
  72. $this->result = $this->reader->decode($this->bitmap);
  73. } catch (NotFoundException $er) {
  74. $this->result = false;
  75. } catch (FormatException $er) {
  76. $this->result = false;
  77. } catch (ChecksumException $er) {
  78. $this->result = false;
  79. }
  80. }
  81. public function text()
  82. {
  83. $this->decode();
  84. if (method_exists($this->result, 'toString')) {
  85. return $this->result->toString();
  86. }
  87. return $this->result;
  88. }
  89. public function getResult()
  90. {
  91. return $this->result;
  92. }
  93. }