Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
62.75% |
32 / 51 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
Support | |
62.75% |
32 / 51 |
|
0.00% |
0 / 5 |
64.69 | |
0.00% |
0 / 1 |
gravity | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
5.20 | |||
imageTypes | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
5.20 | |||
colorSpace | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
5.20 | |||
layerMethod | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
5.03 | |||
supportedFormat | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
56 |
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 2013-05-26 |
13 | */ |
14 | |
15 | declare(strict_types=1); |
16 | |
17 | namespace Karla; |
18 | |
19 | use Karla\Program; |
20 | use Karla\Program\Identify; |
21 | use Karla\Program\Convert; |
22 | use Karla\Program\ImageMagick; |
23 | use Karla\Program\Composite; |
24 | |
25 | /** |
26 | * Class for quering for supported features |
27 | * |
28 | * @category Utility |
29 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
30 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
31 | * @link https://github.com/localgod/karla Karla |
32 | */ |
33 | class Support |
34 | { |
35 | /** |
36 | * Check if a gravity is supported by ImageMagick. |
37 | * |
38 | * @param Program $program |
39 | * Program to check |
40 | * @param string $gravity |
41 | * Gravity to check |
42 | * |
43 | * @return boolean |
44 | * @throws \BadMethodCallException if called in a wrong context |
45 | */ |
46 | public static function gravity(Program $program, string $gravity): bool |
47 | { |
48 | if (! ($program instanceof Convert) && ! ($program instanceof Composite)) { |
49 | $message = 'This method can not be used in this context. (' . get_class($program) . ')'; |
50 | throw new \BadMethodCallException($message); |
51 | } |
52 | $bin = ImageMagick::IMAGEMAGICK_CONVERT . (strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ? '.exe' : ''); |
53 | $gravities = shell_exec($program->binPath . $bin . ' -list gravity'); |
54 | $gravities = explode("\n", $gravities); |
55 | $count = count($gravities); |
56 | for ($i = 0; $i < $count; $i++) { |
57 | $gravities[$i] = trim(strtolower($gravities[$i])); |
58 | } |
59 | |
60 | return in_array(strtolower(trim($gravity)), $gravities); |
61 | } |
62 | |
63 | /** |
64 | * Check if a image type is supported by the ImageMagick program. |
65 | * |
66 | * @param Program $program |
67 | * Program to check |
68 | * @param string $type |
69 | * Type to check |
70 | * |
71 | * @return boolean |
72 | */ |
73 | public static function imageTypes(Program $program, string $type): bool |
74 | { |
75 | if (! ($program instanceof Convert) && ! ($program instanceof Identify)) { |
76 | $message = 'This method can not be used in this context. (' . get_class($program) . ')'; |
77 | throw new \BadMethodCallException($message); |
78 | } |
79 | $bin = ImageMagick::IMAGEMAGICK_CONVERT . (strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ? '.exe' : ''); |
80 | $types = shell_exec($program->binPath . $bin . ' -list type'); |
81 | $types = explode("\n", $types); |
82 | $count = count($types); |
83 | for ($i = 0; $i < $count; $i++) { |
84 | $types[$i] = trim(strtolower($types[$i])); |
85 | } |
86 | |
87 | return in_array(strtolower(trim($type)), $types); |
88 | } |
89 | |
90 | /** |
91 | * Check if a colorspace is supported by the ImageMagick program. |
92 | * |
93 | * @param Program $program |
94 | * Program to check |
95 | * @param string $colorSpace |
96 | * Colorspace to check |
97 | * |
98 | * @return boolean |
99 | */ |
100 | public static function colorSpace(Program $program, string $colorSpace): bool |
101 | { |
102 | if (! ($program instanceof Convert) && ! ($program instanceof Identify)) { |
103 | $message = 'This method can not be used in this context. (' . get_class($program) . ')'; |
104 | throw new \BadMethodCallException($message); |
105 | } |
106 | $bin = ImageMagick::IMAGEMAGICK_CONVERT . (strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ? '.exe' : ''); |
107 | $colorspaces = shell_exec($program->binPath . $bin . ' -list colorspace'); |
108 | $colorspaces = explode("\n", $colorspaces); |
109 | $count = count($colorspaces); |
110 | for ($i = 0; $i < $count; $i++) { |
111 | $colorspaces[$i] = trim(strtolower($colorspaces[$i])); |
112 | } |
113 | |
114 | return in_array(strtolower(trim($colorSpace)), $colorspaces); |
115 | } |
116 | |
117 | /** |
118 | * Check if a method is supported by ImageMagick. |
119 | * |
120 | * @param Program $program |
121 | * Program to check |
122 | * @param string $method |
123 | * Method to check |
124 | * |
125 | * @return boolean |
126 | * @throws \BadMethodCallException if called in a wrong context |
127 | */ |
128 | public static function layerMethod(Program $program, string $method): bool |
129 | { |
130 | if (! ($program instanceof Convert) && ! ($program instanceof Identify)) { |
131 | throw new \BadMethodCallException('This method can not be used in this context'); |
132 | } |
133 | $bin = ImageMagick::IMAGEMAGICK_CONVERT . (strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ? '.exe' : ''); |
134 | $methods = shell_exec($program->binPath . $bin . ' -list layers'); |
135 | $methods = explode("\n", $methods); |
136 | $count = count($methods); |
137 | for ($i = 0; $i < $count; $i++) { |
138 | $methods[$i] = trim(strtolower($methods[$i])); |
139 | } |
140 | |
141 | return in_array(strtolower(trim($method)), $methods); |
142 | } |
143 | |
144 | /** |
145 | * Check if a format is supported by ImageMagick. |
146 | * |
147 | * @param Program $program |
148 | * Program to check |
149 | * @param string $format |
150 | * Format to check |
151 | * |
152 | * @return boolean |
153 | * @throws \BadMethodCallException if called in a wrong context |
154 | */ |
155 | public static function supportedFormat(Program $program, string $format): bool |
156 | { |
157 | if (! ($program instanceof Convert) && ! ($program instanceof Identify)) { |
158 | throw new \BadMethodCallException('This method can not be used in this context'); |
159 | } |
160 | $bin = ImageMagick::IMAGEMAGICK_CONVERT . (strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ? '.exe' : ''); |
161 | $formats = shell_exec($program->binPath . $bin . ' -list format'); |
162 | $formats = explode("\n", $formats); |
163 | $count = count($formats); |
164 | for ($i = 0; $i < $count; $i++) { |
165 | $matches = []; |
166 | preg_match("/^[\s]*[A-Z0-9]+/", $formats[$i], $matches); |
167 | if (isset($matches[0]) && ! strpos($matches[0], 'Format')) { |
168 | $formats[$i] = strtolower(trim($matches[0])); |
169 | } |
170 | } |
171 | |
172 | return in_array(strtolower(trim($format)), $formats); |
173 | } |
174 | } |