1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class QRCodeReader |
4
|
|
|
* |
5
|
|
|
* @created 17.01.2021 |
6
|
|
|
* @author ZXing Authors |
7
|
|
|
* @author Smiley <[email protected]> |
8
|
|
|
* @copyright 2021 Smiley |
9
|
|
|
* @license Apache-2.0 |
10
|
|
|
* |
11
|
|
|
* @noinspection PhpComposerExtensionStubsInspection |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace chillerlan\QRCode; |
15
|
|
|
|
16
|
|
|
use Imagick, InvalidArgumentException; |
17
|
|
|
use chillerlan\QRCode\Decoder\{Decoder, DecoderResult, GDLuminanceSource, IMagickLuminanceSource}; |
18
|
|
|
use function extension_loaded, file_exists, file_get_contents, imagecreatefromstring, is_file, is_readable; |
19
|
|
|
|
20
|
|
|
final class QRCodeReader{ |
21
|
|
|
|
22
|
|
|
private bool $useImagickIfAvailable; |
23
|
|
|
|
24
|
|
|
public function __construct(bool $useImagickIfAvailable = true){ |
25
|
|
|
$this->useImagickIfAvailable = $useImagickIfAvailable && extension_loaded('imagick'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \Imagick|\GdImage|resource $im |
30
|
|
|
* |
31
|
|
|
* @return \chillerlan\QRCode\Decoder\DecoderResult |
32
|
|
|
* @phan-suppress PhanUndeclaredTypeParameter (GdImage) |
33
|
|
|
*/ |
34
|
|
|
protected function decode($im):DecoderResult{ |
35
|
|
|
|
36
|
|
|
$source = $this->useImagickIfAvailable |
37
|
|
|
? new IMagickLuminanceSource($im) |
|
|
|
|
38
|
|
|
: new GDLuminanceSource($im); |
|
|
|
|
39
|
|
|
|
40
|
|
|
return (new Decoder)->decode($source); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $imgFilePath |
45
|
|
|
* |
46
|
|
|
* @return \chillerlan\QRCode\Decoder\DecoderResult |
47
|
|
|
*/ |
48
|
|
|
public function readFile(string $imgFilePath):DecoderResult{ |
49
|
|
|
|
50
|
|
|
if(!file_exists($imgFilePath) || !is_file($imgFilePath) || !is_readable($imgFilePath)){ |
51
|
|
|
throw new InvalidArgumentException('invalid file: '.$imgFilePath); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$im = $this->useImagickIfAvailable |
55
|
|
|
? new Imagick($imgFilePath) |
56
|
|
|
: imagecreatefromstring(file_get_contents($imgFilePath)); |
57
|
|
|
|
58
|
|
|
return $this->decode($im); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $imgBlob |
63
|
|
|
* |
64
|
|
|
* @return \chillerlan\QRCode\Decoder\DecoderResult |
65
|
|
|
*/ |
66
|
|
|
public function readBlob(string $imgBlob):DecoderResult{ |
67
|
|
|
|
68
|
|
|
if($this->useImagickIfAvailable){ |
69
|
|
|
$im = new Imagick; |
70
|
|
|
$im->readImageBlob($imgBlob); |
71
|
|
|
} |
72
|
|
|
else{ |
73
|
|
|
$im = imagecreatefromstring($imgBlob); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->decode($im); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param \Imagick|\GdImage|resource $imgSource |
81
|
|
|
* |
82
|
|
|
* @return \chillerlan\QRCode\Decoder\DecoderResult |
83
|
|
|
*/ |
84
|
|
|
public function readResource($imgSource):DecoderResult{ |
85
|
|
|
return $this->decode($imgSource); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|