]> Raphaël G. Git Repositories - packbundle/blob - Controller/MapController.php
Add static function getVersion returning bundle version
[packbundle] / Controller / MapController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Controller;
13
14 use Symfony\Component\HttpFoundation\HeaderUtils;
15 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
18 use Symfony\Component\Filesystem\Filesystem;
19 use Symfony\Component\HttpFoundation\BinaryFileResponse;
20 use Symfony\Component\HttpFoundation\Request;
21 use Symfony\Component\HttpFoundation\Response;
22 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23 use Symfony\Component\Routing\RequestContext;
24 use Symfony\Contracts\Service\ServiceSubscriberInterface;
25
26 use Rapsys\PackBundle\Util\MapUtil;
27 use Rapsys\PackBundle\Util\SluggerUtil;
28
29 /**
30 * {@inheritdoc}
31 */
32 class MapController extends AbstractController implements ServiceSubscriberInterface {
33 /**
34 * The cache path
35 */
36 protected string $cache;
37
38 /**
39 * The ContainerInterface instance
40 *
41 * @var ContainerInterface
42 */
43 protected $container;
44
45 /**
46 * The stream context instance
47 */
48 protected mixed $ctx;
49
50 /**
51 * The MapUtil instance
52 */
53 protected MapUtil $map;
54
55 /**
56 * The public path
57 */
58 protected string $path;
59
60 /**
61 * The SluggerUtil instance
62 */
63 protected SluggerUtil $slugger;
64
65 /**
66 * The tile server url
67 */
68 protected string $url;
69
70 /**
71 * Creates a new osm controller
72 *
73 * @param ContainerInterface $container The ContainerInterface instance
74 * @param MapUtil $map The MapUtil instance
75 * @param SluggerUtil $slugger The SluggerUtil instance
76 * @param string $cache The cache path
77 * @param string $path The public path
78 # * @param string $prefix The prefix
79 * @param string $url The tile server url
80 */
81 function __construct(ContainerInterface $container, MapUtil $map, SluggerUtil $slugger, string $cache = '../var/cache', string $path = './bundles/rapsyspack', string $prefix = 'map', string $url = MapUtil::osm) {
82 //Set cache
83 $this->cache = $cache.'/'.$prefix;
84
85 //Set container
86 $this->container = $container;
87
88 //Set ctx
89 $this->ctx = stream_context_create(
90 [
91 'http' => [
92 #'header' => ['Referer: https://www.openstreetmap.org/'],
93 'max_redirects' => 5,
94 'timeout' => (int)ini_get('default_socket_timeout'),
95 #'user_agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36',
96 'user_agent' => (string)ini_get('user_agent')?:'rapsys_pack/2.0.0',
97 ]
98 ]
99 );
100
101 //Set map
102 $this->map = $map;
103
104 //Set path
105 $this->path = $path.'/'.$prefix;
106
107 //Set slugger
108 $this->slugger = $slugger;
109
110 //Set url
111 $this->url = $url;
112 }
113
114 /**
115 * Return map image
116 *
117 * @param Request $request The Request instance
118 * @param string $hash The hash
119 * @param int $updated The updated timestamp
120 * @param float $latitude The latitude
121 * @param float $longitude The longitude
122 * @param int $zoom The zoom
123 * @param int $width The width
124 * @param int $height The height
125 * @return Response The rendered image
126 */
127 public function map(Request $request, string $hash, int $updated, float $latitude, float $longitude, int $zoom, int $width, int $height): Response {
128 //Without matching hash
129 if ($hash !== $this->slugger->hash([$updated, $latitude, $longitude, $zoom, $width, $height])) {
130 //Throw new exception
131 throw new NotFoundHttpException(sprintf('Unable to match map hash: %s', $hash));
132 }
133
134 //Set map
135 $map = $this->path.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$width.'x'.$height.'.jpeg';
136
137 //Without multi up to date file
138 if (!is_file($map) || !($mtime = stat($map)['mtime']) || $mtime < $updated) {
139 //Without existing map path
140 if (!is_dir($dir = dirname($map))) {
141 //Create filesystem object
142 $filesystem = new Filesystem();
143
144 try {
145 //Create path
146 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
147 //XXX: on CoW filesystems execute a chattr +C before filling
148 $filesystem->mkdir($dir, 0775);
149 } catch (IOExceptionInterface $e) {
150 //Throw error
151 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
152 }
153 }
154
155 //Create image instance
156 $image = new \Imagick();
157
158 //Add new image
159 $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
160
161 //Create tile instance
162 $tile = new \Imagick();
163
164 //Get tile xy
165 $centerX = $this->map->longitudeToX($longitude, $zoom);
166 $centerY = $this->map->latitudeToY($latitude, $zoom);
167
168 //Calculate start xy
169 $startX = floor(floor($centerX) - $width / MapUtil::tz);
170 $startY = floor(floor($centerY) - $height / MapUtil::tz);
171
172 //Calculate end xy
173 $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
174 $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
175
176 for($x = $startX; $x <= $endX; $x++) {
177 for($y = $startY; $y <= $endY; $y++) {
178 //Set cache path
179 $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png';
180
181 //Without cache image
182 if (!is_file($cache)) {
183 //Set tile url
184 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
185
186 //Without cache path
187 if (!is_dir($dir = dirname($cache))) {
188 //Create filesystem object
189 $filesystem = new Filesystem();
190
191 try {
192 //Create path
193 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
194 $filesystem->mkdir($dir, 0775);
195 } catch (IOExceptionInterface $e) {
196 //Throw error
197 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
198 }
199 }
200
201 //Store tile in cache
202 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
203 }
204
205 //Set dest x
206 $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
207
208 //Set dest y
209 $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
210
211 //Read tile from cache
212 $tile->readImage($cache);
213
214 //Compose image
215 $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
216
217 //Clear tile
218 $tile->clear();
219 }
220 }
221
222 //Add imagick draw instance
223 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
224 $draw = new \ImagickDraw();
225
226 //Set text antialias
227 $draw->setTextAntialias(true);
228
229 //Set stroke antialias
230 $draw->setStrokeAntialias(true);
231
232 //Set text alignment
233 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
234
235 //Set gravity
236 $draw->setGravity(\Imagick::GRAVITY_CENTER);
237
238 //Set fill color
239 $draw->setFillColor('#cff');
240
241 //Set stroke color
242 $draw->setStrokeColor('#00c3f9');
243
244 //Set stroke width
245 $draw->setStrokeWidth(2);
246
247 //Draw circle
248 $draw->circle($width/2 - 5, $height/2 - 5, $width/2 + 5, $height/2 + 5);
249
250 //Draw on image
251 $image->drawImage($draw);
252
253 //Strip image exif data and properties
254 $image->stripImage();
255
256 //Add latitude
257 //XXX: not supported by imagick :'(
258 $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
259
260 //Add longitude
261 //XXX: not supported by imagick :'(
262 $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
263
264 //Add description
265 //XXX: not supported by imagick :'(
266 #$image->setImageProperty('exif:Description', $caption);
267
268 //Set progressive jpeg
269 $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
270
271 //Set compression quality
272 //TODO: ajust that
273 $image->setImageCompressionQuality(70);
274
275 //Save image
276 if (!$image->writeImage($map)) {
277 //Throw error
278 throw new \Exception(sprintf('Unable to write image "%s"', $path));
279 }
280
281 //Set mtime
282 $mtime = stat($map)['mtime'];
283 }
284
285 //Read map from cache
286 $response = new BinaryFileResponse($map);
287
288 //Set file name
289 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'map-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
290
291 //Set etag
292 $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height])));
293
294 //Set last modified
295 $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
296
297 //Disable robot index
298 $response->headers->set('X-Robots-Tag', 'noindex');
299
300 //Set as public
301 $response->setPublic();
302
303 //Return 304 response if not modified
304 $response->isNotModified($request);
305
306 //Return response
307 return $response;
308 }
309
310 /**
311 * Return multi map image
312 *
313 * @param Request $request The Request instance
314 * @param string $hash The hash
315 * @param int $updated The updated timestamp
316 * @param float $latitude The latitude
317 * @param float $longitude The longitude
318 * @param string $coordinates The coordinates
319 * @param int $zoom The zoom
320 * @param int $width The width
321 * @param int $height The height
322 * @return Response The rendered image
323 */
324 public function multiMap(Request $request, string $hash, int $updated, float $latitude, float $longitude, string $coordinates, int $zoom, int $width, int $height): Response {
325 //Without matching hash
326 if ($hash !== $this->slugger->hash([$updated, $latitude, $longitude, $coordinate = $this->slugger->hash($coordinates), $zoom, $width, $height])) {
327 //Throw new exception
328 throw new NotFoundHttpException(sprintf('Unable to match multi map hash: %s', $hash));
329 }
330
331 //Set multi
332 $map = $this->path.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$coordinate.'/'.$width.'x'.$height.'.jpeg';
333
334 //Without multi up to date file
335 if (!is_file($map) || !($mtime = stat($map)['mtime']) || $mtime < $updated) {
336 //Without existing multi path
337 if (!is_dir($dir = dirname($map))) {
338 //Create filesystem object
339 $filesystem = new Filesystem();
340
341 try {
342 //Create path
343 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
344 //XXX: on CoW filesystems execute a chattr +C before filling
345 $filesystem->mkdir($dir, 0775);
346 } catch (IOExceptionInterface $e) {
347 //Throw error
348 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
349 }
350 }
351
352 //Create image instance
353 $image = new \Imagick();
354
355 //Add new image
356 $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
357
358 //Create tile instance
359 $tile = new \Imagick();
360
361 //Get tile xy
362 $centerX = $this->map->longitudeToX($longitude, $zoom);
363 $centerY = $this->map->latitudeToY($latitude, $zoom);
364
365 //Calculate start xy
366 $startX = floor(floor($centerX) - $width / MapUtil::tz);
367 $startY = floor(floor($centerY) - $height / MapUtil::tz);
368
369 //Calculate end xy
370 $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
371 $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
372
373 for($x = $startX; $x <= $endX; $x++) {
374 for($y = $startY; $y <= $endY; $y++) {
375 //Set cache path
376 $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png';
377
378 //Without cache image
379 if (!is_file($cache)) {
380 //Set tile url
381 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
382
383 //Without cache path
384 if (!is_dir($dir = dirname($cache))) {
385 //Create filesystem object
386 $filesystem = new Filesystem();
387
388 try {
389 //Create path
390 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
391 $filesystem->mkdir($dir, 0775);
392 } catch (IOExceptionInterface $e) {
393 //Throw error
394 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
395 }
396 }
397
398 //Store tile in cache
399 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
400 }
401
402 //Set dest x
403 $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
404
405 //Set dest y
406 $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
407
408 //Read tile from cache
409 $tile->readImage($cache);
410
411 //Compose image
412 $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
413
414 //Clear tile
415 $tile->clear();
416 }
417 }
418
419 //Add imagick draw instance
420 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
421 $draw = new \ImagickDraw();
422
423 //Set text antialias
424 $draw->setTextAntialias(true);
425
426 //Set stroke antialias
427 $draw->setStrokeAntialias(true);
428
429 //Set text alignment
430 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
431
432 //Set gravity
433 $draw->setGravity(\Imagick::GRAVITY_CENTER);
434
435 //Convert to array
436 $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);
437
438 //Iterate on locations
439 foreach($coordinates as $id => $coordinate) {
440 //Set dest x
441 $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $this->map->longitudeToX(floatval($coordinate['longitude']), $zoom))));
442
443 //Set dest y
444 $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $this->map->latitudeToY(floatval($coordinate['latitude']), $zoom))));
445
446 //Set fill color
447 $draw->setFillColor($this->map->fill);
448
449 //Set font size
450 $draw->setFontSize($this->map->fontSize);
451
452 //Set stroke color
453 $draw->setStrokeColor($this->map->stroke);
454
455 //Set circle radius
456 $radius = $this->map->radius;
457
458 //Set stroke width
459 $stroke = $this->map->strokeWidth;
460
461 //With matching position
462 if ($coordinate['latitude'] === $latitude && $coordinate['longitude'] == $longitude) {
463 //Set fill color
464 $draw->setFillColor($this->map->highFill);
465
466 //Set font size
467 $draw->setFontSize($this->map->highFontSize);
468
469 //Set stroke color
470 $draw->setStrokeColor($this->map->highStroke);
471
472 //Set circle radius
473 $radius = $this->map->highRadius;
474
475 //Set stroke width
476 $stroke = $this->map->highStrokeWidth;
477 }
478
479 //Set stroke width
480 $draw->setStrokeWidth($stroke);
481
482 //Draw circle
483 $draw->circle($destX - $radius, $destY - $radius, $destX + $radius, $destY + $radius);
484
485 //Set fill color
486 $draw->setFillColor($draw->getStrokeColor());
487
488 //Set stroke width
489 $draw->setStrokeWidth($stroke / 4);
490
491 //Get font metrics
492 #$metrics = $image->queryFontMetrics($draw, strval($id));
493
494 //Add annotation
495 $draw->annotation($destX - $radius, $destY + $stroke, strval($id));
496 }
497
498 //Draw on image
499 $image->drawImage($draw);
500
501 //Strip image exif data and properties
502 $image->stripImage();
503
504 //Add latitude
505 //XXX: not supported by imagick :'(
506 $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
507
508 //Add longitude
509 //XXX: not supported by imagick :'(
510 $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
511
512 //Add description
513 //XXX: not supported by imagick :'(
514 #$image->setImageProperty('exif:Description', $caption);
515
516 //Set progressive jpeg
517 $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
518
519 //Set compression quality
520 //TODO: ajust that
521 $image->setImageCompressionQuality(70);
522
523 //Save image
524 if (!$image->writeImage($map)) {
525 //Throw error
526 throw new \Exception(sprintf('Unable to write image "%s"', $path));
527 }
528
529 //Set mtime
530 $mtime = stat($map)['mtime'];
531 }
532
533 //Read map from cache
534 $response = new BinaryFileResponse($map);
535
536 //Set file name
537 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'multimap-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
538
539 //Set etag
540 $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height])));
541
542 //Set last modified
543 $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
544
545 //Disable robot index
546 $response->headers->set('X-Robots-Tag', 'noindex');
547
548 //Set as public
549 $response->setPublic();
550
551 //Return 304 response if not modified
552 $response->isNotModified($request);
553
554 //Return response
555 return $response;
556 }
557 }