]> Raphaël G. Git Repositories - packbundle/blobdiff - Controller/MapController.php
Remove container member defined in AbstractController
[packbundle] / Controller / MapController.php
index 7bd6fe74ba8217def8d3ad0372876ec2dbc2885e..5de1d8251a56a5a94a8fa8fc413961cb80074c04 100644 (file)
@@ -11,6 +11,7 @@
 
 namespace Rapsys\PackBundle\Controller;
 
+use Symfony\Component\HttpFoundation\HeaderUtils;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
@@ -34,13 +35,6 @@ class MapController extends AbstractController implements ServiceSubscriberInter
         */
        protected string $cache;
 
-       /**
-        * The ContainerInterface instance
-        *
-        * @var ContainerInterface 
-        */
-       protected $container;
-
        /**
         * The stream context instance
         */
@@ -54,7 +48,7 @@ class MapController extends AbstractController implements ServiceSubscriberInter
        /**
         * The public path
         */
-       protected string $public;
+       protected string $path;
 
        /**
         * The SluggerUtil instance
@@ -67,18 +61,19 @@ class MapController extends AbstractController implements ServiceSubscriberInter
        protected string $url;
 
        /**
-        * Creates a new osm util
+        * Creates a new osm controller
         *
         * @param ContainerInterface $container The ContainerInterface instance
         * @param MapUtil $map The MapUtil instance
         * @param SluggerUtil $slugger The SluggerUtil instance
         * @param string $cache The cache path
-        * @param string $public The public path
+        * @param string $path The public path
+#       * @param string $prefix The prefix
         * @param string $url The tile server url
         */
-       function __construct(ContainerInterface $container, MapUtil $map, SluggerUtil $slugger, string $cache = '../var/cache/map', string $public = './bundles/rapsyspack/map', string $url = MapUtil::osm) {
+       function __construct(ContainerInterface $container, MapUtil $map, SluggerUtil $slugger, string $cache = '../var/cache', string $path = './bundles/rapsyspack', string $prefix = 'map', string $url = MapUtil::osm) {
                //Set cache
-               $this->cache = $cache;
+               $this->cache = $cache.'/'.$prefix;
 
                //Set container
                $this->container = $container;
@@ -99,8 +94,8 @@ class MapController extends AbstractController implements ServiceSubscriberInter
                //Set map
                $this->map = $map;
 
-               //Set public
-               $this->public = $public;
+               //Set path
+               $this->path = $path.'/'.$prefix;
 
                //Set slugger
                $this->slugger = $slugger;
@@ -124,167 +119,185 @@ class MapController extends AbstractController implements ServiceSubscriberInter
         */
        public function map(Request $request, string $hash, int $updated, float $latitude, float $longitude, int $zoom, int $width, int $height): Response {
                //Without matching hash
-               if ($hash !== $this->slugger->serialize([$updated, $latitude, $longitude, $zoom, $width, $height])) {
+               if ($hash !== $this->slugger->hash([$updated, $latitude, $longitude, $zoom, $width, $height])) {
                        //Throw new exception
                        throw new NotFoundHttpException(sprintf('Unable to match map hash: %s', $hash));
                }
 
                //Set map
-               $map = $this->public.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$width.'x'.$height.'.jpeg';
-
-               //With map up to date file
-               if (is_file($map) && ($mtime = stat($map)['mtime']) && $mtime >= $updated) {
-                       //Read map from cache
-                       //TODO: handle modified, etag, cache, etc ???
-                       return new BinaryFileResponse($map);
-               }
-
-               //Without existing map
-               if (!is_dir($dir = dirname($map))) {
-                       //Create filesystem object
-                       $filesystem = new Filesystem();
-
-                       try {
-                               //Create path
-                               //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
-                               //XXX: on CoW filesystems execute a chattr +C before filling
-                               $filesystem->mkdir($dir, 0775);
-                       } catch (IOExceptionInterface $e) {
-                               //Throw error
-                               throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
+               $map = $this->path.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$width.'x'.$height.'.jpeg';
+
+               //Without multi up to date file
+               if (!is_file($map) || !($mtime = stat($map)['mtime']) || $mtime < $updated) {
+                       //Without existing map path
+                       if (!is_dir($dir = dirname($map))) {
+                               //Create filesystem object
+                               $filesystem = new Filesystem();
+
+                               try {
+                                       //Create path
+                                       //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
+                                       //XXX: on CoW filesystems execute a chattr +C before filling
+                                       $filesystem->mkdir($dir, 0775);
+                               } catch (IOExceptionInterface $e) {
+                                       //Throw error
+                                       throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
+                               }
                        }
-               }
 
-               //Create image instance
-               $image = new \Imagick();
-
-               //Add new image
-               $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
-
-               //Create tile instance
-               $tile = new \Imagick();
-
-               //Get tile xy
-               $centerX = $this->map->longitudeToX($longitude, $zoom);
-               $centerY = $this->map->latitudeToY($latitude, $zoom);
-
-               //Calculate start xy
-               $startX = floor(floor($centerX) - $width / MapUtil::tz);
-               $startY = floor(floor($centerY) - $height / MapUtil::tz);
-
-               //Calculate end xy
-               $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
-               $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
-
-               for($x = $startX; $x <= $endX; $x++) {
-                       for($y = $startY; $y <= $endY; $y++) {
-                               //Set cache path
-                               $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png';
-
-                               //Without cache image
-                               if (!is_file($cache)) {
-                                       //Set tile url
-                                       $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
-
-                                       //Without cache path
-                                       if (!is_dir($dir = dirname($cache))) {
-                                               //Create filesystem object
-                                               $filesystem = new Filesystem();
-
-                                               try {
-                                                       //Create path
-                                                       //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
-                                                       $filesystem->mkdir($dir, 0775);
-                                               } catch (IOExceptionInterface $e) {
-                                                       //Throw error
-                                                       throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
+                       //Create image instance
+                       $image = new \Imagick();
+
+                       //Add new image
+                       $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
+
+                       //Create tile instance
+                       $tile = new \Imagick();
+
+                       //Get tile xy
+                       $centerX = $this->map->longitudeToX($longitude, $zoom);
+                       $centerY = $this->map->latitudeToY($latitude, $zoom);
+
+                       //Calculate start xy
+                       $startX = floor(floor($centerX) - $width / MapUtil::tz);
+                       $startY = floor(floor($centerY) - $height / MapUtil::tz);
+
+                       //Calculate end xy
+                       $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
+                       $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
+
+                       for($x = $startX; $x <= $endX; $x++) {
+                               for($y = $startY; $y <= $endY; $y++) {
+                                       //Set cache path
+                                       $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png';
+
+                                       //Without cache image
+                                       if (!is_file($cache)) {
+                                               //Set tile url
+                                               $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
+
+                                               //Without cache path
+                                               if (!is_dir($dir = dirname($cache))) {
+                                                       //Create filesystem object
+                                                       $filesystem = new Filesystem();
+
+                                                       try {
+                                                               //Create path
+                                                               //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
+                                                               $filesystem->mkdir($dir, 0775);
+                                                       } catch (IOExceptionInterface $e) {
+                                                               //Throw error
+                                                               throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
+                                                       }
                                                }
+
+                                               //Store tile in cache
+                                               file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
                                        }
 
-                                       //Store tile in cache
-                                       file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
-                               }
+                                       //Set dest x
+                                       $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
 
-                               //Set dest x
-                               $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
-
-                               //Set dest y
-                               $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
+                                       //Set dest y
+                                       $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
 
-                               //Read tile from cache
-                               $tile->readImage($cache);
+                                       //Read tile from cache
+                                       $tile->readImage($cache);
 
-                               //Compose image
-                               $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
+                                       //Compose image
+                                       $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
 
-                               //Clear tile
-                               $tile->clear();
+                                       //Clear tile
+                                       $tile->clear();
+                               }
                        }
-               }
 
-               //Add imagick draw instance
-               //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
-               $draw = new \ImagickDraw();
+                       //Add imagick draw instance
+                       //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
+                       $draw = new \ImagickDraw();
 
-               //Set text antialias
-               $draw->setTextAntialias(true);
+                       //Set text antialias
+                       $draw->setTextAntialias(true);
 
-               //Set stroke antialias
-               $draw->setStrokeAntialias(true);
+                       //Set stroke antialias
+                       $draw->setStrokeAntialias(true);
 
-               //Set text alignment
-               $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
+                       //Set text alignment
+                       $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
 
-               //Set gravity
-               $draw->setGravity(\Imagick::GRAVITY_CENTER);
+                       //Set gravity
+                       $draw->setGravity(\Imagick::GRAVITY_CENTER);
 
-               //Set fill color
-               $draw->setFillColor('#cff');
+                       //Set fill color
+                       $draw->setFillColor('#cff');
 
-               //Set stroke color
-               $draw->setStrokeColor('#00c3f9');
+                       //Set stroke color
+                       $draw->setStrokeColor('#00c3f9');
 
-               //Set stroke width
-               $draw->setStrokeWidth(2);
+                       //Set stroke width
+                       $draw->setStrokeWidth(2);
 
-               //Draw circle
-               $draw->circle($width/2 - 5, $height/2 - 5, $width/2 + 5, $height/2 + 5);
+                       //Draw circle
+                       $draw->circle($width/2 - 5, $height/2 - 5, $width/2 + 5, $height/2 + 5);
 
-               //Draw on image
-               $image->drawImage($draw);
+                       //Draw on image
+                       $image->drawImage($draw);
 
-               //Strip image exif data and properties
-               $image->stripImage();
+                       //Strip image exif data and properties
+                       $image->stripImage();
 
-               //Add latitude
-               //XXX: not supported by imagick :'(
-               $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
+                       //Add latitude
+                       //XXX: not supported by imagick :'(
+                       $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
 
-               //Add longitude
-               //XXX: not supported by imagick :'(
-               $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
+                       //Add longitude
+                       //XXX: not supported by imagick :'(
+                       $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
 
-               //Add description
-               //XXX: not supported by imagick :'(
-               #$image->setImageProperty('exif:Description', $caption);
+                       //Add description
+                       //XXX: not supported by imagick :'(
+                       #$image->setImageProperty('exif:Description', $caption);
 
-               //Set progressive jpeg
-               $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
+                       //Set progressive jpeg
+                       $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
 
-               //Set compression quality
-               //TODO: ajust that
-               $image->setImageCompressionQuality(70);
+                       //Set compression quality
+                       //TODO: ajust that
+                       $image->setImageCompressionQuality(70);
 
-               //Save image
-               if (!$image->writeImage($map)) {
-                       //Throw error
-                       throw new \Exception(sprintf('Unable to write image "%s"', $path));
+                       //Save image
+                       if (!$image->writeImage($map)) {
+                               //Throw error
+                               throw new \Exception(sprintf('Unable to write image "%s"', $path));
+                       }
+
+                       //Set mtime
+                       $mtime = stat($map)['mtime'];
                }
 
+               //Read map from cache
+               $response = new BinaryFileResponse($map);
+
+               //Set file name
+               $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'map-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
+
+               //Set etag
+               $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height])));
+
+               //Set last modified
+               $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
+
+               //Disable robot index
+               $response->headers->set('X-Robots-Tag', 'noindex');
+
+               //Set as public
+               $response->setPublic();
+
+               //Return 304 response if not modified
+               $response->isNotModified($request);
+
                //Return response
-               //TODO: générer l'image ici à partir du cache :p
-               #return new Response($image->getImageBlob());
-               return new BinaryFileResponse($map);
+               return $response;
        }
 
        /**
@@ -303,217 +316,235 @@ class MapController extends AbstractController implements ServiceSubscriberInter
         */
        public function multiMap(Request $request, string $hash, int $updated, float $latitude, float $longitude, string $coordinates, int $zoom, int $width, int $height): Response {
                //Without matching hash
-               if ($hash !== $this->slugger->serialize([$updated, $latitude, $longitude, $coordinate = $this->slugger->hash($coordinates), $zoom, $width, $height])) {
+               if ($hash !== $this->slugger->hash([$updated, $latitude, $longitude, $coordinate = $this->slugger->hash($coordinates), $zoom, $width, $height])) {
                        //Throw new exception
                        throw new NotFoundHttpException(sprintf('Unable to match multi map hash: %s', $hash));
                }
 
                //Set multi
-               $map = $this->public.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$coordinate.'/'.$width.'x'.$height.'.jpeg';
-
-               //With multi up to date file
-               if (is_file($map) && ($mtime = stat($map)['mtime']) && $mtime >= $updated) {
-                       //Read multi from cache
-                       //TODO: handle modified, etag, cache, etc ???
-                       return new BinaryFileResponse($map);
-               }
-
-               //Without existing multi
-               if (!is_dir($dir = dirname($map))) {
-                       //Create filesystem object
-                       $filesystem = new Filesystem();
-
-                       try {
-                               //Create path
-                               //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
-                               //XXX: on CoW filesystems execute a chattr +C before filling
-                               $filesystem->mkdir($dir, 0775);
-                       } catch (IOExceptionInterface $e) {
-                               //Throw error
-                               throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
+               $map = $this->path.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$coordinate.'/'.$width.'x'.$height.'.jpeg';
+
+               //Without multi up to date file
+               if (!is_file($map) || !($mtime = stat($map)['mtime']) || $mtime < $updated) {
+                       //Without existing multi path
+                       if (!is_dir($dir = dirname($map))) {
+                               //Create filesystem object
+                               $filesystem = new Filesystem();
+
+                               try {
+                                       //Create path
+                                       //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
+                                       //XXX: on CoW filesystems execute a chattr +C before filling
+                                       $filesystem->mkdir($dir, 0775);
+                               } catch (IOExceptionInterface $e) {
+                                       //Throw error
+                                       throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
+                               }
                        }
-               }
 
-               //Create image instance
-               $image = new \Imagick();
-
-               //Add new image
-               $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
-
-               //Create tile instance
-               $tile = new \Imagick();
-
-               //Get tile xy
-               $centerX = $this->map->longitudeToX($longitude, $zoom);
-               $centerY = $this->map->latitudeToY($latitude, $zoom);
-
-               //Calculate start xy
-               $startX = floor(floor($centerX) - $width / MapUtil::tz);
-               $startY = floor(floor($centerY) - $height / MapUtil::tz);
-
-               //Calculate end xy
-               $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
-               $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
-
-               for($x = $startX; $x <= $endX; $x++) {
-                       for($y = $startY; $y <= $endY; $y++) {
-                               //Set cache path
-                               $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png';
-
-                               //Without cache image
-                               if (!is_file($cache)) {
-                                       //Set tile url
-                                       $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
-
-                                       //Without cache path
-                                       if (!is_dir($dir = dirname($cache))) {
-                                               //Create filesystem object
-                                               $filesystem = new Filesystem();
-
-                                               try {
-                                                       //Create path
-                                                       //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
-                                                       $filesystem->mkdir($dir, 0775);
-                                               } catch (IOExceptionInterface $e) {
-                                                       //Throw error
-                                                       throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
+                       //Create image instance
+                       $image = new \Imagick();
+
+                       //Add new image
+                       $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
+
+                       //Create tile instance
+                       $tile = new \Imagick();
+
+                       //Get tile xy
+                       $centerX = $this->map->longitudeToX($longitude, $zoom);
+                       $centerY = $this->map->latitudeToY($latitude, $zoom);
+
+                       //Calculate start xy
+                       $startX = floor(floor($centerX) - $width / MapUtil::tz);
+                       $startY = floor(floor($centerY) - $height / MapUtil::tz);
+
+                       //Calculate end xy
+                       $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
+                       $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
+
+                       for($x = $startX; $x <= $endX; $x++) {
+                               for($y = $startY; $y <= $endY; $y++) {
+                                       //Set cache path
+                                       $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png';
+
+                                       //Without cache image
+                                       if (!is_file($cache)) {
+                                               //Set tile url
+                                               $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
+
+                                               //Without cache path
+                                               if (!is_dir($dir = dirname($cache))) {
+                                                       //Create filesystem object
+                                                       $filesystem = new Filesystem();
+
+                                                       try {
+                                                               //Create path
+                                                               //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
+                                                               $filesystem->mkdir($dir, 0775);
+                                                       } catch (IOExceptionInterface $e) {
+                                                               //Throw error
+                                                               throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
+                                                       }
                                                }
-                                       }
 
-                                       //Store tile in cache
-                                       file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
-                               }
+                                               //Store tile in cache
+                                               file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
+                                       }
 
-                               //Set dest x
-                               $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
+                                       //Set dest x
+                                       $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
 
-                               //Set dest y
-                               $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
+                                       //Set dest y
+                                       $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
 
-                               //Read tile from cache
-                               $tile->readImage($cache);
+                                       //Read tile from cache
+                                       $tile->readImage($cache);
 
-                               //Compose image
-                               $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
+                                       //Compose image
+                                       $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
 
-                               //Clear tile
-                               $tile->clear();
+                                       //Clear tile
+                                       $tile->clear();
+                               }
                        }
-               }
 
-               //Add imagick draw instance
-               //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
-               $draw = new \ImagickDraw();
+                       //Add imagick draw instance
+                       //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
+                       $draw = new \ImagickDraw();
 
-               //Set text antialias
-               $draw->setTextAntialias(true);
+                       //Set text antialias
+                       $draw->setTextAntialias(true);
 
-               //Set stroke antialias
-               $draw->setStrokeAntialias(true);
+                       //Set stroke antialias
+                       $draw->setStrokeAntialias(true);
 
-               //Set text alignment
-               $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
+                       //Set text alignment
+                       $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
 
-               //Set gravity
-               $draw->setGravity(\Imagick::GRAVITY_CENTER);
+                       //Set gravity
+                       $draw->setGravity(\Imagick::GRAVITY_CENTER);
 
-               //Convert to array      
-               $coordinates = array_reverse(array_map(function ($v) { $p = strpos($v, ','); return ['latitude' => floatval(substr($v, 0, $p)), 'longitude' => floatval(substr($v, $p + 1))]; }, explode('-', $coordinates)), true);
+                       //Convert to array
+                       $coordinates = array_reverse(array_map(function ($v) { $p = strpos($v, ','); return ['latitude' => floatval(substr($v, 0, $p)), 'longitude' => floatval(substr($v, $p + 1))]; }, explode('-', $coordinates)), true);
 
-               //Iterate on locations
-               foreach($coordinates as $id => $coordinate) {
-                       //Set dest x
-                       $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $this->map->longitudeToX(floatval($coordinate['longitude']), $zoom))));
+                       //Iterate on locations
+                       foreach($coordinates as $id => $coordinate) {
+                               //Set dest x
+                               $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $this->map->longitudeToX(floatval($coordinate['longitude']), $zoom))));
 
-                       //Set dest y
-                       $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $this->map->latitudeToY(floatval($coordinate['latitude']), $zoom))));
+                               //Set dest y
+                               $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $this->map->latitudeToY(floatval($coordinate['latitude']), $zoom))));
 
-                       //Set fill color
-                       $draw->setFillColor($this->map->fill);
+                               //Set fill color
+                               $draw->setFillColor($this->map->fill);
 
-                       //Set font size
-                       $draw->setFontSize($this->map->fontSize);
+                               //Set font size
+                               $draw->setFontSize($this->map->fontSize);
 
-                       //Set stroke color
-                       $draw->setStrokeColor($this->map->stroke);
+                               //Set stroke color
+                               $draw->setStrokeColor($this->map->stroke);
 
-                       //Set circle radius
-                       $radius = $this->map->radius;
+                               //Set circle radius
+                               $radius = $this->map->radius;
 
-                       //Set stroke width
-                       $stroke = $this->map->strokeWidth;
+                               //Set stroke width
+                               $stroke = $this->map->strokeWidth;
 
-                       //With matching position
-                       if ($coordinate['latitude'] === $latitude && $coordinate['longitude'] == $longitude) {
-                               //Set fill color
-                               $draw->setFillColor($this->map->highFill);
+                               //With matching position
+                               if ($coordinate['latitude'] === $latitude && $coordinate['longitude'] == $longitude) {
+                                       //Set fill color
+                                       $draw->setFillColor($this->map->highFill);
 
-                               //Set font size
-                               $draw->setFontSize($this->map->highFontSize);
+                                       //Set font size
+                                       $draw->setFontSize($this->map->highFontSize);
 
-                               //Set stroke color
-                               $draw->setStrokeColor($this->map->highStroke);
+                                       //Set stroke color
+                                       $draw->setStrokeColor($this->map->highStroke);
 
-                               //Set circle radius
-                               $radius = $this->map->highRadius;
+                                       //Set circle radius
+                                       $radius = $this->map->highRadius;
+
+                                       //Set stroke width
+                                       $stroke = $this->map->highStrokeWidth;
+                               }
 
                                //Set stroke width
-                               $stroke = $this->map->highStrokeWidth;
-                       }
+                               $draw->setStrokeWidth($stroke);
 
-                       //Set stroke width
-                       $draw->setStrokeWidth($stroke);
+                               //Draw circle
+                               $draw->circle($destX - $radius, $destY - $radius, $destX + $radius, $destY + $radius);
 
-                       //Draw circle
-                       $draw->circle($destX - $radius, $destY - $radius, $destX + $radius, $destY + $radius);
+                               //Set fill color
+                               $draw->setFillColor($draw->getStrokeColor());
 
-                       //Set fill color
-                       $draw->setFillColor($draw->getStrokeColor());
+                               //Set stroke width
+                               $draw->setStrokeWidth($stroke / 4);
 
-                       //Set stroke width
-                       $draw->setStrokeWidth($stroke / 4);
+                               //Get font metrics
+                               #$metrics = $image->queryFontMetrics($draw, strval($id));
 
-                       //Get font metrics
-                       $metrics = $image->queryFontMetrics($draw, strval($id));
+                               //Add annotation
+                               $draw->annotation($destX - $radius, $destY + $stroke, strval($id));
+                       }
 
-                       //Add annotation
-                       $draw->annotation($destX - $radius, $destY + $stroke, strval($id));
-               }
+                       //Draw on image
+                       $image->drawImage($draw);
 
-               //Draw on image
-               $image->drawImage($draw);
+                       //Strip image exif data and properties
+                       $image->stripImage();
 
-               //Strip image exif data and properties
-               $image->stripImage();
+                       //Add latitude
+                       //XXX: not supported by imagick :'(
+                       $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
 
-               //Add latitude
-               //XXX: not supported by imagick :'(
-               $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
+                       //Add longitude
+                       //XXX: not supported by imagick :'(
+                       $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
 
-               //Add longitude
-               //XXX: not supported by imagick :'(
-               $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
+                       //Add description
+                       //XXX: not supported by imagick :'(
+                       #$image->setImageProperty('exif:Description', $caption);
 
-               //Add description
-               //XXX: not supported by imagick :'(
-               #$image->setImageProperty('exif:Description', $caption);
+                       //Set progressive jpeg
+                       $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
 
-               //Set progressive jpeg
-               $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
+                       //Set compression quality
+                       //TODO: ajust that
+                       $image->setImageCompressionQuality(70);
 
-               //Set compression quality
-               //TODO: ajust that
-               $image->setImageCompressionQuality(70);
+                       //Save image
+                       if (!$image->writeImage($map)) {
+                               //Throw error
+                               throw new \Exception(sprintf('Unable to write image "%s"', $path));
+                       }
 
-               //Save image
-               if (!$image->writeImage($map)) {
-                       //Throw error
-                       throw new \Exception(sprintf('Unable to write image "%s"', $path));
+                       //Set mtime
+                       $mtime = stat($map)['mtime'];
                }
 
+               //Read map from cache
+               $response = new BinaryFileResponse($map);
+
+               //Set file name
+               $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'multimap-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
+
+               //Set etag
+               $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height])));
+
+               //Set last modified
+               $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
+
+               //Disable robot index
+               $response->headers->set('X-Robots-Tag', 'noindex');
+
+               //Set as public
+               $response->setPublic();
+
+               //Return 304 response if not modified
+               $response->isNotModified($request);
+
                //Return response
-               //TODO: générer l'image ici à partir du cache :p
-               #return new Response($image->getImageBlob());
-               return new BinaryFileResponse($map);
+               return $response;
        }
 }