1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RobbieP\ZbarQrdecoder; |
4
|
|
|
|
5
|
|
|
use RobbieP\ZbarQrdecoder\Result\AbstractResult; |
6
|
|
|
use RobbieP\ZbarQrdecoder\Result\ErrorResult; |
7
|
|
|
use RobbieP\ZbarQrdecoder\Result\Parser\ParserXML; |
8
|
|
|
use RobbieP\ZbarQrdecoder\Result\ResultCollection; |
9
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
10
|
|
|
use Symfony\Component\Process\Process; |
11
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
12
|
|
|
|
13
|
|
|
class ZbarDecoder |
14
|
|
|
{ |
15
|
|
|
const EXECUTABLE = 'zbarimg'; |
16
|
|
|
|
17
|
|
|
private $path; |
18
|
|
|
private $filePath; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AbstractResult|ResultCollection |
22
|
|
|
*/ |
23
|
|
|
private $result; |
24
|
|
|
/** |
25
|
|
|
* @var Process |
26
|
|
|
*/ |
27
|
|
|
private $process; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $config |
31
|
|
|
* @param Process $process |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
|
public function __construct(array $config = [], Process $process = null) |
35
|
|
|
{ |
36
|
|
|
if (isset($config['path'])) { |
37
|
|
|
$this->setPath($config['path']); |
38
|
|
|
} |
39
|
|
|
$this->process = null === $process ? new Process($this->getPath()) : $process; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Main constructor - builds the process, runs it then returns the Result object |
44
|
|
|
* |
45
|
|
|
* @param $filename |
46
|
|
|
* |
47
|
|
|
* @return AbstractResult|ResultCollection |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
|
|
public function make($filename) |
51
|
|
|
{ |
52
|
|
|
$this->setFilePath($filename); |
53
|
|
|
$this->buildProcess(); |
54
|
|
|
$this->runProcess(); |
55
|
|
|
|
56
|
|
|
return $this->output(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the path to the executable zbarimg |
61
|
|
|
* Defaults to /usr/bin |
62
|
|
|
* |
63
|
|
|
* @throws \Exception |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function getPath() |
67
|
|
|
{ |
68
|
|
|
if (!$this->path) { |
69
|
|
|
$this->setPath('/usr/bin'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->path; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $path |
77
|
|
|
*/ |
78
|
|
|
public function setPath($path) |
79
|
|
|
{ |
80
|
|
|
$this->path = rtrim($path, '/'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function getFilePath() |
87
|
|
|
{ |
88
|
|
|
return $this->filePath; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param mixed $filePath |
93
|
|
|
* |
94
|
|
|
* @throws \Exception |
95
|
|
|
*/ |
96
|
|
|
public function setFilePath($filePath) |
97
|
|
|
{ |
98
|
|
|
if (!is_file($filePath)) { |
99
|
|
|
throw new \RuntimeException('Invalid filepath given'); |
100
|
|
|
} |
101
|
|
|
$this->filePath = $filePath; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Builds the process |
106
|
|
|
* TODO: Configurable arguments |
107
|
|
|
* |
108
|
|
|
* @throws \Exception |
109
|
|
|
*/ |
110
|
|
|
private function buildProcess() |
111
|
|
|
{ |
112
|
|
|
$path = $this->getPath(); |
113
|
|
|
$this->process->setCommandLine([ |
114
|
|
|
$path . DIRECTORY_SEPARATOR . static::EXECUTABLE, |
115
|
|
|
'-D', |
116
|
|
|
'--xml', |
117
|
|
|
'-q', |
118
|
|
|
$this->getFilePath() |
119
|
|
|
])->enableOutput(); |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Runs the process |
125
|
|
|
* |
126
|
|
|
* @throws \RuntimeException |
127
|
|
|
*/ |
128
|
|
|
private function runProcess() |
129
|
|
|
{ |
130
|
|
|
$process = $this->process; |
131
|
|
|
try { |
132
|
|
|
$process->mustRun(); |
133
|
|
|
$parser = new ParserXML(); |
134
|
|
|
$result = $parser->parse($process->getOutput()); |
135
|
|
|
if (count($result) === 1) { |
136
|
|
|
$result = $result->getResults()[0]; |
137
|
|
|
} |
138
|
|
|
$this->result = $result; |
139
|
|
|
} catch (ProcessFailedException $e) { |
140
|
|
|
switch ($e->getProcess()->getExitCode()) { |
141
|
|
|
case 1: |
142
|
|
|
throw new \RuntimeException('An error occurred while processing the image. It could be bad arguments, I/O errors and image handling errors from ImageMagick'); |
143
|
|
|
case 2: |
144
|
|
|
throw new \RuntimeException('ImageMagick fatal error'); |
145
|
|
|
case 4: |
146
|
|
|
$this->result = new ErrorResult('No barcode detected'); |
147
|
|
|
break; |
148
|
|
|
default: |
149
|
|
|
throw new \RuntimeException('Problem with decode - check you have zbar-tools installed'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Only return the output class to the end user |
157
|
|
|
* |
158
|
|
|
* @return AbstractResult|ResultCollection |
159
|
|
|
*/ |
160
|
|
|
private function output() |
161
|
|
|
{ |
162
|
|
|
return $this->result; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|