]> Raphaël G. Git Repositories - packbundle/blob - Controller.php
Import contact form
[packbundle] / Controller.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
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 Rapsys\PackBundle;
13
14 use Rapsys\PackBundle\Util\ImageUtil;
15 use Rapsys\PackBundle\Util\SluggerUtil;
16
17 use Psr\Container\ContainerInterface;
18
19 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
20 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
21 use Symfony\Component\Filesystem\Filesystem;
22 use Symfony\Component\HttpFoundation\BinaryFileResponse;
23 use Symfony\Component\HttpFoundation\HeaderUtils;
24 use Symfony\Component\HttpFoundation\Request;
25 use Symfony\Component\HttpFoundation\Response;
26 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
27 use Symfony\Component\Routing\RequestContext;
28 use Symfony\Contracts\Service\ServiceSubscriberInterface;
29
30 /**
31 * {@inheritdoc}
32 */
33 class Controller extends AbstractController implements ServiceSubscriberInterface {
34 /**
35 * Alias string
36 */
37 protected string $alias;
38
39 /**
40 * Config array
41 */
42 protected array $config;
43
44 /**
45 * Stream context
46 */
47 protected mixed $ctx;
48
49 /**
50 * Version string
51 */
52 protected string $version;
53
54 /**
55 * Creates a new image controller
56 *
57 * @param ContainerInterface $container The ContainerInterface instance
58 * @param ImageUtil $image The MapUtil instance
59 * @param SluggerUtil $slugger The SluggerUtil instance
60 */
61 function __construct(protected ContainerInterface $container, protected ImageUtil $image, protected SluggerUtil $slugger) {
62 //Retrieve config
63 $this->config = $container->getParameter($this->alias = RapsysPackBundle::getAlias());
64
65 //Set ctx
66 $this->ctx = stream_context_create($this->config['context']);
67 }
68
69 /**
70 * Return captcha image
71 *
72 * @param Request $request The Request instance
73 * @param string $hash The hash
74 * @param string $equation The shorted equation
75 * @param int $height The height
76 * @param int $width The width
77 * @return Response The rendered image
78 */
79 public function captcha(Request $request, string $hash, string $equation, int $height, int $width, string $_format): Response {
80 //Without matching hash
81 if ($hash !== $this->slugger->serialize([$equation, $height, $width])) {
82 //Throw new exception
83 throw new NotFoundHttpException(sprintf('Unable to match captcha hash: %s', $hash));
84 //Without valid format
85 } elseif ($_format !== 'jpeg' && $_format !== 'png' && $_format !== 'webp') {
86 //Throw new exception
87 throw new NotFoundHttpException('Invalid thumb format');
88 }
89
90 //Unshort equation
91 $equation = $this->slugger->unshort($short = $equation);
92
93 //Set hashed tree
94 $hashed = str_split(strval($equation));
95
96 //Set captcha
97 $captcha = $this->config['cache'].'/'.$this->config['prefixes']['captcha'].'/'.$hashed[0].'/'.$hashed[4].'/'.$hashed[8].'/'.$short.'.'.$_format;
98
99 //Without captcha up to date file
100 if (!is_file($captcha) || !($mtime = stat($captcha)['mtime']) || $mtime < (new \DateTime('-1 hour'))->getTimestamp()) {
101 //Without existing captcha path
102 if (!is_dir($dir = dirname($captcha))) {
103 //Create filesystem object
104 $filesystem = new Filesystem();
105
106 try {
107 //Create path
108 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
109 //XXX: on CoW filesystems execute a chattr +C before filling
110 $filesystem->mkdir($dir, 0775);
111 } catch (IOExceptionInterface $e) {
112 //Throw error
113 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
114 }
115 }
116
117 //Create image instance
118 $image = new \Imagick();
119
120 //Add imagick draw instance
121 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
122 $draw = new \ImagickDraw();
123
124 //Set text antialias
125 $draw->setTextAntialias(true);
126
127 //Set stroke antialias
128 $draw->setStrokeAntialias(true);
129
130 //Set text alignment
131 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
132
133 //Set gravity
134 $draw->setGravity(\Imagick::GRAVITY_CENTER);
135
136 //Set fill color
137 $draw->setFillColor($this->config['captcha']['fill']);
138
139 //Set stroke color
140 $draw->setStrokeColor($this->config['captcha']['border']);
141
142 //Set font size
143 $draw->setFontSize($this->config['captcha']['size'] / 1.5);
144
145 //Set stroke width
146 $draw->setStrokeWidth($this->config['captcha']['thickness'] / 3);
147
148 //Set rotation
149 $draw->rotate($rotate = (rand(25, 75)*(rand(0,1)?-.1:.1)));
150
151 //Get font metrics
152 $metrics2 = $image->queryFontMetrics($draw, strval('stop spam'));
153
154 //Add annotation
155 $draw->annotation($width / 2 - ceil(rand(intval(-$metrics2['textWidth']), intval($metrics2['textWidth'])) / 2) - abs($rotate), ceil($metrics2['textHeight'] + $metrics2['descender'] + $metrics2['ascender']) / 2 - $this->config['captcha']['thickness'] - $rotate, strval('stop spam'));
156
157 //Set rotation
158 $draw->rotate(-$rotate);
159
160 //Set font size
161 $draw->setFontSize($this->config['captcha']['size']);
162
163 //Set stroke width
164 $draw->setStrokeWidth($this->config['captcha']['thickness']);
165
166 //Set rotation
167 $draw->rotate($rotate = (rand(25, 50)*(rand(0,1)?-.1:.1)));
168
169 //Get font metrics
170 $metrics = $image->queryFontMetrics($draw, strval($equation));
171
172 //Add annotation
173 $draw->annotation($width / 2, ceil($metrics['textHeight'] + $metrics['descender'] + $metrics['ascender']) / 2 - $this->config['captcha']['thickness'], strval($equation));
174
175 //Set rotation
176 $draw->rotate(-$rotate);
177
178 //Add new image
179 #$image->newImage(intval(ceil($metrics['textWidth'])), intval(ceil($metrics['textHeight'] + $metrics['descender'])), new \ImagickPixel($this->config['captcha']['background']), 'jpeg');
180 $image->newImage($width, $height, new \ImagickPixel($this->config['captcha']['background']), $_format);
181
182 //Draw on image
183 $image->drawImage($draw);
184
185 //Strip image exif data and properties
186 $image->stripImage();
187
188 //Set compression quality
189 $image->setImageCompressionQuality(70);
190
191 //Save captcha
192 if (!$image->writeImage($captcha)) {
193 //Throw error
194 throw new \Exception(sprintf('Unable to write image "%s"', $captcha));
195 }
196
197 //Set mtime
198 $mtime = stat($captcha)['mtime'];
199 }
200
201 //Read captcha from cache
202 $response = new BinaryFileResponse($captcha);
203
204 //Set file name
205 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'captcha-stop-spam-'.str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation).'.'.$_format);
206
207 //Set etag
208 $response->setEtag(md5($hash));
209
210 //Set last modified
211 $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
212
213 //Set as public
214 $response->setPublic();
215
216 //Return 304 response if not modified
217 $response->isNotModified($request);
218
219 //Return response
220 return $response;
221 }
222
223 /**
224 * Return facebook image
225 *
226 * @param Request $request The Request instance
227 * @param string $hash The hash
228 * @param string $path The image path
229 * @param int $height The height
230 * @param int $width The width
231 * @return Response The rendered image
232 */
233 public function facebook(Request $request, string $hash, string $path, int $height, int $width, string $_format): Response {
234 //Without matching hash
235 if ($hash !== $this->slugger->serialize([$path, $height, $width])) {
236 //Throw new exception
237 throw new NotFoundHttpException(sprintf('Unable to match facebook hash: %s', $hash));
238 //Without matching format
239 } elseif ($_format !== 'jpeg' && $_format !== 'png' && $_format !== 'webp') {
240 //Throw new exception
241 throw new NotFoundHttpException(sprintf('Invalid facebook format: %s', $_format));
242 }
243
244 //Unshort path
245 $path = $this->slugger->unshort($short = $path);
246
247 //Without facebook file
248 if (!is_file($facebook = $this->config['cache'].'/'.$this->config['prefixes']['facebook'].$path.'.'.$_format)) {
249 //Throw new exception
250 throw new NotFoundHttpException('Unable to get facebook file');
251 }
252
253 //Read facebook from cache
254 $response = new BinaryFileResponse($facebook);
255
256 //Set file name
257 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'facebook-'.$hash.'.'.$_format);
258
259 //Set etag
260 $response->setEtag(md5($hash));
261
262 //Set last modified
263 $response->setLastModified(\DateTime::createFromFormat('U', strval(stat($facebook)['mtime'])));
264
265 //Set as public
266 $response->setPublic();
267
268 //Return 304 response if not modified
269 $response->isNotModified($request);
270
271 //Return response
272 return $response;
273 }
274
275 /**
276 * Return map image
277 *
278 * @param Request $request The Request instance
279 * @param string $hash The hash
280 * @param int $updated The updated timestamp
281 * @param float $latitude The latitude
282 * @param float $longitude The longitude
283 * @param int $zoom The zoom
284 * @param int $width The width
285 * @param int $height The height
286 * @return Response The rendered image
287 */
288 public function map(Request $request, string $hash, float $latitude, float $longitude, int $height, int $width, int $zoom, string $_format): Response {
289 //Without matching hash
290 if ($hash !== $this->slugger->hash([$height, $width, $zoom, $latitude, $longitude])) {
291 //Throw new exception
292 throw new NotFoundHttpException(sprintf('Unable to match map hash: %s', $hash));
293 }
294
295 //Set map
296 $map = $this->config['cache'].'/'.$this->config['prefixes']['map'].'/'.$zoom.'/'.($latitude*1000000%10).'/'.($longitude*1000000%10).'/'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.'.$_format;
297
298 //Without map file
299 //TODO: refresh after config modification ?
300 if (!is_file($map)) {
301 //Without existing map path
302 if (!is_dir($dir = dirname($map))) {
303 //Create filesystem object
304 $filesystem = new Filesystem();
305
306 try {
307 //Create path
308 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
309 //XXX: on CoW filesystems execute a chattr +C before filling
310 $filesystem->mkdir($dir, 0775);
311 } catch (IOExceptionInterface $e) {
312 //Throw error
313 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
314 }
315 }
316
317 //Create image instance
318 $image = new \Imagick();
319
320 //Add new image
321 $image->newImage($width, $height, new \ImagickPixel('transparent'), $_format);
322
323 //Create tile instance
324 $tile = new \Imagick();
325
326 //Get tile xy
327 $centerX = $this->image->longitudeToX($longitude, $zoom);
328 $centerY = $this->image->latitudeToY($latitude, $zoom);
329
330 //Calculate start xy
331 $startX = floor(floor($centerX) - $width / $this->config['map']['tz']);
332 $startY = floor(floor($centerY) - $height / $this->config['map']['tz']);
333
334 //Calculate end xy
335 $endX = ceil(ceil($centerX) + $width / $this->config['map']['tz']);
336 $endY = ceil(ceil($centerY) + $height / $this->config['map']['tz']);
337
338 for($x = $startX; $x <= $endX; $x++) {
339 for($y = $startY; $y <= $endY; $y++) {
340 //Set cache path
341 $cache = $this->config['cache'].'/'.$this->config['prefixes']['map'].'/'.$zoom.'/'.($x%10).'/'.($y%10).'/'.$x.','.$y.'.png';
342
343 //Without cache image
344 if (!is_file($cache)) {
345 //Set tile url
346 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->config['servers'][$this->config['map']['server']]);
347
348 //Without cache path
349 if (!is_dir($dir = dirname($cache))) {
350 //Create filesystem object
351 $filesystem = new Filesystem();
352
353 try {
354 //Create path
355 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
356 $filesystem->mkdir($dir, 0775);
357 } catch (IOExceptionInterface $e) {
358 //Throw error
359 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
360 }
361 }
362
363 //Store tile in cache
364 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
365 }
366
367 //Set dest x
368 $destX = intval(floor($width / 2 - $this->config['map']['tz'] * ($centerX - $x)));
369
370 //Set dest y
371 $destY = intval(floor($height / 2 - $this->config['map']['tz'] * ($centerY - $y)));
372
373 //Read tile from cache
374 $tile->readImage($cache);
375
376 //Compose image
377 $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
378
379 //Clear tile
380 $tile->clear();
381 }
382 }
383
384 //Add imagick draw instance
385 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
386 $draw = new \ImagickDraw();
387
388 //Set text antialias
389 $draw->setTextAntialias(true);
390
391 //Set stroke antialias
392 $draw->setStrokeAntialias(true);
393
394 //Set text alignment
395 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
396
397 //Set gravity
398 $draw->setGravity(\Imagick::GRAVITY_CENTER);
399
400 //Set fill color
401 $draw->setFillColor($this->config['map']['fill']);
402
403 //Set stroke color
404 $draw->setStrokeColor($this->config['map']['border']);
405
406 //Set stroke width
407 $draw->setStrokeWidth($this->config['map']['thickness']);
408
409 //Draw circle
410 $draw->circle($width/2 - $this->config['map']['radius'], $height/2 - $this->config['map']['radius'], $width/2 + $this->config['map']['radius'], $height/2 + $this->config['map']['radius']);
411
412 //Draw on image
413 $image->drawImage($draw);
414
415 //Strip image exif data and properties
416 $image->stripImage();
417
418 //Add latitude
419 //XXX: not supported by imagick :'(
420 $image->setImageProperty('exif:GPSLatitude', $this->image->latitudeToSexagesimal($latitude));
421
422 //Add longitude
423 //XXX: not supported by imagick :'(
424 $image->setImageProperty('exif:GPSLongitude', $this->image->longitudeToSexagesimal($longitude));
425
426 //Set progressive jpeg
427 $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
428
429 //Set compression quality
430 $image->setImageCompressionQuality($this->config['map']['quality']);
431
432 //Save image
433 if (!$image->writeImage($map)) {
434 //Throw error
435 throw new \Exception(sprintf('Unable to write image "%s"', $map));
436 }
437 }
438
439 //Read map from cache
440 $response = new BinaryFileResponse($map);
441
442 //Set file name
443 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, basename($map));
444
445 //Set etag
446 $response->setEtag(md5(serialize([$height, $width, $zoom, $latitude, $longitude])));
447
448 //Set last modified
449 $response->setLastModified(\DateTime::createFromFormat('U', strval(stat($map)['mtime'])));
450
451 //Disable robot index
452 $response->headers->set('X-Robots-Tag', 'noindex');
453
454 //Set as public
455 $response->setPublic();
456
457 //Return 304 response if not modified
458 $response->isNotModified($request);
459
460 //Return response
461 return $response;
462 }
463
464 /**
465 * Return multi map image
466 *
467 * @param Request $request The Request instance
468 * @param string $hash The hash
469 * @param int $updated The updated timestamp
470 * @param float $latitude The latitude
471 * @param float $longitude The longitude
472 * @param string $coordinates The coordinates
473 * @param int $zoom The zoom
474 * @param int $width The width
475 * @param int $height The height
476 * @return Response The rendered image
477 */
478 public function multi(Request $request, string $hash, string $coordinate, int $height, int $width, int $zoom, string $_format): Response {
479 //Without matching hash
480 if ($hash !== $this->slugger->hash([$height, $width, $zoom, $coordinate])) {
481 //Throw new exception
482 throw new NotFoundHttpException(sprintf('Unable to match multi map hash: %s', $hash));
483 }
484
485 //Set latitudes and longitudes array
486 $latitudes = $longitudes = [];
487
488 //Set coordinates
489 $coordinates = array_map(
490 function ($v) use (&$latitudes, &$longitudes) {
491 list($latitude, $longitude) = explode(',', $v);
492 $latitudes[] = $latitude;
493 $longitudes[] = $longitude;
494 return [ $latitude, $longitude ];
495 },
496 explode('-', $coordinate)
497 );
498
499 //Set latitude
500 $latitude = round((min($latitudes)+max($latitudes))/2, 6);
501
502 //Set longitude
503 $longitude = round((min($longitudes)+max($longitudes))/2, 6);
504
505 //Set map
506 $map = $this->config['cache'].'/'.$this->config['prefixes']['multi'].'/'.$zoom.'/'.($latitude*1000000%10).'/'.($longitude*1000000%10).'/'.$coordinate.'-'.$zoom.'-'.$width.'x'.$height.'.'.$_format;
507
508 //Without map file
509 if (!is_file($map)) {
510 //Without existing multi path
511 if (!is_dir($dir = dirname($map))) {
512 //Create filesystem object
513 $filesystem = new Filesystem();
514
515 try {
516 //Create path
517 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
518 //XXX: on CoW filesystems execute a chattr +C before filling
519 $filesystem->mkdir($dir, 0775);
520 } catch (IOExceptionInterface $e) {
521 //Throw error
522 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
523 }
524 }
525
526 //Create image instance
527 $image = new \Imagick();
528
529 //Add new image
530 $image->newImage($width, $height, new \ImagickPixel('transparent'), $_format);
531
532 //Create tile instance
533 $tile = new \Imagick();
534
535 //Get tile xy
536 $centerX = $this->image->longitudeToX($longitude, $zoom);
537 $centerY = $this->image->latitudeToY($latitude, $zoom);
538
539 //Calculate start xy
540 $startX = floor(floor($centerX) - $width / $this->config['multi']['tz']);
541 $startY = floor(floor($centerY) - $height / $this->config['multi']['tz']);
542
543 //Calculate end xy
544 $endX = ceil(ceil($centerX) + $width / $this->config['multi']['tz']);
545 $endY = ceil(ceil($centerY) + $height / $this->config['multi']['tz']);
546
547 for($x = $startX; $x <= $endX; $x++) {
548 for($y = $startY; $y <= $endY; $y++) {
549 //Set cache path
550 $cache = $this->config['cache'].'/'.$this->config['prefixes']['multi'].'/'.$zoom.'/'.($x%10).'/'.($y%10).'/'.$x.','.$y.'.png';
551
552 //Without cache image
553 if (!is_file($cache)) {
554 //Set tile url
555 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->config['servers'][$this->config['multi']['server']]);
556
557 //Without cache path
558 if (!is_dir($dir = dirname($cache))) {
559 //Create filesystem object
560 $filesystem = new Filesystem();
561
562 try {
563 //Create path
564 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
565 $filesystem->mkdir($dir, 0775);
566 } catch (IOExceptionInterface $e) {
567 //Throw error
568 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
569 }
570 }
571
572 //Store tile in cache
573 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
574 }
575
576 //Set dest x
577 $destX = intval(floor($width / 2 - $this->config['multi']['tz'] * ($centerX - $x)));
578
579 //Set dest y
580 $destY = intval(floor($height / 2 - $this->config['multi']['tz'] * ($centerY - $y)));
581
582 //Read tile from cache
583 $tile->readImage($cache);
584
585 //Compose image
586 $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
587
588 //Clear tile
589 $tile->clear();
590 }
591 }
592
593 //Add imagick draw instance
594 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
595 $draw = new \ImagickDraw();
596
597 //Set text antialias
598 $draw->setTextAntialias(true);
599
600 //Set stroke antialias
601 $draw->setStrokeAntialias(true);
602
603 //Set text alignment
604 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
605
606 //Set gravity
607 $draw->setGravity(\Imagick::GRAVITY_CENTER);
608
609 //Iterate on locations
610 foreach($coordinates as $id => $coordinate) {
611 //Get coordinates
612 list($clatitude, $clongitude) = $coordinate;
613
614 //Set dest x
615 $destX = intval(floor($width / 2 - $this->config['multi']['tz'] * ($centerX - $this->image->longitudeToX(floatval($clongitude), $zoom))));
616
617 //Set dest y
618 $destY = intval(floor($height / 2 - $this->config['multi']['tz'] * ($centerY - $this->image->latitudeToY(floatval($clatitude), $zoom))));
619
620 //Set fill color
621 $draw->setFillColor($this->config['multi']['fill']);
622
623 //Set font size
624 $draw->setFontSize($this->config['multi']['size']);
625
626 //Set stroke color
627 $draw->setStrokeColor($this->config['multi']['border']);
628
629 //Set circle radius
630 $radius = $this->config['multi']['radius'];
631
632 //Set stroke width
633 $stroke = $this->config['multi']['thickness'];
634
635 //With matching position
636 if ($clatitude === $latitude && $clongitude == $longitude) {
637 //Set fill color
638 $draw->setFillColor($this->config['multi']['highfill']);
639
640 //Set font size
641 $draw->setFontSize($this->config['multi']['highsize']);
642
643 //Set stroke color
644 $draw->setStrokeColor($this->config['multi']['highborder']);
645
646 //Set circle radius
647 $radius = $this->config['multi']['highradius'];
648
649 //Set stroke width
650 $stroke = $this->config['multi']['highthickness'];
651 }
652
653 //Set stroke width
654 $draw->setStrokeWidth($stroke);
655
656 //Draw circle
657 $draw->circle($destX - $radius, $destY - $radius, $destX + $radius, $destY + $radius);
658
659 //Set fill color
660 $draw->setFillColor($draw->getStrokeColor());
661
662 //Set stroke width
663 $draw->setStrokeWidth($stroke / 4);
664
665 //Get font metrics
666 #$metrics = $image->queryFontMetrics($draw, strval($id));
667
668 //Add annotation
669 $draw->annotation($destX - $radius, $destY + $stroke, strval($id));
670 }
671
672 //Draw on image
673 $image->drawImage($draw);
674
675 //Strip image exif data and properties
676 $image->stripImage();
677
678 //Add latitude
679 //XXX: not supported by imagick :'(
680 $image->setImageProperty('exif:GPSLatitude', $this->image->latitudeToSexagesimal($latitude));
681
682 //Add longitude
683 //XXX: not supported by imagick :'(
684 $image->setImageProperty('exif:GPSLongitude', $this->image->longitudeToSexagesimal($longitude));
685
686 //Add description
687 //XXX: not supported by imagick :'(
688 #$image->setImageProperty('exif:Description', $caption);
689
690 //Set progressive jpeg
691 $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
692
693 //Set compression quality
694 $image->setImageCompressionQuality($this->config['multi']['quality']);
695
696 //Save image
697 if (!$image->writeImage($map)) {
698 //Throw error
699 throw new \Exception(sprintf('Unable to write image "%s"', $path));
700 }
701 }
702
703 //Read map from cache
704 $response = new BinaryFileResponse($map);
705
706 //Set file name
707 #$response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'multimap-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
708 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, basename($map));
709
710 //Set etag
711 $response->setEtag(md5(serialize([$height, $width, $zoom, $coordinate])));
712
713 //Set last modified
714 $response->setLastModified(\DateTime::createFromFormat('U', strval(stat($map)['mtime'])));
715
716 //Disable robot index
717 $response->headers->set('X-Robots-Tag', 'noindex');
718
719 //Set as public
720 $response->setPublic();
721
722 //Return 304 response if not modified
723 $response->isNotModified($request);
724
725 //Return response
726 return $response;
727 }
728
729 /**
730 * Return thumb image
731 *
732 * @param Request $request The Request instance
733 * @param string $hash The hash
734 * @param string $path The image path
735 * @param int $height The height
736 * @param int $width The width
737 * @return Response The rendered image
738 */
739 public function thumb(Request $request, string $hash, string $path, int $height, int $width, string $_format): Response {
740 //Without matching hash
741 if ($hash !== $this->slugger->serialize([$path, $height, $width])) {
742 //Throw new exception
743 throw new NotFoundHttpException('Invalid thumb hash');
744 //Without valid format
745 } elseif ($_format !== 'jpeg' && $_format !== 'png' && $_format !== 'webp') {
746 //Throw new exception
747 throw new NotFoundHttpException('Invalid thumb format');
748 }
749
750 //Unshort path
751 $path = $this->slugger->unshort($short = $path);
752
753 //Set thumb
754 $thumb = $this->config['cache'].'/'.$this->config['prefixes']['thumb'].$path.'.'.$_format;
755
756 //Without file
757 if (!is_file($path) || !($updated = stat($path)['mtime'])) {
758 //Throw new exception
759 throw new NotFoundHttpException('Unable to get thumb file');
760 }
761
762 //Without thumb up to date file
763 if (!is_file($thumb) || !($mtime = stat($thumb)['mtime']) || $mtime < $updated) {
764 //Without existing thumb path
765 if (!is_dir($dir = dirname($thumb))) {
766 //Create filesystem object
767 $filesystem = new Filesystem();
768
769 try {
770 //Create path
771 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
772 //XXX: on CoW filesystems execute a chattr +C before filling
773 $filesystem->mkdir($dir, 0775);
774 } catch (IOExceptionInterface $e) {
775 //Throw error
776 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
777 }
778 }
779
780 //Create image instance
781 $image = new \Imagick();
782
783 //Read image
784 $image->readImage(realpath($path));
785
786 //Crop using aspect ratio
787 //XXX: for better result upload image directly in aspect ratio :)
788 $image->cropThumbnailImage($width, $height);
789
790 //Strip image exif data and properties
791 $image->stripImage();
792
793 //Set compression quality
794 //TODO: ajust that
795 $image->setImageCompressionQuality(70);
796
797 //Set image format
798 #$image->setImageFormat($_format);
799
800 //Save thumb
801 if (!$image->writeImage($thumb)) {
802 //Throw error
803 throw new \Exception(sprintf('Unable to write image "%s"', $thumb));
804 }
805
806 //Set mtime
807 $mtime = stat($thumb)['mtime'];
808 }
809
810 //Read thumb from cache
811 $response = new BinaryFileResponse($thumb);
812
813 //Set file name
814 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'thumb-'.$hash.'.'.$_format);
815
816 //Set etag
817 $response->setEtag(md5($hash));
818
819 //Set last modified
820 $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
821
822 //Set as public
823 $response->setPublic();
824
825 //Return 304 response if not modified
826 $response->isNotModified($request);
827
828 //Return response
829 return $response;
830 }
831 }