-
Notifications
You must be signed in to change notification settings - Fork 378
/
ImagineController.php
103 lines (89 loc) · 3.27 KB
/
ImagineController.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
<?php
namespace Liip\ImagineBundle\Controller;
use Imagine\Image\ImagineInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Data\DataManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\UriSigner;
class ImagineController
{
/**
* @var DataManager
*/
protected $dataManager;
/**
* @var FilterManager
*/
protected $filterManager;
/**
* @var CacheManager
*/
protected $cacheManager;
/**
* @var UriSigner
*/
protected $uriSigner;
/**
* @param DataManager $dataManager
* @param FilterManager $filterManager
* @param CacheManager $cacheManager
* @param UriSigner $uriSigner
*/
public function __construct(
DataManager $dataManager,
FilterManager $filterManager,
CacheManager $cacheManager,
UriSigner $uriSigner
) {
$this->dataManager = $dataManager;
$this->filterManager = $filterManager;
$this->cacheManager = $cacheManager;
$this->uriSigner = $uriSigner;
}
/**
* This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time.
*
* @param Request $request
* @param string $path
* @param string $filter
*
* @throws \RuntimeException
* @throws BadRequestHttpException
*
* @return RedirectResponse
*/
public function filterAction(Request $request, $path, $filter)
{
try {
$runtimeConfig = array();
$pathPrefix = '';
if ($runtimeFilters = $request->query->get('filters', array())) {
if (false == $this->uriSigner->check($request->getSchemeAndHttpHost().$request->getRequestUri())) {
throw new BadRequestHttpException('Signed url does not pass the sign check. Maybe it was modified by someone.');
}
$runtimeConfig['filters'] = $runtimeFilters;
$pathPrefix = substr($request->query->get('_hash'), 0, 8).'/';
}
if (!$this->cacheManager->isStored($path, $filter)) {
try {
$binary = $this->dataManager->find($filter, $path);
} catch (NotLoadableException $e) {
throw new NotFoundHttpException('Source image could not be found', $e);
}
$this->cacheManager->store(
$this->filterManager->applyFilter($binary, $filter, $runtimeConfig),
$pathPrefix.$path,
$filter
);
}
return new RedirectResponse($this->cacheManager->resolve($pathPrefix.$path, $filter), 301);
} catch (RuntimeException $e) {
throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $pathPrefix.$path, $filter, $e->getMessage()), 0, $e);
}
}
}