forked from snowcap/SnowcapImBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManager.php
307 lines (270 loc) · 7.77 KB
/
Manager.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/*
* This file is part of the Snowcap ImBundle package.
*
* (c) Snowcap <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Snowcap\ImBundle;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Snowcap\ImBundle\Exception\NotFoundException;
use Snowcap\ImBundle\Exception\InvalidArgumentException;
/**
* Im manager
*/
class Manager
{
/**
* @var Wrapper
*/
protected $wrapper;
/**
* @var array
*/
protected $formats;
/**
* @var string
*/
protected $rootDir;
/**
* @var string
*/
protected $webPath;
/**
* @var string
*/
protected $cachePath;
/**
* @param Wrapper $wrapper The ImBundle Wrapper instance
* @param string $rootDir Symfony Kernel root directory
* @param string $webPath Relative path to the web folder (relative to root directory)
* @param string $cachePath Relative path to the images cache folder (relative to web path)
* @param array $formats Formats definition
*/
public function __construct(Wrapper $wrapper, $rootDir, $webPath, $cachePath, $formats = array())
{
$this->wrapper = $wrapper;
$this->formats = $formats;
$this->setRootDir($rootDir);
$this->setWebPath($webPath);
$this->setCachePath($cachePath);
}
/**
* Add a format to the config
*
* @param string $name
* @param string $config
*/
public function addFormat($name, $config)
{
$this->formats[$name] = $config;
}
/**
* @return string
*/
public function getRootDir()
{
return $this->rootDir;
}
/**
* @param string $rootDir
*/
public function setRootDir($rootDir)
{
$this->rootDir = rtrim($rootDir, '/');
}
/**
* @return string
*/
public function getWebPath()
{
return $this->webPath;
}
/**
* @param string $webPath
*/
public function setWebPath($webPath)
{
$this->webPath = trim($webPath, '/');
}
/**
* @return string
*/
public function getWebDirectory()
{
return $this->getRootDir() . '/' . $this->getWebPath();
}
/**
* @return string
*/
public function getCachePath()
{
return $this->cachePath;
}
/**
* @param string $cachePath
*/
public function setCachePath($cachePath)
{
$this->cachePath = trim($cachePath, '/');
}
/**
* @return string
*/
public function getCacheDirectory()
{
return $this->getRootDir() . '/' . $this->getWebPath() . '/' . $this->getCachePath();
}
/**
* To know if a cache exist for a image in a format
*
* @param string $format ImBundle format string
* @param string $path Source file path
*
* @return bool
*/
public function cacheExists($format, $path)
{
return (file_exists($this->getCacheDirectory() . '/' . $format . '/' . $path) === true);
}
/**
* To get a cached image content
*
* @param string $format ImBundle format string
* @param string $path Source file path
*
* @return string
*/
public function getCacheContent($format, $path)
{
return file_get_contents($this->getCacheDirectory() . '/' . $format . '/' . $path);
}
/**
* To get the web path for a format
*
* @param string $format ImBundle format string
* @param string $path Source file path
*
* @return string
*/
public function getUrl($format, $path)
{
return $this->getCachePath() . '/' . $format . '/' . $path;
}
/**
* Shortcut to run a "convert" command => creates a new image
*
* @param string $format ImBundle format string
* @param string $file Source file path
*
* @return string
* @codeCoverageIgnore
*/
public function convert($format, $file)
{
$file = ltrim($file, '/');
$this->checkImage($file);
return $this->wrapper->run("convert", $this->getWebDirectory() . '/' . $file, $this->convertFormat($format), $this->getCacheDirectory() . '/' . $this->pathify($format) . '/' . $file);
}
/**
* Shortcut to run a "mogrify" command => modifies the image source
*
* @param string $format ImBundle format string
* @param string $file Source file path
*
* @return string
* @codeCoverageIgnore
*/
public function mogrify($format, $file)
{
$this->checkImage($file);
return $this->wrapper->run("mogrify", $file, $this->convertFormat($format));
}
/**
* @param string $format ImBundle format string
* @param string $path cached path for an external image - ex: http/somepath/somefile.jpg or https/somepath/someotherfile.jpg
*
* The cached path is equivalent to the original path except that the '://' syntax after the protocol is replaced by a simple "/", to conserve a correct URL encoded string
* The Twig tag 'imResize' will automatically make this conversion for you
*
* @return string
*/
public function downloadExternalImage($format, $path)
{
$protocol = substr($path, 0, strpos($path, '/'));
$newPath = str_replace($protocol . '/', $this->getCacheDirectory() . '/' . $format . '/' . $protocol . '/', $path);
$this->wrapper->checkDirectory($newPath);
$fp = fopen($newPath, 'w');
$ch = curl_init(str_replace($protocol . '/', $protocol . '://', $path));
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return $newPath;
}
/**
* Returns the attributes for converting the image regarding a specific format
*
* @param string $format
*
* @return array
* @throws InvalidArgumentException
*/
private function convertFormat($format)
{
if (is_array($format)) {
// sounds like the format is already done, let's keep it as it is
return $format;
}
if (array_key_exists($format, $this->formats)) {
// it's a format defined in config, let's use all defined parameters
return $this->formats[$format];
} elseif (preg_match("/^([0-9]*)x([0-9]*)/", $format)) {
// it's a custom [width]x[height] format, let's make a thumb
return array('thumbnail' => $format);
} else {
throw new InvalidArgumentException(sprintf("Unknown IM format: %s", $format));
}
}
/**
* Validates that an image exists
*
* @param string $path
*
* @throws NotFoundException
* @throws HttpException
*/
private function checkImage($path)
{
// remove explicit format if present
if(preg_match("/:(?!\/\/)/", $path) === 1) {
$path = preg_split("/:(?!\/\/)/", $path);
$path = $path[1];
}
if (!file_exists($this->getWebDirectory() . '/' . $path) && !file_exists($path)) {
throw new NotFoundException(sprintf("Unable to find image \"%s\"", $path));
}
if (!is_file($this->getWebDirectory() . '/' . $path) && !is_file($path)) {
throw new HttpException(400, sprintf('[ImBundle] "%s" is no file', $path));
}
}
/**
* Takes a format (array or string) and return it as a valid path string
*
* @param mixed $format
*
* @return string
*/
private function pathify($format)
{
if (is_array($format)) {
return md5(serialize($format));
} else {
return $format;
}
}
}