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\Controller
;
14 use Rapsys\PackBundle\Util\MapUtil
;
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 MapController
extends AbstractController
implements ServiceSubscriberInterface
{
35 * The stream context instance
40 * Creates a new osm controller
42 * @param ContainerInterface $container The ContainerInterface instance
43 * @param MapUtil $map The MapUtil instance
44 * @param SluggerUtil $slugger The SluggerUtil instance
45 * @param string $cache The cache path
46 * @param string $path The public path
47 * @param string $prefix The prefix
48 * @param string $url The tile server url
50 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
) {
52 $this->ctx
= stream_context_create(
55 #'header' => ['Referer: https://www.openstreetmap.org/'],
56 'max_redirects' => $_ENV['RAPSYSPACK_REDIRECT'] ?? 20,
57 'timeout' => $_ENV['RAPSYSPACK_TIMEOUT'] ?? (($timeout = ini_get('default_socket_timeout')) !== false && $timeout !== "" ? (float)$timeout : 60),
58 'user_agent' => $_ENV['RAPSYSPACK_AGENT'] ?? (($agent = ini_get('user_agent')) !== false && $agent !== "" ? (string)$agent : RapsysPackBundle
::getAlias().'/'.RapsysPackBundle
::getVersion())
67 * @param Request $request The Request instance
68 * @param string $hash The hash
69 * @param int $updated The updated timestamp
70 * @param float $latitude The latitude
71 * @param float $longitude The longitude
72 * @param int $zoom The zoom
73 * @param int $width The width
74 * @param int $height The height
75 * @return Response The rendered image
77 public function map(Request
$request, string $hash, int $updated, float $latitude, float $longitude, int $zoom, int $width, int $height): Response
{
78 //Without matching hash
79 if ($hash !== $this->slugger
->hash([$updated, $latitude, $longitude, $zoom, $width, $height])) {
81 throw new NotFoundHttpException(sprintf('Unable to match map hash: %s', $hash));
85 $map = $this->path
.'/'.$this->prefix
.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$width.'x'.$height.'.jpeg';
87 //Without multi up to date file
88 if (!is_file($map) || !($mtime = stat($map)['mtime']) || $mtime < $updated) {
89 //Without existing map path
90 if (!is_dir($dir = dirname($map))) {
91 //Create filesystem object
92 $filesystem = new Filesystem();
96 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
97 //XXX: on CoW filesystems execute a chattr +C before filling
98 $filesystem->mkdir($dir, 0775);
99 } catch (IOExceptionInterface
$e) {
101 throw new \
Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
105 //Create image instance
106 $image = new \
Imagick();
109 $image->newImage($width, $height, new \
ImagickPixel('transparent'), 'jpeg');
111 //Create tile instance
112 $tile = new \
Imagick();
115 $centerX = $this->map
->longitudeToX($longitude, $zoom);
116 $centerY = $this->map
->latitudeToY($latitude, $zoom);
119 $startX = floor(floor($centerX) - $width / MapUtil
::tz
);
120 $startY = floor(floor($centerY) - $height / MapUtil
::tz
);
123 $endX = ceil(ceil($centerX) +
$width / MapUtil
::tz
);
124 $endY = ceil(ceil($centerY) +
$height / MapUtil
::tz
);
126 for($x = $startX; $x <= $endX; $x++
) {
127 for($y = $startY; $y <= $endY; $y++
) {
129 $cache = $this->cache
.'/'.$this->prefix
.'/'.$zoom.'/'.$x.'/'.$y.'.png';
131 //Without cache image
132 if (!is_file($cache)) {
134 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url
);
137 if (!is_dir($dir = dirname($cache))) {
138 //Create filesystem object
139 $filesystem = new Filesystem();
143 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
144 $filesystem->mkdir($dir, 0775);
145 } catch (IOExceptionInterface
$e) {
147 throw new \
Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
151 //Store tile in cache
152 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx
));
156 $destX = intval(floor($width / 2 - MapUtil
::tz
* ($centerX - $x)));
159 $destY = intval(floor($height / 2 - MapUtil
::tz
* ($centerY - $y)));
161 //Read tile from cache
162 $tile->readImage($cache);
165 $image->compositeImage($tile, \Imagick
::COMPOSITE_OVER
, $destX, $destY);
172 //Add imagick draw instance
173 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
174 $draw = new \
ImagickDraw();
177 $draw->setTextAntialias(true);
179 //Set stroke antialias
180 $draw->setStrokeAntialias(true);
183 $draw->setTextAlignment(\Imagick
::ALIGN_CENTER
);
186 $draw->setGravity(\Imagick
::GRAVITY_CENTER
);
189 $draw->setFillColor('#cff');
192 $draw->setStrokeColor('#00c3f9');
195 $draw->setStrokeWidth(2);
198 $draw->circle($width/2 - 5, $height/2 - 5, $width/2 +
5, $height/2 +
5);
201 $image->drawImage($draw);
203 //Strip image exif data and properties
204 $image->stripImage();
207 //XXX: not supported by imagick :'(
208 $image->setImageProperty('exif:GPSLatitude', $this->map
->latitudeToSexagesimal($latitude));
211 //XXX: not supported by imagick :'(
212 $image->setImageProperty('exif:GPSLongitude', $this->map
->longitudeToSexagesimal($longitude));
215 //XXX: not supported by imagick :'(
216 #$image->setImageProperty('exif:Description', $caption);
218 //Set progressive jpeg
219 $image->setInterlaceScheme(\Imagick
::INTERLACE_PLANE
);
221 //Set compression quality
223 $image->setImageCompressionQuality(70);
226 if (!$image->writeImage($map)) {
228 throw new \
Exception(sprintf('Unable to write image "%s"', $path));
232 $mtime = stat($map)['mtime'];
235 //Read map from cache
236 $response = new BinaryFileResponse($map);
239 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, 'map-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
242 $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height])));
245 $response->setLastModified(\DateTime
::createFromFormat('U', strval($mtime)));
247 //Disable robot index
248 $response->headers
->set('X-Robots-Tag', 'noindex');
251 $response->setPublic();
253 //Return 304 response if not modified
254 $response->isNotModified($request);
261 * Return multi map image
263 * @param Request $request The Request instance
264 * @param string $hash The hash
265 * @param int $updated The updated timestamp
266 * @param float $latitude The latitude
267 * @param float $longitude The longitude
268 * @param string $coordinates The coordinates
269 * @param int $zoom The zoom
270 * @param int $width The width
271 * @param int $height The height
272 * @return Response The rendered image
274 public function multiMap(Request
$request, string $hash, int $updated, float $latitude, float $longitude, string $coordinates, int $zoom, int $width, int $height): Response
{
275 //Without matching hash
276 if ($hash !== $this->slugger
->hash([$updated, $latitude, $longitude, $coordinate = $this->slugger
->hash($coordinates), $zoom, $width, $height])) {
277 //Throw new exception
278 throw new NotFoundHttpException(sprintf('Unable to match multi map hash: %s', $hash));
282 $map = $this->path
.'/'.$this->prefix
.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$coordinate.'/'.$width.'x'.$height.'.jpeg';
284 //Without multi up to date file
285 if (!is_file($map) || !($mtime = stat($map)['mtime']) || $mtime < $updated) {
286 //Without existing multi path
287 if (!is_dir($dir = dirname($map))) {
288 //Create filesystem object
289 $filesystem = new Filesystem();
293 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
294 //XXX: on CoW filesystems execute a chattr +C before filling
295 $filesystem->mkdir($dir, 0775);
296 } catch (IOExceptionInterface
$e) {
298 throw new \
Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
302 //Create image instance
303 $image = new \
Imagick();
306 $image->newImage($width, $height, new \
ImagickPixel('transparent'), 'jpeg');
308 //Create tile instance
309 $tile = new \
Imagick();
312 $centerX = $this->map
->longitudeToX($longitude, $zoom);
313 $centerY = $this->map
->latitudeToY($latitude, $zoom);
316 $startX = floor(floor($centerX) - $width / MapUtil
::tz
);
317 $startY = floor(floor($centerY) - $height / MapUtil
::tz
);
320 $endX = ceil(ceil($centerX) +
$width / MapUtil
::tz
);
321 $endY = ceil(ceil($centerY) +
$height / MapUtil
::tz
);
323 for($x = $startX; $x <= $endX; $x++
) {
324 for($y = $startY; $y <= $endY; $y++
) {
326 $cache = $this->cache
.'/'.$this->prefix
.'/'.$zoom.'/'.$x.'/'.$y.'.png';
328 //Without cache image
329 if (!is_file($cache)) {
331 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url
);
334 if (!is_dir($dir = dirname($cache))) {
335 //Create filesystem object
336 $filesystem = new Filesystem();
340 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
341 $filesystem->mkdir($dir, 0775);
342 } catch (IOExceptionInterface
$e) {
344 throw new \
Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
348 //Store tile in cache
349 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx
));
353 $destX = intval(floor($width / 2 - MapUtil
::tz
* ($centerX - $x)));
356 $destY = intval(floor($height / 2 - MapUtil
::tz
* ($centerY - $y)));
358 //Read tile from cache
359 $tile->readImage($cache);
362 $image->compositeImage($tile, \Imagick
::COMPOSITE_OVER
, $destX, $destY);
369 //Add imagick draw instance
370 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
371 $draw = new \
ImagickDraw();
374 $draw->setTextAntialias(true);
376 //Set stroke antialias
377 $draw->setStrokeAntialias(true);
380 $draw->setTextAlignment(\Imagick
::ALIGN_CENTER
);
383 $draw->setGravity(\Imagick
::GRAVITY_CENTER
);
386 $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);
388 //Iterate on locations
389 foreach($coordinates as $id => $coordinate) {
391 $destX = intval(floor($width / 2 - MapUtil
::tz
* ($centerX - $this->map
->longitudeToX(floatval($coordinate['longitude']), $zoom))));
394 $destY = intval(floor($height / 2 - MapUtil
::tz
* ($centerY - $this->map
->latitudeToY(floatval($coordinate['latitude']), $zoom))));
397 $draw->setFillColor($this->map
->getFill());
400 $draw->setFontSize($this->map
->getFontSize());
403 $draw->setStrokeColor($this->map
->getStroke());
406 $radius = $this->map
->getRadius();
409 $stroke = $this->map
->getStrokeWidth();
411 //With matching position
412 if ($coordinate['latitude'] === $latitude && $coordinate['longitude'] == $longitude) {
414 $draw->setFillColor($this->map
->getHighFill());
417 $draw->setFontSize($this->map
->getHighFontSize());
420 $draw->setStrokeColor($this->map
->getHighStroke());
423 $radius = $this->map
->getHighRadius();
426 $stroke = $this->map
->getHighStrokeWidth();
430 $draw->setStrokeWidth($stroke);
433 $draw->circle($destX - $radius, $destY - $radius, $destX +
$radius, $destY +
$radius);
436 $draw->setFillColor($draw->getStrokeColor());
439 $draw->setStrokeWidth($stroke / 4);
442 #$metrics = $image->queryFontMetrics($draw, strval($id));
445 $draw->annotation($destX - $radius, $destY +
$stroke, strval($id));
449 $image->drawImage($draw);
451 //Strip image exif data and properties
452 $image->stripImage();
455 //XXX: not supported by imagick :'(
456 $image->setImageProperty('exif:GPSLatitude', $this->map
->latitudeToSexagesimal($latitude));
459 //XXX: not supported by imagick :'(
460 $image->setImageProperty('exif:GPSLongitude', $this->map
->longitudeToSexagesimal($longitude));
463 //XXX: not supported by imagick :'(
464 #$image->setImageProperty('exif:Description', $caption);
466 //Set progressive jpeg
467 $image->setInterlaceScheme(\Imagick
::INTERLACE_PLANE
);
469 //Set compression quality
471 $image->setImageCompressionQuality(70);
474 if (!$image->writeImage($map)) {
476 throw new \
Exception(sprintf('Unable to write image "%s"', $path));
480 $mtime = stat($map)['mtime'];
483 //Read map from cache
484 $response = new BinaryFileResponse($map);
487 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, 'multimap-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
490 $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height])));
493 $response->setLastModified(\DateTime
::createFromFormat('U', strval($mtime)));
495 //Disable robot index
496 $response->headers
->set('X-Robots-Tag', 'noindex');
499 $response->setPublic();
501 //Return 304 response if not modified
502 $response->isNotModified($request);