|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jb\Bundle\PhumborBundle\Transformer; |
|
4
|
|
|
|
|
5
|
|
|
use Thumbor\Url\BuilderFactory; |
|
6
|
|
|
use Thumbor\Url\Builder; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Description of BaseTransformer |
|
10
|
|
|
* |
|
11
|
|
|
* @author jobou |
|
12
|
|
|
*/ |
|
13
|
|
|
class BaseTransformer |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Method name for each operation |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected static $filterMethod = array( |
|
21
|
|
|
'trim' => 'trim', |
|
22
|
|
|
'crop' => 'crop', |
|
23
|
|
|
'fit_in' => 'fitIn', |
|
24
|
|
|
'full_fit_in' => 'fullFitIn', |
|
25
|
|
|
'resize' => 'resize', |
|
26
|
|
|
'halign' => 'halign', |
|
27
|
|
|
'valign' => 'valign', |
|
28
|
|
|
'smart_crop' => 'smartCrop', |
|
29
|
|
|
'metadata_only' => 'metadataOnly', |
|
30
|
|
|
'filters' => 'filters' |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Phumbor Builder Factory |
|
35
|
|
|
* |
|
36
|
|
|
* @var \Thumbor\Url\BuilderFactory |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $factory; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The configured transformations |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $transformations; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Constructor |
|
49
|
|
|
* |
|
50
|
|
|
* @param \Thumbor\Url\BuilderFactory $factory |
|
51
|
|
|
* @param array $transformations |
|
52
|
|
|
*/ |
|
53
|
20 |
|
public function __construct(BuilderFactory $factory, array $transformations) |
|
54
|
|
|
{ |
|
55
|
20 |
|
$this->factory = $factory; |
|
56
|
20 |
|
$this->transformations = $transformations; |
|
57
|
20 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Transform an url or path for thumbor |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $orig the original url or path |
|
63
|
|
|
* @param string $transformation the name of the transformation to apply to the original image |
|
64
|
|
|
* @param array $overrides an array of additionnal filters to override the ones from the transformation |
|
65
|
|
|
* |
|
66
|
|
|
* @return \Thumbor\Url\Builder |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Jb\Bundle\PhumborBundle\Transformer\Exception\UnknownTransformationException |
|
69
|
|
|
*/ |
|
70
|
17 |
|
public function transform($orig, $transformation = null, $overrides = array()) |
|
71
|
|
|
{ |
|
72
|
17 |
|
$url = $this->factory->url($orig); |
|
73
|
17 |
|
if (is_null($transformation) && count($overrides) == 0) { |
|
74
|
1 |
|
return $url; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Check if a transformation is given without overrides |
|
78
|
16 |
|
if (!isset($this->transformations[$transformation]) && count($overrides) == 0) { |
|
79
|
1 |
|
throw new Exception\UnknownTransformationException( |
|
80
|
1 |
|
"Unknown transformation $transformation. Use on of " |
|
81
|
1 |
|
. "the following ".implode(', ', array_keys($this->transformations)) |
|
82
|
1 |
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// Override transformation configuration with custom values |
|
86
|
15 |
|
$configuration = array(); |
|
87
|
15 |
|
if (isset($this->transformations[$transformation])) { |
|
88
|
4 |
|
$configuration = $this->transformations[$transformation]; |
|
89
|
4 |
|
} |
|
90
|
15 |
|
$configuration = array_merge($configuration, $overrides); |
|
91
|
|
|
|
|
92
|
|
|
// Build url from transformation configuration |
|
93
|
15 |
|
foreach ($configuration as $filter => $arguments) { |
|
94
|
15 |
|
$method = self::$filterMethod[$filter]; |
|
95
|
15 |
|
$this->$method($url, $arguments); |
|
96
|
15 |
|
} |
|
97
|
|
|
|
|
98
|
15 |
|
return $url; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Apply trim filter |
|
103
|
|
|
* |
|
104
|
|
|
* @param \Thumbor\Url\Builder $url |
|
105
|
|
|
* @param bool|string $args |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
2 |
|
protected function trim(Builder $url, $args) |
|
110
|
|
|
{ |
|
111
|
2 |
|
$args = (is_string($args)) ? $args : null; |
|
112
|
2 |
|
$url->trim($args); |
|
113
|
2 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Apply resize filter |
|
117
|
|
|
* |
|
118
|
|
|
* @param \Thumbor\Url\Builder $url |
|
119
|
|
|
* @param array $args |
|
120
|
|
|
* |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
5 |
|
protected function resize(Builder $url, $args) |
|
124
|
|
|
{ |
|
125
|
5 |
|
$url->resize($args['width'], $args['height']); |
|
126
|
5 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Apply crop filter |
|
130
|
|
|
* |
|
131
|
|
|
* @param \Thumbor\Url\Builder $url |
|
132
|
|
|
* @param array $args |
|
133
|
|
|
* |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
1 |
|
protected function crop(Builder $url, $args) |
|
137
|
|
|
{ |
|
138
|
1 |
|
$url->crop($args['top_left_x'], $args['top_left_y'], $args['bottom_right_x'], $args['bottom_right_y']); |
|
139
|
1 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Apply fitIn filter |
|
143
|
|
|
* |
|
144
|
|
|
* @param \Thumbor\Url\Builder $url |
|
145
|
|
|
* @param array $args |
|
146
|
|
|
* |
|
147
|
|
|
* @return void |
|
148
|
|
|
*/ |
|
149
|
2 |
|
protected function fitIn(Builder $url, $args) |
|
150
|
|
|
{ |
|
151
|
2 |
|
$url->fitIn($args['width'], $args['height']); |
|
152
|
2 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Apply fullFitIn filter |
|
156
|
|
|
* |
|
157
|
|
|
* @param \Thumbor\Url\Builder $url |
|
158
|
|
|
* @param array $args |
|
159
|
|
|
* |
|
160
|
|
|
* @return void |
|
161
|
|
|
*/ |
|
162
|
1 |
|
protected function fullFitIn(Builder $url, $args) |
|
163
|
|
|
{ |
|
164
|
1 |
|
$url->fullFitIn($args['width'], $args['height']); |
|
165
|
1 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Apply halign filter |
|
169
|
|
|
* |
|
170
|
|
|
* @param \Thumbor\Url\Builder $url |
|
171
|
|
|
* @param array $args |
|
172
|
|
|
* |
|
173
|
|
|
* @return void |
|
174
|
|
|
*/ |
|
175
|
1 |
|
protected function halign(Builder $url, $args) |
|
176
|
|
|
{ |
|
177
|
1 |
|
$url->halign($args); |
|
178
|
1 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Apply valign filter |
|
182
|
|
|
* |
|
183
|
|
|
* @param \Thumbor\Url\Builder $url |
|
184
|
|
|
* @param array $args |
|
185
|
|
|
* |
|
186
|
|
|
* @return void |
|
187
|
|
|
*/ |
|
188
|
1 |
|
protected function valign(Builder $url, $args) |
|
189
|
|
|
{ |
|
190
|
1 |
|
$url->valign($args); |
|
191
|
1 |
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Apply smartCrop filter |
|
195
|
|
|
* |
|
196
|
|
|
* @param \Thumbor\Url\Builder $url |
|
197
|
|
|
* @param array $args |
|
198
|
|
|
* |
|
199
|
|
|
* @return void |
|
200
|
|
|
*/ |
|
201
|
1 |
|
protected function smartCrop(Builder $url, $args) |
|
202
|
|
|
{ |
|
203
|
1 |
|
$url->smartCrop($args); |
|
204
|
1 |
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Request metadata endpoint |
|
208
|
|
|
* |
|
209
|
|
|
* @param \Thumbor\Url\Builder $url |
|
210
|
|
|
* @param array $args |
|
211
|
|
|
* |
|
212
|
|
|
* @return void |
|
213
|
|
|
*/ |
|
214
|
1 |
|
protected function metadataOnly(Builder $url, $args) |
|
215
|
|
|
{ |
|
216
|
1 |
|
$url->metadataOnly($args); |
|
217
|
1 |
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Apply filters |
|
221
|
|
|
* |
|
222
|
|
|
* @param \Thumbor\Url\Builder $url |
|
223
|
|
|
* @param array $args |
|
224
|
|
|
* |
|
225
|
|
|
* @return void |
|
226
|
|
|
*/ |
|
227
|
1 |
|
protected function filters(Builder $url, $args) |
|
228
|
|
|
{ |
|
229
|
1 |
|
foreach ($args as $arg) { |
|
230
|
1 |
|
$arguments = (is_array($arg['arguments'])) ? $arg['arguments'] : array($arg['arguments']); |
|
231
|
1 |
|
array_unshift($arguments, $arg['name']); |
|
232
|
1 |
|
call_user_func_array(array($url, 'addFilter'), $arguments); |
|
233
|
1 |
|
} |
|
234
|
1 |
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Setter allowing for factory override |
|
238
|
|
|
* |
|
239
|
|
|
* @param BuildFactory $factory |
|
240
|
|
|
* |
|
241
|
|
|
*/ |
|
242
|
|
|
public function setFactory(BuildFactory $factory) |
|
243
|
|
|
{ |
|
244
|
|
|
$this->factory = $factory; |
|
|
|
|
|
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..