Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
79.17% |
19 / 24 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
Karla | |
79.17% |
19 / 24 |
|
50.00% |
3 / 6 |
17.03 | |
0.00% |
0 / 1 |
perform | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
__construct | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
raw | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
convert | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
identify | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
composite | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
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 2010-06-05 |
13 | */ |
14 | |
15 | declare(strict_types=1); |
16 | |
17 | namespace Karla; |
18 | |
19 | /** |
20 | * Karla core class |
21 | * |
22 | * @category Utility |
23 | * @author Johannes Skov Frandsen <jsf@greenoak.dk> |
24 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
25 | * @link https://github.com/localgod/karla Karla |
26 | */ |
27 | class Karla |
28 | { |
29 | /** |
30 | * Path to imagemagick binary |
31 | * |
32 | * @var string $binPath imagemagick binary |
33 | */ |
34 | private string $binPath; |
35 | |
36 | /** |
37 | * Cache controller |
38 | * |
39 | * @var Cache |
40 | */ |
41 | private Cache|null $cache; |
42 | |
43 | /** |
44 | * Instance of a imagmagick object. |
45 | * |
46 | * @var Karla $instance imagmagick object. |
47 | */ |
48 | private static $instance; |
49 | |
50 | /** |
51 | * Get a instance of Karla. |
52 | * |
53 | * @param string $binPath |
54 | * Path to imagemagic binaries (optional) |
55 | * @param Cache|null $cache |
56 | * Cache controller (optional) |
57 | * |
58 | * @return Karla |
59 | * @throws \InvalidArgumentException |
60 | */ |
61 | public static function perform(string $binPath = '/opt/local/bin/', Cache|null $cache = null): Karla |
62 | { |
63 | if (! (Karla::$instance instanceof Karla)) { |
64 | try { |
65 | Karla::$instance = new Karla($binPath, $cache); |
66 | } catch (\InvalidArgumentException $e) { |
67 | throw new \RuntimeException($e->getMessage() . '(' . $binPath . ')'); |
68 | } |
69 | } |
70 | |
71 | return Karla::$instance; |
72 | } |
73 | |
74 | /** |
75 | * Construct a new Karla object. |
76 | * |
77 | * @param string $binPath |
78 | * Path to imagemagic binaries |
79 | * @param Cache $cache |
80 | * Cache controller |
81 | * |
82 | * @throws \InvalidArgumentException if path for imagemagick is invalid |
83 | */ |
84 | private function __construct(string $binPath, Cache|null $cache) |
85 | { |
86 | if (! file_exists($binPath)) { |
87 | throw new \InvalidArgumentException('Bin path not found'); |
88 | } |
89 | |
90 | if (shell_exec($binPath . 'convert -version | grep ImageMagick') == "") { |
91 | throw new \InvalidArgumentException('ImageMagick could not be located at specified path'); |
92 | } |
93 | $this->binPath = 'export PATH=$PATH:' . $binPath . ';'; |
94 | $this->cache = $cache; |
95 | } |
96 | |
97 | /** |
98 | * Run a raw ImageMagick command |
99 | * |
100 | * Karla was never intended to wrap all of the functionality of ImageMagick |
101 | * and likely never will, you will from time to time need to write arguments |
102 | * to ImageMagick like you would have done directly in the consol. |
103 | * |
104 | * @param string $program |
105 | * Imagemagick tool to use |
106 | * @param string $arguments |
107 | * Arguments for the tool |
108 | * |
109 | * @return string Result of the command if any |
110 | * @throws \InvalidArgumentException if you try to run a non ImageMagick program |
111 | */ |
112 | public function raw(string $program, string $arguments = ""): string |
113 | { |
114 | if (! Program\ImageMagick::validProgram($program)) { |
115 | throw new \InvalidArgumentException('ImageMagick could not be located at specified path'); |
116 | } |
117 | strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ? $bin = $program . '.exe' : $bin = $program; |
118 | |
119 | return shell_exec($this->binPath . $bin . ' ' . $arguments); |
120 | } |
121 | |
122 | /** |
123 | * Start a convert operation |
124 | * |
125 | * @return Program\Convert |
126 | */ |
127 | public function convert(): Program\Convert |
128 | { |
129 | $bin = strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ? |
130 | Program\ImageMagick::IMAGEMAGICK_CONVERT . '.exe' : Program\ImageMagick::IMAGEMAGICK_CONVERT; |
131 | |
132 | return new Program\Convert($this->binPath, $bin, $this->cache); |
133 | } |
134 | |
135 | /** |
136 | * Start a identify operation |
137 | * |
138 | * @return Program\Identify |
139 | */ |
140 | public function identify(): Program\Identify |
141 | { |
142 | $bin = strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ? |
143 | Program\ImageMagick::IMAGEMAGICK_IDENTIFY . '.exe' : Program\ImageMagick::IMAGEMAGICK_IDENTIFY; |
144 | |
145 | return new Program\Identify($this->binPath, $bin, $this->cache); |
146 | } |
147 | |
148 | /** |
149 | * Start a composite operation |
150 | * |
151 | * @return Program\Composite |
152 | */ |
153 | public function composite(): Program\Composite |
154 | { |
155 | $bin = strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ? |
156 | Program\ImageMagick::IMAGEMAGICK_COMPOSITE . '.exe' : Program\ImageMagick::IMAGEMAGICK_COMPOSITE; |
157 | |
158 | return new Program\Composite($this->binPath, $bin, $this->cache); |
159 | } |
160 | } |