|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\MediaBundle\Thumbnail; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\MediaBundle\Model\MediaInterface; |
|
15
|
|
|
use Sonata\MediaBundle\Provider\MediaProviderInterface; |
|
16
|
|
|
use Sonata\MediaBundle\Resizer\ResizerInterface; |
|
17
|
|
|
|
|
18
|
|
|
class FormatThumbnail implements ThumbnailInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $defaultFormat; |
|
24
|
|
|
|
|
25
|
|
|
private $resizers = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $defaultFormat |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($defaultFormat) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->defaultFormat = $defaultFormat; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $id |
|
37
|
|
|
* @param ResizerInterface $resizer |
|
38
|
|
|
*/ |
|
39
|
|
|
public function addResizer($id, ResizerInterface $resizer) |
|
40
|
|
|
{ |
|
41
|
|
|
if (!isset($this->resizers[$id])) { |
|
42
|
|
|
$this->resizers[$id] = $resizer; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $id |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
* |
|
51
|
|
|
* @return ResizerInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getResizer($id) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!isset($this->resizers[$id])) { |
|
56
|
|
|
throw new \Exception('Resizer with id: '.$id.' is not attached.'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->resizers[$id]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function generatePublicUrl(MediaProviderInterface $provider, MediaInterface $media, $format) |
|
66
|
|
|
{ |
|
67
|
|
|
if ($format == 'reference') { |
|
68
|
|
|
$path = $provider->getReferenceImage($media); |
|
69
|
|
|
} else { |
|
70
|
|
|
$path = sprintf('%s/thumb_%s_%s.%s', $provider->generatePath($media), $media->getId(), $format, $this->getExtension($media)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $path; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function generatePrivateUrl(MediaProviderInterface $provider, MediaInterface $media, $format) |
|
80
|
|
|
{ |
|
81
|
|
|
if ('reference' === $format) { |
|
82
|
|
|
return $provider->getReferenceImage($media); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return sprintf( |
|
86
|
|
|
'%s/thumb_%s_%s.%s', |
|
87
|
|
|
$provider->generatePath($media), |
|
88
|
|
|
$media->getId(), |
|
89
|
|
|
$format, |
|
90
|
|
|
$this->getExtension($media) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function generate(MediaProviderInterface $provider, MediaInterface $media) |
|
98
|
|
|
{ |
|
99
|
|
|
if (!$provider->requireThumbnails()) { |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$referenceFile = $provider->getReferenceFile($media); |
|
104
|
|
|
|
|
105
|
|
|
if (!$referenceFile->exists()) { |
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
foreach ($provider->getFormats() as $format => $settings) { |
|
110
|
|
|
if (substr($format, 0, strlen($media->getContext())) == $media->getContext() || $format === 'admin') { |
|
111
|
|
|
$resizer = (isset($settings['resizer']) && ($settings['resizer'])) ? $this->getResizer($settings['resizer']) : $provider->getResizer(); |
|
112
|
|
|
$resizer->resize( |
|
113
|
|
|
$media, |
|
114
|
|
|
$referenceFile, |
|
115
|
|
|
$provider->getFilesystem()->get($provider->generatePrivateUrl($media, $format), true), |
|
116
|
|
|
$this->getExtension($media), |
|
117
|
|
|
$settings |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public function delete(MediaProviderInterface $provider, MediaInterface $media, $formats = null) |
|
127
|
|
|
{ |
|
128
|
|
|
if (is_null($formats)) { |
|
129
|
|
|
$formats = array_keys($provider->getFormats()); |
|
130
|
|
|
} elseif (is_string($formats)) { |
|
131
|
|
|
$formats = array($formats); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (!is_array($formats)) { |
|
135
|
|
|
throw new \InvalidArgumentException('"Formats" argument should be string or array'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
foreach ($formats as $format) { |
|
139
|
|
|
$path = $provider->generatePrivateUrl($media, $format); |
|
140
|
|
|
if ($path && $provider->getFilesystem()->has($path)) { |
|
141
|
|
|
$provider->getFilesystem()->delete($path); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param MediaInterface $media |
|
148
|
|
|
* |
|
149
|
|
|
* @return string the file extension for the $media, or the $defaultExtension if not available |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function getExtension(MediaInterface $media) |
|
152
|
|
|
{ |
|
153
|
|
|
$ext = $media->getExtension(); |
|
154
|
|
|
if (!is_string($ext) || strlen($ext) < 3) { |
|
155
|
|
|
$ext = $this->defaultFormat; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $ext; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|