]> Raphaël G. Git Repositories - packbundle/blob - Controller/MapController.php
Add captcha option
[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 Rapsys\PackBundle\Util\MapUtil;
15 use Rapsys\PackBundle\Util\SluggerUtil;
16
17 use Psr\Container\ContainerInterface;
18
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;
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' => $_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())
59 ]
60 ]
61 );
62 }
63
64 /**
65 * Return map image
66 *
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
76 */
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])) {
80 //Throw new exception
81 throw new NotFoundHttpException(sprintf('Unable to match map hash: %s', $hash));
82 }
83
84 //Set map
85 $map = $this->path.'/'.$this->prefix.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$width.'x'.$height.'.jpeg';
86
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();
93
94 try {
95 //Create path
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) {
100 //Throw error
101 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
102 }
103 }
104
105 //Create image instance
106 $image = new \Imagick();
107
108 //Add new image
109 $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
110
111 //Create tile instance
112 $tile = new \Imagick();
113
114 //Get tile xy
115 $centerX = $this->map->longitudeToX($longitude, $zoom);
116 $centerY = $this->map->latitudeToY($latitude, $zoom);
117
118 //Calculate start xy
119 $startX = floor(floor($centerX) - $width / MapUtil::tz);
120 $startY = floor(floor($centerY) - $height / MapUtil::tz);
121
122 //Calculate end xy
123 $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
124 $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
125
126 for($x = $startX; $x <= $endX; $x++) {
127 for($y = $startY; $y <= $endY; $y++) {
128 //Set cache path
129 $cache = $this->cache.'/'.$this->prefix.'/'.$zoom.'/'.$x.'/'.$y.'.png';
130
131 //Without cache image
132 if (!is_file($cache)) {
133 //Set tile url
134 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
135
136 //Without cache path
137 if (!is_dir($dir = dirname($cache))) {
138 //Create filesystem object
139 $filesystem = new Filesystem();
140
141 try {
142 //Create path
143 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
144 $filesystem->mkdir($dir, 0775);
145 } catch (IOExceptionInterface $e) {
146 //Throw error
147 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
148 }
149 }
150
151 //Store tile in cache
152 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
153 }
154
155 //Set dest x
156 $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
157
158 //Set dest y
159 $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
160
161 //Read tile from cache
162 $tile->readImage($cache);
163
164 //Compose image
165 $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
166
167 //Clear tile
168 $tile->clear();
169 }
170 }
171
172 //Add imagick draw instance
173 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
174 $draw = new \ImagickDraw();
175
176 //Set text antialias
177 $draw->setTextAntialias(true);
178
179 //Set stroke antialias
180 $draw->setStrokeAntialias(true);
181
182 //Set text alignment
183 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
184
185 //Set gravity
186 $draw->setGravity(\Imagick::GRAVITY_CENTER);
187
188 //Set fill color
189 $draw->setFillColor('#cff');
190
191 //Set stroke color
192 $draw->setStrokeColor('#00c3f9');
193
194 //Set stroke width
195 $draw->setStrokeWidth(2);
196
197 //Draw circle
198 $draw->circle($width/2 - 5, $height/2 - 5, $width/2 + 5, $height/2 + 5);
199
200 //Draw on image
201 $image->drawImage($draw);
202
203 //Strip image exif data and properties
204 $image->stripImage();
205
206 //Add latitude
207 //XXX: not supported by imagick :'(
208 $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
209
210 //Add longitude
211 //XXX: not supported by imagick :'(
212 $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
213
214 //Add description
215 //XXX: not supported by imagick :'(
216 #$image->setImageProperty('exif:Description', $caption);
217
218 //Set progressive jpeg
219 $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
220
221 //Set compression quality
222 //TODO: ajust that
223 $image->setImageCompressionQuality(70);
224
225 //Save image
226 if (!$image->writeImage($map)) {
227 //Throw error
228 throw new \Exception(sprintf('Unable to write image "%s"', $path));
229 }
230
231 //Set mtime
232 $mtime = stat($map)['mtime'];
233 }
234
235 //Read map from cache
236 $response = new BinaryFileResponse($map);
237
238 //Set file name
239 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'map-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
240
241 //Set etag
242 $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height])));
243
244 //Set last modified
245 $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
246
247 //Disable robot index
248 $response->headers->set('X-Robots-Tag', 'noindex');
249
250 //Set as public
251 $response->setPublic();
252
253 //Return 304 response if not modified
254 $response->isNotModified($request);
255
256 //Return response
257 return $response;
258 }
259
260 /**
261 * Return multi map image
262 *
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
273 */
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));
279 }
280
281 //Set multi
282 $map = $this->path.'/'.$this->prefix.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$coordinate.'/'.$width.'x'.$height.'.jpeg';
283
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();
290
291 try {
292 //Create path
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) {
297 //Throw error
298 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
299 }
300 }
301
302 //Create image instance
303 $image = new \Imagick();
304
305 //Add new image
306 $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
307
308 //Create tile instance
309 $tile = new \Imagick();
310
311 //Get tile xy
312 $centerX = $this->map->longitudeToX($longitude, $zoom);
313 $centerY = $this->map->latitudeToY($latitude, $zoom);
314
315 //Calculate start xy
316 $startX = floor(floor($centerX) - $width / MapUtil::tz);
317 $startY = floor(floor($centerY) - $height / MapUtil::tz);
318
319 //Calculate end xy
320 $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
321 $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
322
323 for($x = $startX; $x <= $endX; $x++) {
324 for($y = $startY; $y <= $endY; $y++) {
325 //Set cache path
326 $cache = $this->cache.'/'.$this->prefix.'/'.$zoom.'/'.$x.'/'.$y.'.png';
327
328 //Without cache image
329 if (!is_file($cache)) {
330 //Set tile url
331 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
332
333 //Without cache path
334 if (!is_dir($dir = dirname($cache))) {
335 //Create filesystem object
336 $filesystem = new Filesystem();
337
338 try {
339 //Create path
340 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
341 $filesystem->mkdir($dir, 0775);
342 } catch (IOExceptionInterface $e) {
343 //Throw error
344 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
345 }
346 }
347
348 //Store tile in cache
349 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
350 }
351
352 //Set dest x
353 $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
354
355 //Set dest y
356 $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
357
358 //Read tile from cache
359 $tile->readImage($cache);
360
361 //Compose image
362 $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
363
364 //Clear tile
365 $tile->clear();
366 }
367 }
368
369 //Add imagick draw instance
370 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
371 $draw = new \ImagickDraw();
372
373 //Set text antialias
374 $draw->setTextAntialias(true);
375
376 //Set stroke antialias
377 $draw->setStrokeAntialias(true);
378
379 //Set text alignment
380 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
381
382 //Set gravity
383 $draw->setGravity(\Imagick::GRAVITY_CENTER);
384
385 //Convert to array
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);
387
388 //Iterate on locations
389 foreach($coordinates as $id => $coordinate) {
390 //Set dest x
391 $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $this->map->longitudeToX(floatval($coordinate['longitude']), $zoom))));
392
393 //Set dest y
394 $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $this->map->latitudeToY(floatval($coordinate['latitude']), $zoom))));
395
396 //Set fill color
397 $draw->setFillColor($this->map->getFill());
398
399 //Set font size
400 $draw->setFontSize($this->map->getFontSize());
401
402 //Set stroke color
403 $draw->setStrokeColor($this->map->getStroke());
404
405 //Set circle radius
406 $radius = $this->map->getRadius();
407
408 //Set stroke width
409 $stroke = $this->map->getStrokeWidth();
410
411 //With matching position
412 if ($coordinate['latitude'] === $latitude && $coordinate['longitude'] == $longitude) {
413 //Set fill color
414 $draw->setFillColor($this->map->getHighFill());
415
416 //Set font size
417 $draw->setFontSize($this->map->getHighFontSize());
418
419 //Set stroke color
420 $draw->setStrokeColor($this->map->getHighStroke());
421
422 //Set circle radius
423 $radius = $this->map->getHighRadius();
424
425 //Set stroke width
426 $stroke = $this->map->getHighStrokeWidth();
427 }
428
429 //Set stroke width
430 $draw->setStrokeWidth($stroke);
431
432 //Draw circle
433 $draw->circle($destX - $radius, $destY - $radius, $destX + $radius, $destY + $radius);
434
435 //Set fill color
436 $draw->setFillColor($draw->getStrokeColor());
437
438 //Set stroke width
439 $draw->setStrokeWidth($stroke / 4);
440
441 //Get font metrics
442 #$metrics = $image->queryFontMetrics($draw, strval($id));
443
444 //Add annotation
445 $draw->annotation($destX - $radius, $destY + $stroke, strval($id));
446 }
447
448 //Draw on image
449 $image->drawImage($draw);
450
451 //Strip image exif data and properties
452 $image->stripImage();
453
454 //Add latitude
455 //XXX: not supported by imagick :'(
456 $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
457
458 //Add longitude
459 //XXX: not supported by imagick :'(
460 $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
461
462 //Add description
463 //XXX: not supported by imagick :'(
464 #$image->setImageProperty('exif:Description', $caption);
465
466 //Set progressive jpeg
467 $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
468
469 //Set compression quality
470 //TODO: ajust that
471 $image->setImageCompressionQuality(70);
472
473 //Save image
474 if (!$image->writeImage($map)) {
475 //Throw error
476 throw new \Exception(sprintf('Unable to write image "%s"', $path));
477 }
478
479 //Set mtime
480 $mtime = stat($map)['mtime'];
481 }
482
483 //Read map from cache
484 $response = new BinaryFileResponse($map);
485
486 //Set file name
487 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'multimap-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg');
488
489 //Set etag
490 $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height])));
491
492 //Set last modified
493 $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
494
495 //Disable robot index
496 $response->headers->set('X-Robots-Tag', 'noindex');
497
498 //Set as public
499 $response->setPublic();
500
501 //Return 304 response if not modified
502 $response->isNotModified($request);
503
504 //Return response
505 return $response;
506 }
507 }