1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Utility; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\Generator\ImageGenerator; |
6
|
|
|
use MediaMonks\SonataMediaBundle\Handler\ParameterBag; |
7
|
|
|
use MediaMonks\SonataMediaBundle\Handler\ParameterHandlerInterface; |
8
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
9
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
|
12
|
|
|
class ImageUtility |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ParameterHandlerInterface |
16
|
|
|
*/ |
17
|
|
|
private $parameterHandler; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ImageGenerator |
21
|
|
|
*/ |
22
|
|
|
private $imageGenerator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $mediaBaseUrl; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $cacheTtl; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ParameterHandlerInterface $parameterHandler |
36
|
|
|
* @param ImageGenerator $imageGenerator |
37
|
|
|
* @param string $mediaBaseUrl |
38
|
|
|
* @param int $cacheTtl |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
ParameterHandlerInterface $parameterHandler, |
42
|
|
|
ImageGenerator $imageGenerator, |
43
|
|
|
$mediaBaseUrl, |
44
|
|
|
$cacheTtl |
45
|
|
|
) { |
46
|
|
|
$this->parameterHandler = $parameterHandler; |
47
|
|
|
$this->imageGenerator = $imageGenerator; |
48
|
|
|
$this->mediaBaseUrl = $mediaBaseUrl; |
49
|
|
|
$this->cacheTtl = $cacheTtl; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $mediaBaseUrl |
54
|
|
|
*/ |
55
|
|
|
public function setMediaBaseUrl($mediaBaseUrl) |
56
|
|
|
{ |
57
|
|
|
$this->mediaBaseUrl = $mediaBaseUrl; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param MediaInterface $media |
62
|
|
|
* @param int $width |
63
|
|
|
* @param int $height |
64
|
|
|
* @param array $extra |
65
|
|
|
* @return RedirectResponse |
66
|
|
|
*/ |
67
|
|
|
public function getRedirectResponse(MediaInterface $media, $width, $height, array $extra = []) |
68
|
|
|
{ |
69
|
|
|
$response = new RedirectResponse($this->mediaBaseUrl.$this->getFilename($media, $width, $height, $extra)); |
70
|
|
|
$response->setSharedMaxAge($this->cacheTtl); |
71
|
|
|
$response->setMaxAge($this->cacheTtl); |
72
|
|
|
|
73
|
|
|
return $response; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param MediaInterface $media |
78
|
|
|
* @param int $width |
79
|
|
|
* @param int $height |
80
|
|
|
* @param array $parameters |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getFilename(MediaInterface $media, $width, $height, array $parameters) |
84
|
|
|
{ |
85
|
|
|
$parameterBag = $this->parameterHandler->getPayload($media->getId(), $width, $height, $parameters); |
86
|
|
|
$filename = $this->imageGenerator->generate($media, $parameterBag); |
87
|
|
|
|
88
|
|
|
return $filename; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|