Completed
Push — master ( ffb969...161021 )
by Jonathan
04:46
created

Server::getImageAsBase64()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 5
Bugs 1 Features 2
Metric Value
c 5
b 1
f 2
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4286
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
3
namespace League\Glide;
4
5
use InvalidArgumentException;
6
use League\Flysystem\FileExistsException;
7
use League\Flysystem\FilesystemInterface;
8
use League\Glide\Api\ApiInterface;
9
use League\Glide\Filesystem\FileNotFoundException;
10
use League\Glide\Filesystem\FilesystemException;
11
use League\Glide\Responses\ResponseFactoryInterface;
12
13
class Server
14
{
15
    /**
16
     * Source file system.
17
     * @var FilesystemInterface
18
     */
19
    protected $source;
20
21
    /**
22
     * Source path prefix.
23
     * @var string
24
     */
25
    protected $sourcePathPrefix;
26
27
    /**
28
     * Cache file system.
29
     * @var FilesystemInterface
30
     */
31
    protected $cache;
32
33
    /**
34
     * Cache path prefix.
35
     * @var string
36
     */
37
    protected $cachePathPrefix;
38
39
    /**
40
     * Image manipulation API.
41
     * @var ApiInterface
42
     */
43
    protected $api;
44
45
    /**
46
     * Response factory.
47
     * @var ResponseFactoryInterface|null
48
     */
49
    protected $responseFactory;
50
51
    /**
52
     * Base URL.
53
     * @var string
54
     */
55
    protected $baseUrl;
56
57
    /**
58
     * Default image manipulations.
59
     * @var array
60
     */
61
    protected $defaults = [];
62
63
    /**
64
     * Preset image manipulations.
65
     * @var array
66
     */
67
    protected $presets = [];
68
69
    /**
70
     * Create Server instance.
71
     * @param FilesystemInterface $source Source file system.
72
     * @param FilesystemInterface $cache  Cache file system.
73
     * @param ApiInterface        $api    Image manipulation API.
74
     */
75 176
    public function __construct(FilesystemInterface $source, FilesystemInterface $cache, ApiInterface $api)
76
    {
77 176
        $this->setSource($source);
78 176
        $this->setCache($cache);
79 176
        $this->setApi($api);
80 176
    }
81
82
    /**
83
     * Set source file system.
84
     * @param FilesystemInterface $source Source file system.
85
     */
86 176
    public function setSource(FilesystemInterface $source)
87
    {
88 176
        $this->source = $source;
89 176
    }
90
91
    /**
92
     * Get source file system.
93
     * @return FilesystemInterface Source file system.
94
     */
95 8
    public function getSource()
96
    {
97 8
        return $this->source;
98
    }
99
100
    /**
101
     * Set source path prefix.
102
     * @param string $sourcePathPrefix Source path prefix.
103
     */
104 20
    public function setSourcePathPrefix($sourcePathPrefix)
105
    {
106 20
        $this->sourcePathPrefix = trim($sourcePathPrefix, '/');
107 20
    }
108
109
    /**
110
     * Get source path prefix.
111
     * @return string Source path prefix.
112
     */
113 8
    public function getSourcePathPrefix()
114
    {
115 8
        return $this->sourcePathPrefix;
116
    }
117
118
    /**
119
     * Get source path.
120
     * @param  string                $path Image path.
121
     * @return string                The source path.
122
     * @throws FileNotFoundException
123
     */
124 84
    public function getSourcePath($path)
125
    {
126 84
        $path = trim($path, '/');
127
128 84
        if (substr($path, 0, strlen($this->baseUrl)) === $this->baseUrl) {
129 4
            $path = trim(substr($path, strlen($this->baseUrl)), '/');
130 3
        }
131
132 84
        if ($path === '') {
133 4
            throw new FileNotFoundException('Image path missing.');
134
        }
135
136 80
        if ($this->sourcePathPrefix) {
137 8
            $path = $this->sourcePathPrefix.'/'.$path;
138 6
        }
139
140 80
        return rawurldecode($path);
141
    }
142
143
    /**
144
     * Check if a source file exists.
145
     * @param  string $path Image path.
146
     * @return bool   Whether the source file exists.
147
     */
148 24
    public function sourceFileExists($path)
149
    {
150 24
        return $this->source->has($this->getSourcePath($path));
151
    }
152
153
    /**
154
     * Set base URL.
155
     * @param string $baseUrl Base URL.
156
     */
157 16
    public function setBaseUrl($baseUrl)
158
    {
159 16
        $this->baseUrl = trim($baseUrl, '/');
160 16
    }
161
162
    /**
163
     * Get base URL.
164
     * @return string Base URL.
165
     */
166 8
    public function getBaseUrl()
167
    {
168 8
        return $this->baseUrl;
169
    }
170
171
    /**
172
     * Set cache file system.
173
     * @param FilesystemInterface $cache Cache file system.
174
     */
175 176
    public function setCache(FilesystemInterface $cache)
176
    {
177 176
        $this->cache = $cache;
178 176
    }
179
180
    /**
181
     * Get cache file system.
182
     * @return FilesystemInterface Cache file system.
183
     */
184 8
    public function getCache()
185
    {
186 8
        return $this->cache;
187
    }
188
189
    /**
190
     * Set cache path prefix.
191
     * @param string $cachePathPrefix Cache path prefix.
192
     */
193 16
    public function setCachePathPrefix($cachePathPrefix)
194
    {
195 16
        $this->cachePathPrefix = trim($cachePathPrefix, '/');
196 16
    }
197
198
    /**
199
     * Get cache path prefix.
200
     * @return string Cache path prefix.
201
     */
202 8
    public function getCachePathPrefix()
203
    {
204 8
        return $this->cachePathPrefix;
205
    }
206
207
    /**
208
     * Get cache path.
209
     * @param  string $path   Image path.
210
     * @param  array  $params Image manipulation params.
211
     * @return string Cache path.
212
     */
213 60
    public function getCachePath($path, array $params = [])
214
    {
215 60
        $sourcePath = $this->getSourcePath($path);
216
217 60
        if ($this->sourcePathPrefix) {
218 4
            $sourcePath = substr($sourcePath, strlen($this->sourcePathPrefix) + 1);
219 3
        }
220
221 60
        $params = $this->getAllParams($params);
222 60
        unset($params['s'], $params['p']);
223 60
        ksort($params);
224
225 60
        $md5 = md5($sourcePath.'?'.http_build_query($params));
226
227 60
        $path = $sourcePath.'/'.$md5;
228
229 60
        if ($this->cachePathPrefix) {
230 4
            $path = $this->cachePathPrefix.'/'.$path;
231 3
        }
232
233 60
        return $path;
234
    }
235
236
    /**
237
     * Check if a cache file exists.
238
     * @param  string $path   Image path.
239
     * @param  array  $params Image manipulation params.
240
     * @return bool   Whether the cache file exists.
241
     */
242 44
    public function cacheFileExists($path, array $params)
243
    {
244 44
        return $this->cache->has(
245 44
            $this->getCachePath($path, $params)
246 33
        );
247
    }
248
249
    /**
250
     * Delete cached manipulations for an image.
251
     * @param  string $path Image path.
252
     * @return bool   Whether the delete succeeded.
253
     */
254 4
    public function deleteCache($path)
255
    {
256 4
        return $this->cache->deleteDir(
257 4
            dirname($this->getCachePath($path))
258 3
        );
259
    }
260
261
    /**
262
     * Set image manipulation API.
263
     * @param ApiInterface $api Image manipulation API.
264
     */
265 176
    public function setApi(ApiInterface $api)
266
    {
267 176
        $this->api = $api;
268 176
    }
269
270
    /**
271
     * Get image manipulation API.
272
     * @return ApiInterface Image manipulation API.
273
     */
274 8
    public function getApi()
275
    {
276 8
        return $this->api;
277
    }
278
279
    /**
280
     * Set default image manipulations.
281
     * @param array $defaults Default image manipulations.
282
     */
283 20
    public function setDefaults(array $defaults)
284
    {
285 20
        $this->defaults = $defaults;
286 20
    }
287
288
    /**
289
     * Get default image manipulations.
290
     * @return array Default image manipulations.
291
     */
292 8
    public function getDefaults()
293
    {
294 8
        return $this->defaults;
295
    }
296
297
    /**
298
     * Set preset image manipulations.
299
     * @param array $presets Preset image manipulations.
300
     */
301 20
    public function setPresets(array $presets)
302
    {
303 20
        $this->presets = $presets;
304 20
    }
305
306
    /**
307
     * Get preset image manipulations.
308
     * @return array Preset image manipulations.
309
     */
310 8
    public function getPresets()
311
    {
312 8
        return $this->presets;
313
    }
314
315
    /**
316
     * Get all image manipulations params, including defaults and presets.
317
     * @param  array $params Image manipulation params.
318
     * @return array All image manipulation params.
319
     */
320 64
    public function getAllParams(array $params)
321
    {
322 64
        $all = $this->defaults;
323
324 64
        if (isset($params['p'])) {
325 4
            foreach (explode(',', $params['p']) as $preset) {
326 4
                if (isset($this->presets[$preset])) {
327 4
                    $all = array_merge($all, $this->presets[$preset]);
328 3
                }
329 3
            }
330 3
        }
331
332 64
        return array_merge($all, $params);
333
    }
334
335
    /**
336
     * Set response factory.
337
     * @param ResponseFactoryInterface|null $responseFactory Response factory.
338
     */
339 20
    public function setResponseFactory(ResponseFactoryInterface $responseFactory = null)
340
    {
341 20
        $this->responseFactory = $responseFactory;
342 20
    }
343
344
    /**
345
     * Get response factory.
346
     * @return ResponseFactoryInterface Response factory.
347
     */
348 8
    public function getResponseFactory()
349
    {
350 8
        return $this->responseFactory;
351
    }
352
353
    /**
354
     * Generate and return image response.
355
     * @param  string                   $path   Image path.
356
     * @param  array                    $params Image manipulation params.
357
     * @return mixed                    Image response.
358
     * @throws InvalidArgumentException
359
     */
360 8
    public function getImageResponse($path, array $params)
361
    {
362 8
        if (is_null($this->responseFactory)) {
363 4
            throw new InvalidArgumentException(
364 1
                'Unable to get image response, no response factory defined.'
365 3
            );
366
        }
367
368 4
        $path = $this->makeImage($path, $params);
369
370 4
        return $this->responseFactory->create($this->cache, $path);
371
    }
372
373
    /**
374
     * Generate and return Base64 encoded image.
375
     * @param  string              $path   Image path.
376
     * @param  array               $params Image manipulation params.
377
     * @return string              Base64 encoded image.
378
     * @throws FilesystemException
379
     */
380 8
    public function getImageAsBase64($path, array $params)
381
    {
382 8
        $path = $this->makeImage($path, $params);
383
384 8
        $source = $this->cache->read($path);
385
386 8
        if ($source === false) {
387 4
            throw new FilesystemException(
388 4
                'Could not read the image `'.$path.'`.'
389 3
            );
390
        }
391
392 4
        return 'data:'.$this->cache->getMimetype($path).';base64,'.base64_encode($source);
393
    }
394
395
    /**
396
     * Generate and output image.
397
     * @param  string                   $path   Image path.
398
     * @param  array                    $params Image manipulation params.
399
     * @throws InvalidArgumentException
400
     */
401 4
    public function outputImage($path, array $params)
402
    {
403 4
        $path = $this->makeImage($path, $params);
404
405 4
        header('Content-Type:'.$this->cache->getMimetype($path));
406 4
        header('Content-Length:'.$this->cache->getSize($path));
407 4
        header('Cache-Control:'.'max-age=31536000, public');
408 4
        header('Expires:'.date_create('+1 years')->format('D, d M Y H:i:s').' GMT');
409
410 4
        $stream = $this->cache->readStream($path);
411
412 4
        rewind($stream);
413 4
        fpassthru($stream);
414 4
        fclose($stream);
415 4
    }
416
417
    /**
418
     * Generate manipulated image.
419
     * @param  string                $path   Image path.
420
     * @param  array                 $params Image manipulation params.
421
     * @return string                Cache path.
422
     * @throws FileNotFoundException
423
     * @throws FilesystemException
424
     */
425 40
    public function makeImage($path, array $params)
426
    {
427 40
        $sourcePath = $this->getSourcePath($path);
428 40
        $cachedPath = $this->getCachePath($path, $params);
429
430 40
        if ($this->cacheFileExists($path, $params) === true) {
431 20
            return $cachedPath;
432
        }
433
434 20
        if ($this->sourceFileExists($path) === false) {
435 4
            throw new FileNotFoundException(
436 4
                'Could not find the image `'.$sourcePath.'`.'
437 3
            );
438
        }
439
440 16
        $source = $this->source->read(
441
            $sourcePath
442 12
        );
443
444 16
        if ($source === false) {
445 4
            throw new FilesystemException(
446 4
                'Could not read the image `'.$sourcePath.'`.'
447 3
            );
448
        }
449
450
        // We need to write the image to the local disk before
451
        // doing any manipulations. This is because EXIF data
452
        // can only be read from an actual file.
453 12
        $tmp = tempnam(sys_get_temp_dir(), 'Glide');
454
455 12
        if (file_put_contents($tmp, $source) === false) {
456
            throw new FilesystemException(
457
                'Unable to write temp file for `'.$sourcePath.'`.'
458
            );
459
        }
460
461
        try {
462 12
            $write = $this->cache->write(
463 9
                $cachedPath,
464 12
                $this->api->run($tmp, $this->getAllParams($params))
465 9
            );
466
467 8
            if ($write === false) {
468 4
                throw new FilesystemException(
469 5
                    'Could not write the image `'.$cachedPath.'`.'
470 3
                );
471
            }
472 11
        } catch (FileExistsException $exception) {
473
            // This edge case occurs when the target already exists
474
            // because it's currently be written to disk in another
475
            // request. It's best to just fail silently.
476
        }
477
478 8
        unlink($tmp);
479
480 8
        return $cachedPath;
481
    }
482
}
483