控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QrReader.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Zxing;
  3. use Zxing\Common\HybridBinarizer;
  4. use Zxing\Qrcode\QRCodeReader;
  5. final class QrReader
  6. {
  7. public const SOURCE_TYPE_FILE = 'file';
  8. public const SOURCE_TYPE_BLOB = 'blob';
  9. public const SOURCE_TYPE_RESOURCE = 'resource';
  10. private readonly \Zxing\BinaryBitmap $bitmap;
  11. private readonly \Zxing\Qrcode\QRCodeReader $reader;
  12. private \Zxing\Result|bool|null $result = null;
  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 (!$im instanceof \GdImage && !is_object($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($hints = null): void
  70. {
  71. try {
  72. $this->result = $this->reader->decode($this->bitmap, $hints);
  73. } catch (NotFoundException|FormatException|ChecksumException) {
  74. $this->result = false;
  75. }
  76. }
  77. public function text($hints = null)
  78. {
  79. $this->decode($hints);
  80. if ($this->result !== false && method_exists($this->result, 'toString')) {
  81. return $this->result->toString();
  82. }
  83. return $this->result;
  84. }
  85. public function getResult()
  86. {
  87. return $this->result;
  88. }
  89. }