1 | <?php |
||
14 | class ImageTransformer |
||
15 | { |
||
16 | use Utils\CacheMessageTrait; |
||
17 | use Utils\AttributeTrait; |
||
18 | use Utils\StreamTrait; |
||
19 | |||
20 | const KEY_GENERATOR = 'IMAGE_TRANSFORMER'; |
||
21 | |||
22 | /** |
||
23 | * @var array|false Enable client hints |
||
24 | */ |
||
25 | private $clientHints = false; |
||
26 | |||
27 | /** |
||
28 | * @var array Available sizes |
||
29 | */ |
||
30 | private $sizes = []; |
||
31 | |||
32 | /** |
||
33 | * Returns a callable to generate urls. |
||
34 | * |
||
35 | * @param ServerRequestInterface $request |
||
36 | * |
||
37 | * @return callable|null |
||
38 | */ |
||
39 | public static function getGenerator(ServerRequestInterface $request) |
||
43 | |||
44 | /** |
||
45 | * Define the available sizes, for example: |
||
46 | * [ |
||
47 | * 'small' => 'resizeCrop,50,50', |
||
48 | * 'medium' => 'resize,500', |
||
49 | * 'big' => 'resize,1000', |
||
50 | * ]. |
||
51 | * |
||
52 | * @param array $sizes |
||
53 | */ |
||
54 | public function __construct(array $sizes) |
||
71 | |||
72 | /** |
||
73 | * To save the transformed images in the cache. |
||
74 | * |
||
75 | * @param CacheItemPoolInterface $cache |
||
76 | * |
||
77 | * @return self |
||
78 | */ |
||
79 | public function cache(CacheItemPoolInterface $cache) |
||
85 | |||
86 | /** |
||
87 | * Enable the client hints. |
||
88 | * |
||
89 | * @param array $clientHints |
||
90 | * |
||
91 | * @return self |
||
92 | */ |
||
93 | public function clientHints($clientHints = ['Dpr', 'Viewport-Width', 'Width']) |
||
99 | |||
100 | /** |
||
101 | * Execute the middleware. |
||
102 | * |
||
103 | * @param ServerRequestInterface $request |
||
104 | * @param ResponseInterface $response |
||
105 | * @param callable $next |
||
106 | * |
||
107 | * @return ResponseInterface |
||
108 | */ |
||
109 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
166 | |||
167 | /** |
||
168 | * Transform the image. |
||
169 | * |
||
170 | * @param ServerRequestInterface $request |
||
171 | * @param ResponseInterface $response |
||
172 | * @param string $transform |
||
173 | * |
||
174 | * @return ResponseInterface |
||
175 | */ |
||
176 | private function transform(ServerRequestInterface $request, ResponseInterface $response, $transform) |
||
195 | |||
196 | /** |
||
197 | * Parses the path and return the file and transform values. |
||
198 | * For example, the path "/images/small.avatar.jpg" returns: |
||
199 | * ["/images/avatar.jpg", "resizeCrop,50,50"]. |
||
200 | * |
||
201 | * @param string $path |
||
202 | * |
||
203 | * @return false|array [file, transform] |
||
204 | */ |
||
205 | private function parsePath($path) |
||
227 | |||
228 | /** |
||
229 | * Returns the client hints sent. |
||
230 | * |
||
231 | * @param ServerRequestInterface $request |
||
232 | * |
||
233 | * @return array|null |
||
234 | */ |
||
235 | private function getClientHints(ServerRequestInterface $request) |
||
249 | |||
250 | /** |
||
251 | * Generates the key used to save the image in cache. |
||
252 | * |
||
253 | * @param ServerRequestInterface $request |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | private function getCacheKey(ServerRequestInterface $request) |
||
268 | } |
||
269 |