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