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