1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys PackBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\PackBundle
;
14 use Rapsys\PackBundle\Util\ImageUtil
;
15 use Rapsys\PackBundle\Util\SluggerUtil
;
17 use Psr\Container\ContainerInterface
;
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
;
33 class Controller
extends AbstractController
implements ServiceSubscriberInterface
{
37 protected string $alias;
42 protected array $config;
52 protected string $version;
55 * Creates a new image controller
57 * @param ContainerInterface $container The ContainerInterface instance
58 * @param ImageUtil $image The MapUtil instance
59 * @param SluggerUtil $slugger The SluggerUtil instance
61 function __construct(protected ContainerInterface
$container, protected ImageUtil
$image, protected SluggerUtil
$slugger) {
63 $this->config
= $container->getParameter($this->alias
= RapsysPackBundle
::getAlias());
66 $this->ctx
= stream_context_create($this->config
['context']);
70 * Return captcha image
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
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])) {
83 throw new NotFoundHttpException(sprintf('Unable to match captcha hash: %s', $hash));
84 //Without valid format
85 } elseif ($_format !== 'jpeg' && $_format !== 'png' && $_format !== 'webp') {
87 throw new NotFoundHttpException('Invalid thumb format');
91 $equation = $this->slugger
->unshort($short = $equation);
94 $hashed = str_split(strval($equation));
97 $captcha = $this->config
['cache'].'/'.$this->config
['prefixes']['captcha'].'/'.$hashed[0].'/'.$hashed[4].'/'.$hashed[8].'/'.$short.'.'.$_format;
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();
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) {
113 throw new \
Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
117 //Create image instance
118 $image = new \
Imagick();
120 //Add imagick draw instance
121 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
122 $draw = new \
ImagickDraw();
125 $draw->setTextAntialias(true);
127 //Set stroke antialias
128 $draw->setStrokeAntialias(true);
131 $draw->setTextAlignment(\Imagick
::ALIGN_CENTER
);
134 $draw->setGravity(\Imagick
::GRAVITY_CENTER
);
137 $draw->setFillColor($this->config
['captcha']['fill']);
140 $draw->setStrokeColor($this->config
['captcha']['border']);
143 $draw->setFontSize($this->config
['captcha']['size'] / 1.5);
146 $draw->setStrokeWidth($this->config
['captcha']['thickness'] / 3);
149 $draw->rotate($rotate = (rand(25, 75)*(rand(0,1)?-.1:.1)));
152 $metrics2 = $image->queryFontMetrics($draw, strval('stop spam'));
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'));
158 $draw->rotate(-$rotate);
161 $draw->setFontSize($this->config
['captcha']['size']);
164 $draw->setStrokeWidth($this->config
['captcha']['thickness']);
167 $draw->rotate($rotate = (rand(25, 50)*(rand(0,1)?-.1:.1)));
170 $metrics = $image->queryFontMetrics($draw, strval($equation));
173 $draw->annotation($width / 2, ceil($metrics['textHeight'] +
$metrics['descender'] +
$metrics['ascender']) / 2 - $this->config
['captcha']['thickness'], strval($equation));
176 $draw->rotate(-$rotate);
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);
183 $image->drawImage($draw);
185 //Strip image exif data and properties
186 $image->stripImage();
188 //Set compression quality
189 $image->setImageCompressionQuality(70);
192 if (!$image->writeImage($captcha)) {
194 throw new \
Exception(sprintf('Unable to write image "%s"', $captcha));
198 $mtime = stat($captcha)['mtime'];
201 //Read captcha from cache
202 $response = new BinaryFileResponse($captcha);
205 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, 'captcha-stop-spam-'.str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation).'.'.$_format);
208 $response->setEtag(md5($hash));
211 $response->setLastModified(\DateTime
::createFromFormat('U', strval($mtime)));
214 $response->setPublic();
216 //Return 304 response if not modified
217 $response->isNotModified($request);
224 * Return download file
226 * @param Request $request The Request instance
227 * @param string $hash The hash
228 * @param string $path The image path
229 * @return Response The rendered image
231 public function download(Request
$request, string $hash, string $path/*, string $_format*/): Response
{
232 //Without matching hash
233 if ($hash !== $this->slugger
->hash($path)) {
234 //Throw new exception
235 throw new NotFoundHttpException('Invalid download hash');
236 //Without valid format
237 #} elseif ($_format !== 'jpeg' && $_format !== 'png' && $_format !== 'webp') {
238 # //Throw new exception
239 # throw new NotFoundHttpException('Invalid download format');
243 $path = $this->slugger
->unshort($short = $path);
246 if (!is_file($path) || !($mtime = stat($path)['mtime'])) {
247 //Throw new exception
248 throw new NotFoundHttpException('Unable to get thumb file');
251 //Read thumb from cache
252 $response = new BinaryFileResponse($path);
255 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, basename($path));
258 //TODO: set etag to file content md5 ? cache it ?
259 $response->setEtag(md5($hash));
262 $response->setLastModified(\DateTime
::createFromFormat('U', strval($mtime)));
265 $response->setPublic();
267 //Return 304 response if not modified
268 $response->isNotModified($request);
275 * Return facebook image
277 * @param Request $request The Request instance
278 * @param string $hash The hash
279 * @param string $path The image path
280 * @param int $height The height
281 * @param int $width The width
282 * @return Response The rendered image
284 public function facebook(Request
$request, string $hash, string $path, int $height, int $width, string $_format): Response
{
285 //Without matching hash
286 if ($hash !== $this->slugger
->serialize([$path, $height, $width])) {
287 //Throw new exception
288 throw new NotFoundHttpException(sprintf('Unable to match facebook hash: %s', $hash));
289 //Without matching format
290 } elseif ($_format !== 'jpeg' && $_format !== 'png' && $_format !== 'webp') {
291 //Throw new exception
292 throw new NotFoundHttpException(sprintf('Invalid facebook format: %s', $_format));
296 $path = $this->slugger
->unshort($short = $path);
298 //Without facebook file
299 if (!is_file($facebook = $this->config
['cache'].'/'.$this->config
['prefixes']['facebook'].$path.'.'.$_format)) {
300 //Throw new exception
301 throw new NotFoundHttpException('Unable to get facebook file');
304 //Read facebook from cache
305 $response = new BinaryFileResponse($facebook);
308 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, 'facebook-'.$hash.'.'.$_format);
311 //TODO: set etag to file content md5 ? cache it ?
312 $response->setEtag(md5($hash));
315 $response->setLastModified(\DateTime
::createFromFormat('U', strval(stat($facebook)['mtime'])));
318 $response->setPublic();
320 //Return 304 response if not modified
321 $response->isNotModified($request);
330 * @param Request $request The Request instance
331 * @param string $hash The hash
332 * @param int $updated The updated timestamp
333 * @param float $latitude The latitude
334 * @param float $longitude The longitude
335 * @param int $zoom The zoom
336 * @param int $width The width
337 * @param int $height The height
338 * @return Response The rendered image
340 public function map(Request
$request, string $hash, float $latitude, float $longitude, int $height, int $width, int $zoom, string $_format): Response
{
341 //Without matching hash
342 if ($hash !== $this->slugger
->hash([$height, $width, $zoom, $latitude, $longitude])) {
343 //Throw new exception
344 throw new NotFoundHttpException(sprintf('Unable to match map hash: %s', $hash));
348 $map = $this->config
['cache'].'/'.$this->config
['prefixes']['map'].'/'.$zoom.'/'.($latitude*1000000%
10).'/'.($longitude*1000000%
10).'/'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.'.$_format;
351 //TODO: refresh after config modification ?
352 if (!is_file($map)) {
353 //Without existing map path
354 if (!is_dir($dir = dirname($map))) {
355 //Create filesystem object
356 $filesystem = new Filesystem();
360 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
361 //XXX: on CoW filesystems execute a chattr +C before filling
362 $filesystem->mkdir($dir, 0775);
363 } catch (IOExceptionInterface
$e) {
365 throw new \
Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
369 //Create image instance
370 $image = new \
Imagick();
373 $image->newImage($width, $height, new \
ImagickPixel('transparent'), $_format);
375 //Create tile instance
376 $tile = new \
Imagick();
379 $centerX = $this->image
->longitudeToX($longitude, $zoom);
380 $centerY = $this->image
->latitudeToY($latitude, $zoom);
383 $startX = floor(floor($centerX) - $width / $this->config
['map']['tz']);
384 $startY = floor(floor($centerY) - $height / $this->config
['map']['tz']);
387 $endX = ceil(ceil($centerX) +
$width / $this->config
['map']['tz']);
388 $endY = ceil(ceil($centerY) +
$height / $this->config
['map']['tz']);
390 for($x = $startX; $x <= $endX; $x++
) {
391 for($y = $startY; $y <= $endY; $y++
) {
393 $cache = $this->config
['cache'].'/'.$this->config
['prefixes']['map'].'/'.$zoom.'/'.($x%
10).'/'.($y%
10).'/'.$x.','.$y.'.png';
395 //Without cache image
396 if (!is_file($cache)) {
398 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->config
['servers'][$this->config
['map']['server']]);
401 if (!is_dir($dir = dirname($cache))) {
402 //Create filesystem object
403 $filesystem = new Filesystem();
407 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
408 $filesystem->mkdir($dir, 0775);
409 } catch (IOExceptionInterface
$e) {
411 throw new \
Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
415 //Store tile in cache
416 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx
));
420 $destX = intval(floor($width / 2 - $this->config
['map']['tz'] * ($centerX - $x)));
423 $destY = intval(floor($height / 2 - $this->config
['map']['tz'] * ($centerY - $y)));
425 //Read tile from cache
426 $tile->readImage($cache);
429 $image->compositeImage($tile, \Imagick
::COMPOSITE_OVER
, $destX, $destY);
436 //Add imagick draw instance
437 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
438 $draw = new \
ImagickDraw();
441 $draw->setTextAntialias(true);
443 //Set stroke antialias
444 $draw->setStrokeAntialias(true);
447 $draw->setTextAlignment(\Imagick
::ALIGN_CENTER
);
450 $draw->setGravity(\Imagick
::GRAVITY_CENTER
);
453 $draw->setFillColor($this->config
['map']['fill']);
456 $draw->setStrokeColor($this->config
['map']['border']);
459 $draw->setStrokeWidth($this->config
['map']['thickness']);
462 $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']);
465 $image->drawImage($draw);
467 //Strip image exif data and properties
468 $image->stripImage();
471 //XXX: not supported by imagick :'(
472 $image->setImageProperty('exif:GPSLatitude', $this->image
->latitudeToSexagesimal($latitude));
475 //XXX: not supported by imagick :'(
476 $image->setImageProperty('exif:GPSLongitude', $this->image
->longitudeToSexagesimal($longitude));
478 //Set progressive jpeg
479 $image->setInterlaceScheme(\Imagick
::INTERLACE_PLANE
);
481 //Set compression quality
482 $image->setImageCompressionQuality($this->config
['map']['quality']);
485 if (!$image->writeImage($map)) {
487 throw new \
Exception(sprintf('Unable to write image "%s"', $map));
491 //Read map from cache
492 $response = new BinaryFileResponse($map);
495 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, basename($map));
498 //TODO: set etag to file content md5 ? cache it ?
499 $response->setEtag(md5(serialize([$height, $width, $zoom, $latitude, $longitude])));
502 $response->setLastModified(\DateTime
::createFromFormat('U', strval(stat($map)['mtime'])));
504 //Disable robot index
505 $response->headers
->set('X-Robots-Tag', 'noindex');
508 $response->setPublic();
510 //Return 304 response if not modified
511 $response->isNotModified($request);
518 * Return multi map image
520 * @param Request $request The Request instance
521 * @param string $hash The hash
522 * @param int $updated The updated timestamp
523 * @param float $latitude The latitude
524 * @param float $longitude The longitude
525 * @param string $coordinates The coordinates
526 * @param int $zoom The zoom
527 * @param int $width The width
528 * @param int $height The height
529 * @return Response The rendered image
531 public function multi(Request
$request, string $hash, string $coordinate, int $height, int $width, int $zoom, string $_format): Response
{
532 //Without matching hash
533 if ($hash !== $this->slugger
->hash([$height, $width, $zoom, $coordinate])) {
534 //Throw new exception
535 throw new NotFoundHttpException(sprintf('Unable to match multi map hash: %s', $hash));
538 //Set latitudes and longitudes array
539 $latitudes = $longitudes = [];
542 $coordinates = array_map(
543 function ($v) use (&$latitudes, &$longitudes) {
544 list($latitude, $longitude) = explode(',', $v);
545 $latitudes[] = $latitude;
546 $longitudes[] = $longitude;
547 return [ $latitude, $longitude ];
549 explode('-', $coordinate)
553 $latitude = round((min($latitudes)+
max($latitudes))/2, 6);
556 $longitude = round((min($longitudes)+
max($longitudes))/2, 6);
559 $map = $this->config
['cache'].'/'.$this->config
['prefixes']['multi'].'/'.$zoom.'/'.($latitude*1000000%
10).'/'.($longitude*1000000%
10).'/'.$coordinate.'-'.$zoom.'-'.$width.'x'.$height.'.'.$_format;
562 if (!is_file($map)) {
563 //Without existing multi path
564 if (!is_dir($dir = dirname($map))) {
565 //Create filesystem object
566 $filesystem = new Filesystem();
570 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
571 //XXX: on CoW filesystems execute a chattr +C before filling
572 $filesystem->mkdir($dir, 0775);
573 } catch (IOExceptionInterface
$e) {
575 throw new \
Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
579 //Create image instance
580 $image = new \
Imagick();
583 $image->newImage($width, $height, new \
ImagickPixel('transparent'), $_format);
585 //Create tile instance
586 $tile = new \
Imagick();
589 $centerX = $this->image
->longitudeToX($longitude, $zoom);
590 $centerY = $this->image
->latitudeToY($latitude, $zoom);
593 $startX = floor(floor($centerX) - $width / $this->config
['multi']['tz']);
594 $startY = floor(floor($centerY) - $height / $this->config
['multi']['tz']);
597 $endX = ceil(ceil($centerX) +
$width / $this->config
['multi']['tz']);
598 $endY = ceil(ceil($centerY) +
$height / $this->config
['multi']['tz']);
600 for($x = $startX; $x <= $endX; $x++
) {
601 for($y = $startY; $y <= $endY; $y++
) {
603 $cache = $this->config
['cache'].'/'.$this->config
['prefixes']['multi'].'/'.$zoom.'/'.($x%
10).'/'.($y%
10).'/'.$x.','.$y.'.png';
605 //Without cache image
606 if (!is_file($cache)) {
608 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->config
['servers'][$this->config
['multi']['server']]);
611 if (!is_dir($dir = dirname($cache))) {
612 //Create filesystem object
613 $filesystem = new Filesystem();
617 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
618 $filesystem->mkdir($dir, 0775);
619 } catch (IOExceptionInterface
$e) {
621 throw new \
Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
625 //Store tile in cache
626 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx
));
630 $destX = intval(floor($width / 2 - $this->config
['multi']['tz'] * ($centerX - $x)));
633 $destY = intval(floor($height / 2 - $this->config
['multi']['tz'] * ($centerY - $y)));
635 //Read tile from cache
636 $tile->readImage($cache);
639 $image->compositeImage($tile, \Imagick
::COMPOSITE_OVER
, $destX, $destY);
646 //Add imagick draw instance
647 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
648 $draw = new \
ImagickDraw();
651 $draw->setTextAntialias(true);
653 //Set stroke antialias
654 $draw->setStrokeAntialias(true);
657 $draw->setTextAlignment(\Imagick
::ALIGN_CENTER
);
660 $draw->setGravity(\Imagick
::GRAVITY_CENTER
);
662 //Iterate on locations
663 foreach($coordinates as $id => $coordinate) {
665 list($clatitude, $clongitude) = $coordinate;
668 $destX = intval(floor($width / 2 - $this->config
['multi']['tz'] * ($centerX - $this->image
->longitudeToX(floatval($clongitude), $zoom))));
671 $destY = intval(floor($height / 2 - $this->config
['multi']['tz'] * ($centerY - $this->image
->latitudeToY(floatval($clatitude), $zoom))));
674 $draw->setFillColor($this->config
['multi']['fill']);
677 $draw->setFontSize($this->config
['multi']['size']);
680 $draw->setStrokeColor($this->config
['multi']['border']);
683 $radius = $this->config
['multi']['radius'];
686 $stroke = $this->config
['multi']['thickness'];
688 //With matching position
689 if ($clatitude === $latitude && $clongitude == $longitude) {
691 $draw->setFillColor($this->config
['multi']['highfill']);
694 $draw->setFontSize($this->config
['multi']['highsize']);
697 $draw->setStrokeColor($this->config
['multi']['highborder']);
700 $radius = $this->config
['multi']['highradius'];
703 $stroke = $this->config
['multi']['highthickness'];
707 $draw->setStrokeWidth($stroke);
710 $draw->circle($destX - $radius, $destY - $radius, $destX +
$radius, $destY +
$radius);
713 $draw->setFillColor($draw->getStrokeColor());
716 $draw->setStrokeWidth($stroke / 4);
719 #$metrics = $image->queryFontMetrics($draw, strval($id));
722 $draw->annotation($destX - $radius, $destY +
$stroke, strval($id));
726 $image->drawImage($draw);
728 //Strip image exif data and properties
729 $image->stripImage();
732 //XXX: not supported by imagick :'(
733 $image->setImageProperty('exif:GPSLatitude', $this->image
->latitudeToSexagesimal($latitude));
736 //XXX: not supported by imagick :'(
737 $image->setImageProperty('exif:GPSLongitude', $this->image
->longitudeToSexagesimal($longitude));
740 //XXX: not supported by imagick :'(
741 #$image->setImageProperty('exif:Description', $caption);
743 //Set progressive jpeg
744 $image->setInterlaceScheme(\Imagick
::INTERLACE_PLANE
);
746 //Set compression quality
747 $image->setImageCompressionQuality($this->config
['multi']['quality']);
750 if (!$image->writeImage($map)) {
752 throw new \
Exception(sprintf('Unable to write image "%s"', $path));
756 //Read map from cache
757 $response = new BinaryFileResponse($map);
760 #$response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'multimap-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
761 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, basename($map));
764 //TODO: set etag to file content md5 ? cache it ?
765 $response->setEtag(md5(serialize([$height, $width, $zoom, $coordinate])));
768 $response->setLastModified(\DateTime
::createFromFormat('U', strval(stat($map)['mtime'])));
770 //Disable robot index
771 $response->headers
->set('X-Robots-Tag', 'noindex');
774 $response->setPublic();
776 //Return 304 response if not modified
777 $response->isNotModified($request);
786 * @param Request $request The Request instance
787 * @param string $hash The hash
788 * @param string $path The image path
789 * @param int $height The height
790 * @param int $width The width
791 * @return Response The rendered image
793 public function thumb(Request
$request, string $hash, string $path, int $height, int $width, string $_format): Response
{
794 //Without matching hash
795 if ($hash !== $this->slugger
->serialize([$path, $height, $width])) {
796 //Throw new exception
797 throw new NotFoundHttpException('Invalid thumb hash');
798 //Without valid format
799 } elseif ($_format !== 'jpeg' && $_format !== 'png' && $_format !== 'webp') {
800 //Throw new exception
801 throw new NotFoundHttpException('Invalid thumb format');
805 $path = $this->slugger
->unshort($short = $path);
808 $thumb = $this->config
['cache'].'/'.$this->config
['prefixes']['thumb'].$path.'.'.$width.'x'.$height.'.'.$_format;
811 if (!is_file($path) || !($mtime = stat($path)['mtime'])) {
812 //Throw new exception
813 throw new NotFoundHttpException('Unable to get thumb file');
816 //Without thumb up to date file
817 if (!is_file($thumb) || !($updated = stat($thumb)['mtime']) || $updated < $mtime) {
818 //Without existing thumb path
819 if (!is_dir($dir = dirname($thumb))) {
820 //Create filesystem object
821 $filesystem = new Filesystem();
825 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
826 //XXX: on CoW filesystems execute a chattr +C before filling
827 $filesystem->mkdir($dir, 0775);
828 } catch (IOExceptionInterface
$e) {
830 throw new \
Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
834 //Create image instance
835 $image = new \
Imagick();
838 $image->readImage(realpath($path));
840 //Crop using aspect ratio
841 //XXX: for better result upload image directly in aspect ratio :)
842 $image->cropThumbnailImage($width, $height);
844 //Strip image exif data and properties
845 $image->stripImage();
847 //Set compression quality
849 $image->setImageCompressionQuality(70);
852 #$image->setImageFormat($_format);
855 if (!$image->writeImage($thumb)) {
857 throw new \
Exception(sprintf('Unable to write image "%s"', $thumb));
861 $updated = stat($thumb)['mtime'];
864 //Read thumb from cache
865 $response = new BinaryFileResponse($thumb);
868 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, 'thumb-'.$hash.'.'.$_format);
871 //TODO: set etag to file content md5 ? cache it ?
872 $response->setEtag(md5($hash));
875 $response->setLastModified(\DateTime
::createFromFormat('U', strval($updated)));
878 $response->setPublic();
880 //Return 304 response if not modified
881 $response->isNotModified($request);