X-Git-Url: https://git.rapsys.eu/packbundle/blobdiff_plain/42a99324bb26ad7305e738882ac2de332ce5cc38..780564d1998c2e41b4f9a145b5191a66dbaf6ed4:/Controller/MapController.php?ds=sidebyside diff --git a/Controller/MapController.php b/Controller/MapController.php index 7bd6fe7..255c47b 100644 --- a/Controller/MapController.php +++ b/Controller/MapController.php @@ -11,8 +11,10 @@ namespace Rapsys\PackBundle\Controller; +use Psr\Container\ContainerInterface; + +use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Filesystem\Exception\IOExceptionInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\BinaryFileResponse; @@ -29,84 +31,34 @@ use Rapsys\PackBundle\Util\SluggerUtil; * {@inheritdoc} */ class MapController extends AbstractController implements ServiceSubscriberInterface { - /** - * The cache path - */ - protected string $cache; - - /** - * The ContainerInterface instance - * - * @var ContainerInterface - */ - protected $container; - /** * The stream context instance */ protected mixed $ctx; /** - * The MapUtil instance - */ - protected MapUtil $map; - - /** - * The public path - */ - protected string $public; - - /** - * The SluggerUtil instance - */ - protected SluggerUtil $slugger; - - /** - * The tile server url - */ - 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) { - //Set cache - $this->cache = $cache; - - //Set container - $this->container = $container; - + function __construct(protected ContainerInterface $container, protected MapUtil $map, protected SluggerUtil $slugger, protected string $cache = '../var/cache', protected string $path = './bundles/rapsyspack', protected string $prefix = 'map', protected string $url = MapUtil::osm) { //Set ctx $this->ctx = stream_context_create( [ 'http' => [ #'header' => ['Referer: https://www.openstreetmap.org/'], - 'max_redirects' => 5, - 'timeout' => (int)ini_get('default_socket_timeout'), - #'user_agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36', - 'user_agent' => (string)ini_get('user_agent')?:'rapsys_pack/2.0.0', + 'max_redirects' => $_ENV['RAPSYSPACK_REDIRECT'] ?? 20, + 'timeout' => $_ENV['RAPSYSPACK_TIMEOUT'] ?? (($timeout = ini_get('default_socket_timeout')) !== false && $timeout !== "" ? (float)$timeout : 60), + 'user_agent' => $_ENV['RAPSYSPACK_AGENT'] ?? (($agent = ini_get('user_agent')) !== false && $agent !== "" ? (string)$agent : RapsysPackBundle::getAlias().'/'.RapsysPackBundle::getVersion()) ] ] ); - - //Set map - $this->map = $map; - - //Set public - $this->public = $public; - - //Set slugger - $this->slugger = $slugger; - - //Set url - $this->url = $url; } /** @@ -124,167 +76,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.'/'.$this->prefix.'/'.$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.'/'.$this->prefix.'/'.$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 +273,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.'/'.$this->prefix.'/'.$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.'/'.$this->prefix.'/'.$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->getFill()); - //Set font size - $draw->setFontSize($this->map->fontSize); + //Set font size + $draw->setFontSize($this->map->getFontSize()); - //Set stroke color - $draw->setStrokeColor($this->map->stroke); + //Set stroke color + $draw->setStrokeColor($this->map->getStroke()); - //Set circle radius - $radius = $this->map->radius; + //Set circle radius + $radius = $this->map->getRadius(); - //Set stroke width - $stroke = $this->map->strokeWidth; + //Set stroke width + $stroke = $this->map->getStrokeWidth(); - //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->getHighFill()); - //Set font size - $draw->setFontSize($this->map->highFontSize); + //Set font size + $draw->setFontSize($this->map->getHighFontSize()); - //Set stroke color - $draw->setStrokeColor($this->map->highStroke); + //Set stroke color + $draw->setStrokeColor($this->map->getHighStroke()); - //Set circle radius - $radius = $this->map->highRadius; + //Set circle radius + $radius = $this->map->getHighRadius(); + + //Set stroke width + $stroke = $this->map->getHighStrokeWidth(); + } //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; } }