Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.19% |
23 / 27 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
ImageMagick | |
85.19% |
23 / 27 |
|
62.50% |
5 / 8 |
14.64 | |
0.00% |
0 / 1 |
__construct | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
getQuery | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setQuery | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__clone | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCommand | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
raw | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
validProgram | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * Karla ImageMagick wrapper library |
5 | * |
6 | * PHP Version 8.0< |
7 | * |
8 | * @category Utility |
9 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
10 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
11 | * @link https://github.com/localgod/karla Karla |
12 | * @since 2012-04-05 |
13 | */ |
14 | |
15 | declare(strict_types=1); |
16 | |
17 | namespace Karla\Program; |
18 | |
19 | use Karla\Query; |
20 | use Karla\Cache; |
21 | |
22 | /** |
23 | * Class for wrapping ImageMagick arguments used by all tools |
24 | * |
25 | * @category Utility |
26 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
27 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
28 | * @link https://github.com/localgod/karla Karla |
29 | */ |
30 | abstract class ImageMagick implements \Karla\Program |
31 | { |
32 | /** |
33 | * ImageMagick tool animate |
34 | * |
35 | * @var string |
36 | */ |
37 | public const IMAGEMAGICK_ANIMATE = 'animate'; |
38 | |
39 | /** |
40 | * ImageMagick tool compare |
41 | * |
42 | * @var string |
43 | */ |
44 | public const IMAGEMAGICK_COMPARE = 'compare'; |
45 | |
46 | /** |
47 | * ImageMagick tool composite |
48 | * |
49 | * @var string |
50 | */ |
51 | public const IMAGEMAGICK_COMPOSITE = 'composite'; |
52 | |
53 | /** |
54 | * ImageMagick tool |
55 | * |
56 | * @var string |
57 | */ |
58 | public const IMAGEMAGICK_CONJURE = 'conjure'; |
59 | |
60 | /** |
61 | * ImageMagick tool conjure |
62 | * |
63 | * @var string |
64 | */ |
65 | public const IMAGEMAGICK_CONVERT = 'convert'; |
66 | |
67 | /** |
68 | * ImageMagick tool display |
69 | * |
70 | * @var string |
71 | */ |
72 | public const IMAGEMAGICK_DISPLAY = 'display'; |
73 | |
74 | /** |
75 | * ImageMagick tool identify |
76 | * |
77 | * @var string |
78 | */ |
79 | public const IMAGEMAGICK_IDENTIFY = 'identify'; |
80 | |
81 | /** |
82 | * ImageMagick tool import |
83 | * |
84 | * @var string |
85 | */ |
86 | public const IMAGEMAGICK_IMPORT = 'import'; |
87 | |
88 | /** |
89 | * ImageMagick tool mogrify |
90 | * |
91 | * @var string |
92 | */ |
93 | public const IMAGEMAGICK_MOGRIFY = 'mogrify'; |
94 | |
95 | /** |
96 | * ImageMagick tool montage |
97 | * |
98 | * @var string |
99 | */ |
100 | public const IMAGEMAGICK_MONTAGE = 'montage'; |
101 | |
102 | /** |
103 | * ImageMagick tool stream |
104 | * |
105 | * @var string |
106 | */ |
107 | public const IMAGEMAGICK_STREAM = 'stream'; |
108 | |
109 | /** |
110 | * Path to binaries |
111 | * |
112 | * @var string |
113 | */ |
114 | public string $binPath; |
115 | |
116 | /** |
117 | * Name of binary |
118 | * |
119 | * @var string |
120 | */ |
121 | protected string $bin; |
122 | |
123 | /** |
124 | * Cache controller |
125 | * |
126 | * @var Cache |
127 | */ |
128 | protected Cache|null $cache; |
129 | |
130 | /** |
131 | * The current query |
132 | * |
133 | * @var Query |
134 | */ |
135 | private Query $query; |
136 | |
137 | /** |
138 | * Contructs a new program |
139 | * |
140 | * @param string $binPath |
141 | * Path to binaries |
142 | * @param string $bin |
143 | * Binary |
144 | * @param \Karla\Cache|null $cache |
145 | * Cache controller (default null = no cache) |
146 | * |
147 | * @throws \InvalidArgumentException |
148 | */ |
149 | final public function __construct(string $binPath, string $bin, \Karla\Cache|null $cache = null) |
150 | { |
151 | if ($binPath == '') { |
152 | throw new \InvalidArgumentException('Invalid bin path'); |
153 | } |
154 | if ($bin == '') { |
155 | throw new \InvalidArgumentException('Invalid bin'); |
156 | } |
157 | $this->binPath = $binPath; |
158 | $this->bin = $bin; |
159 | $this->cache = $cache; |
160 | $this->query = new Query(); |
161 | $this->getQuery()->reset(); |
162 | } |
163 | |
164 | /** |
165 | * Get the current query |
166 | * |
167 | * @return Query |
168 | */ |
169 | public function getQuery(): Query |
170 | { |
171 | return $this->query; |
172 | } |
173 | |
174 | /** |
175 | * Set the current query |
176 | * |
177 | * @param Query $query |
178 | * Query to set |
179 | * |
180 | * @return void |
181 | */ |
182 | public function setQuery(Query $query): void |
183 | { |
184 | $this->query = $query; |
185 | } |
186 | |
187 | /** |
188 | * It should not be possible to clon the ImageMagick object. |
189 | * |
190 | * @return void |
191 | */ |
192 | final public function __clone(): void |
193 | { |
194 | throw new \BadMethodCallException("Clone is not allowed"); |
195 | } |
196 | |
197 | /** |
198 | * Get the command to run |
199 | * |
200 | * @return string |
201 | */ |
202 | public function getCommand(): string |
203 | { |
204 | return $this->binPath . $this->bin; |
205 | } |
206 | |
207 | /** |
208 | * Execute the command |
209 | * |
210 | * @param boolean $reset |
211 | * Reset the query |
212 | * |
213 | * @return string |
214 | */ |
215 | public function execute(bool $reset = true): string|object |
216 | { |
217 | $result = shell_exec($this->getCommand()); |
218 | if ($reset) { |
219 | $this->getQuery()->reset(); |
220 | } |
221 | |
222 | return $result; |
223 | } |
224 | |
225 | /** |
226 | * Raw arguments directly to ImageMagick |
227 | * |
228 | * @param string $arguments |
229 | * Arguments |
230 | * @param boolean $input |
231 | * Defaults to an input option, use false to use it as an output option |
232 | * |
233 | * @return self |
234 | */ |
235 | public function raw(string $arguments, bool $input = true): self |
236 | { |
237 | if ($input) { |
238 | $this->getQuery()->setInputOption($arguments); |
239 | } else { |
240 | $this->getQuery()->setOutputOption($arguments); |
241 | } |
242 | return $this; |
243 | } |
244 | |
245 | /** |
246 | * Check if the input is a valid ImageMagick program |
247 | * |
248 | * @param string $program |
249 | * Program name |
250 | * |
251 | * @return boolean |
252 | */ |
253 | final public static function validProgram(string $program): bool |
254 | { |
255 | $class = new \ReflectionClass(__CLASS__); |
256 | $constants = $class->getConstants(); |
257 | foreach ($constants as $constant) { |
258 | if ($constant == $program) { |
259 | return true; |
260 | } |
261 | } |
262 | |
263 | return false; |
264 | } |
265 | } |