|
48 | 48 |
|
49 | 49 | final class QrReader |
50 | 50 | { |
| 51 | + const SOURCE_TYPE_FILE = 'file'; |
| 52 | + const SOURCE_TYPE_BLOB = 'blob'; |
| 53 | + const SOURCE_TYPE_RESOURCE = 'resource'; |
51 | 54 | public $result; |
52 | 55 |
|
53 | | - function __construct($filename) |
| 56 | + function __construct($imgsource, $sourcetype = QrReader::SOURCE_TYPE_FILE, $isUseImagickIfAvailable = true) |
54 | 57 | { |
55 | 58 |
|
56 | 59 | try { |
| 60 | + switch($sourcetype) { |
| 61 | + case QrReader::SOURCE_TYPE_FILE: |
| 62 | + if($isUseImagickIfAvailable && extension_loaded('imagick')) { |
| 63 | + $im = new Imagick(); |
| 64 | + $im->readImage($imgsource); |
| 65 | + }else { |
| 66 | + $image = file_get_contents($imgsource); |
| 67 | + $im = imagecreatefromstring($image); |
| 68 | + } |
57 | 69 |
|
58 | | - if(extension_loaded('imagick')) { |
59 | | - $im = new Imagick(); |
60 | | - $im->readImage($filename); |
| 70 | + break; |
| 71 | + |
| 72 | + case QrReader::SOURCE_TYPE_BLOB: |
| 73 | + if($isUseImagickIfAvailable && extension_loaded('imagick')) { |
| 74 | + $im = new Imagick(); |
| 75 | + $im->readimageblob($imgsource); |
| 76 | + }else { |
| 77 | + $im = imagecreatefromstring($imgsource); |
| 78 | + } |
| 79 | + |
| 80 | + break; |
| 81 | + |
| 82 | + case QrReader::SOURCE_TYPE_RESOURCE: |
| 83 | + $im = $imgsource; |
| 84 | + if($isUseImagickIfAvailable && extension_loaded('imagick')) { |
| 85 | + $isUseImagickIfAvailable = true; |
| 86 | + }else { |
| 87 | + $isUseImagickIfAvailable = false; |
| 88 | + } |
| 89 | + |
| 90 | + break; |
| 91 | + } |
| 92 | + |
| 93 | + if($isUseImagickIfAvailable && extension_loaded('imagick')) { |
61 | 94 | $width = $im->getImageWidth(); |
62 | 95 | $height = $im->getImageHeight(); |
63 | 96 | $source = new \Zxing\IMagickLuminanceSource($im, $width, $height); |
64 | 97 | }else { |
65 | | - $image = file_get_contents($filename); |
66 | | - $sizes = getimagesize($filename); |
67 | | - $width = $sizes[0]; |
68 | | - $height = $sizes[1]; |
69 | | - $im = imagecreatefromstring($image); |
70 | | - |
| 98 | + $width = imagesx($im); |
| 99 | + $height = imagesy($im); |
71 | 100 | $source = new \Zxing\GDLuminanceSource($im, $width, $height); |
72 | 101 | } |
73 | 102 | $histo = new \Zxing\Common\HybridBinarizer($source); |
|
0 commit comments