-
Notifications
You must be signed in to change notification settings - Fork 166
/
ImagineController.php
123 lines (103 loc) · 3.45 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Avalanche\Bundle\ImagineBundle\Controller;
use Avalanche\Bundle\ImagineBundle\Imagine\Filter\FilterManager;
use Imagine\Image\ImagineInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Avalanche\Bundle\ImagineBundle\Imagine\CacheManager;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
class ImagineController
{
/**
* @var Request
*/
private $request;
/**
* @var ImagineInterface
*/
private $imagine;
/**
* @var CacheManager
*/
private $cacheManager;
/**
* @var Avalanche\Bundle\ImagineBundle\Imagine\Filter\FilterManager
*/
private $filterManager;
/**
* Constructs by setting $cachePathResolver
*
* @param Request $request
* @param ImagineInterface $imagine
* @param CacheManager $cacheManager
* @param FilterManager $filterManager
*/
public function __construct(ImagineInterface $imagine, CacheManager $cacheManager, FilterManager $filterManager)
{
$this->imagine = $imagine;
$this->cacheManager = $cacheManager;
$this->filterManager = $filterManager;
}
/**
* This action applies a given filter to a given image, saves the image and
* outputs it to the browser at the same time
*
* @param string $path
* @param string $filter
*
* @return Response
*/
public function filter($path, $filter)
{
try {
$cachedPath = $this->cacheManager->cacheImage($this->request->getBaseUrl(), $path, $filter);
} catch (RouteNotFoundException $e) {
throw new NotFoundHttpException('Filter doesn\'t exist.');
}
// if cache path cannot be determined, return 404
if (null === $cachedPath) {
throw new NotFoundHttpException('Image doesn\'t exist');
}
ob_start();
try {
$format = $this->filterManager->getOption($filter, "format", "png");
$this->imagine->open($cachedPath)->show($format);
$type = 'image/' . $format;
$length = ob_get_length();
$content = ob_get_clean();
// TODO: add more media headers
$response = new Response($content, 201, array(
'content-type' => $type,
'content-length' => $length,
));
// Cache
$cacheType = $this->filterManager->getOption($filter, "cache_type", false);
if (false == $cacheType) {
return $response;
}
($cacheType === "public") ? $response->setPublic() : $response->setPrivate();
$cacheExpires = $this->filterManager->getOption($filter, "cache_expires", "1 day");
$expirationDate = new \DateTime("+" . $cacheExpires);
$maxAge = $expirationDate->format("U") - time();
if ($maxAge < 0) {
throw new \InvalidArgumentException("Invalid cache expiration date");
}
$response->setExpires($expirationDate);
$response->setMaxAge($maxAge);
return $response;
} catch (\Exception $e) {
ob_end_clean();
throw $e;
}
}
/**
* Set the request
*
* @param Request $request
*/
public function setRequest(Request $request = null)
{
$this->request = $request;
}
}