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