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