-
Notifications
You must be signed in to change notification settings - Fork 13
/
Wrapper.php
197 lines (170 loc) · 5.61 KB
/
Wrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/*
* This file is part of the Snowcap ImBundle package.
*
* (c) Snowcap <shoot@snowcap.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Snowcap\ImBundle;
use Snowcap\ImBundle\Exception\InvalidArgumentException;
use Snowcap\ImBundle\Exception\RuntimeException;
/**
* Im wrapper
*
* Imagemagick command line wrapper
*
* Used by the manager
*
*/
class Wrapper
{
private $processClass;
private $binaryPath;
private $_acceptedBinaries = array(
'animate', 'compare', 'composite',
'conjure', 'convert', 'display',
'identify', 'import', 'mogrify',
'montage', 'stream'
);
/**
* @var int Timeout for the process
*/
private $timeout;
/**
* @param string $processClass The class name of the command line processor
* @param string $binaryPath The path where the Imagemagick binaries lies
* @param int $timeout The timeout in seconds
*/
public function __construct($processClass, $binaryPath = "", $timeout = 60)
{
$this->binaryPath = empty($binaryPath) ? $binaryPath : rtrim($binaryPath, '/') . '/';
$this->processClass = $processClass;
$this->timeout = $timeout;
}
/**
* Shortcut to construct & run an Imagemagick command
*
* @param string $command @see _self::buildCommand
* @param string $inputfile @see _self::buildCommand
* @param array $attributes @see _self::buildCommand
* @param string $outputfile @see _self::buildCommand
*
* @return string
*
* @codeCoverageIgnore
*/
public function run($command, $inputfile, $attributes = array(), $outputfile = "")
{
$commandString = $this->buildCommand($command, $inputfile, $attributes, $outputfile);
return $this->rawRun($commandString);
}
/**
* @param string $command Imagemagick command (convert, mogrify, ...)
* @param string $inputfile Source file to use
* @param array $attributes Array of Imagemagick key/values attributes
* @param string $outputfile Destination file - used when converting
*
* @return string
*/
private function buildCommand($command, $inputfile, $attributes = array(), $outputfile = "")
{
$attributesString = trim($this->prepareAttributes($attributes));
if (strlen($attributesString) > 0) {
$attributesString = " " . $attributesString;
}
if ($outputfile !== "") {
$this->checkDirectory($outputfile);
$commandString = $this->binaryPath . $command . " " . $inputfile . $attributesString . " " . $outputfile;
} else {
$commandString = $this->binaryPath . $command . $attributesString . " " . $inputfile;
}
$this->validateCommand($commandString);
return $commandString;
}
/**
* Run a command. Only use if the run() method doesn't fit your need
*
* @param string $commandString
*
* @return string
* @throws RuntimeException
*/
public function rawRun($commandString)
{
$this->validateCommand($commandString);
/** @var $process \Symfony\Component\Process\Process */
$process = new $this->processClass($commandString);
$process->setTimeout($this->timeout);
$process->run();
if (!$process->isSuccessful()) {
throw new RuntimeException($process->getErrorOutput());
}
return $process->getOutput();
}
/**
* Takes an array of attributes and formats it as CLI parameters
*
* @param array $attributes
*
* @return string
* @throws InvalidArgumentException
*/
private function prepareAttributes($attributes = array())
{
if (!is_array($attributes)) {
throw new InvalidArgumentException("[ImBundle] format attributes must be an array, recieved: " . var_export($attributes, true));
}
$result = "";
foreach ($attributes as $key => $value) {
if ($key === null || $key === "") {
$result .= " " . $value;
} else {
$result .= " -" . $key;
if ($value != "") {
$result .= " \"" . $value . "\"";
}
}
}
return $result;
}
/**
* Creates the given directory if unexistant
*
* @param string $path
*
* @throws RuntimeException
*/
public function checkDirectory($path)
{
$dir = dirname($path);
if (!is_dir($dir)) {
if (false === @mkdir($dir, 0777, true)) {
throw new RuntimeException(sprintf('Unable to create the "%s" directory', $dir));
}
}
}
/**
* Validates that the command launches a Imagemagick command line tool executable
*
* @param string $commandString
*
* @throws InvalidArgumentException
* @return boolean
*/
private function validateCommand($commandString)
{
$cmdParts = explode(" ", $commandString);
if (count($cmdParts) < 2) {
throw new InvalidArgumentException("This command isn't properly structured : '" . $commandString . "'");
}
$binaryPath = $cmdParts[0];
$binaryPathParts = explode('/', $binaryPath);
$binary = $binaryPathParts[count($binaryPathParts)-1];
if (!in_array($binary, $this->_acceptedBinaries)) {
throw new InvalidArgumentException("This command isn't part of the ImageMagick command line tools : '" . $binary . "'");
}
return true;
}
}