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 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
; 
  26 use Rapsys\PackBundle\Util\MapUtil
; 
  27 use Rapsys\PackBundle\Util\SluggerUtil
; 
  32 class CaptchaController 
extends AbstractController 
implements ServiceSubscriberInterface 
{ 
  36         protected string $cache; 
  39          * The ContainerInterface instance 
  41          * @var ContainerInterface  
  46          * The stream context instance 
  51          * The MapUtil instance 
  53         protected MapUtil 
$map; 
  58         protected string $path; 
  61          * The SluggerUtil instance 
  63         protected SluggerUtil 
$slugger; 
  68         protected string $url; 
  71          * Creates a new captcha controller 
  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 
  79         function __construct(ContainerInterface 
$container, SluggerUtil 
$slugger, string $cache = '../var/cache', string $path = './bundles/rapsyspack', string $prefix = 'captcha') { 
  81                 $this->cache 
= $cache.'/'.$prefix; 
  84                 $this->container 
= $container; 
  87                 $this->path 
= $path.'/'.$prefix; 
  90                 $this->slugger 
= $slugger; 
  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 
 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)); 
 114                 $map = $this->path
.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$width.'x'.$height.'.jpeg'; 
 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(); 
 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) { 
 130                                         throw new \
Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e); 
 134                         //Create image instance 
 135                         $image = new \
Imagick(); 
 138                         $image->newImage($width, $height, new \
ImagickPixel('transparent'), 'jpeg'); 
 140                         //Create tile instance 
 141                         $tile = new \
Imagick(); 
 144                         $centerX = $this->map
->longitudeToX($longitude, $zoom); 
 145                         $centerY = $this->map
->latitudeToY($latitude, $zoom); 
 148                         $startX = floor(floor($centerX) - $width / MapUtil
::tz
); 
 149                         $startY = floor(floor($centerY) - $height / MapUtil
::tz
); 
 152                         $endX = ceil(ceil($centerX) + 
$width / MapUtil
::tz
); 
 153                         $endY = ceil(ceil($centerY) + 
$height / MapUtil
::tz
); 
 155                         for($x = $startX; $x <= $endX; $x++
) { 
 156                                 for($y = $startY; $y <= $endY; $y++
) { 
 158                                         $cache = $this->cache
.'/'.$zoom.'/'.$x.'/'.$y.'.png'; 
 160                                         //Without cache image 
 161                                         if (!is_file($cache)) { 
 163                                                 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url
); 
 166                                                 if (!is_dir($dir = dirname($cache))) { 
 167                                                         //Create filesystem object 
 168                                                         $filesystem = new Filesystem(); 
 172                                                                 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755) 
 173                                                                 $filesystem->mkdir($dir, 0775); 
 174                                                         } catch (IOExceptionInterface 
$e) { 
 176                                                                 throw new \
Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e); 
 180                                                 //Store tile in cache 
 181                                                 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx
)); 
 185                                         $destX = intval(floor($width / 2 - MapUtil
::tz 
* ($centerX - $x))); 
 188                                         $destY = intval(floor($height / 2 - MapUtil
::tz 
* ($centerY - $y))); 
 190                                         //Read tile from cache 
 191                                         $tile->readImage($cache); 
 194                                         $image->compositeImage($tile, \Imagick
::COMPOSITE_OVER
, $destX, $destY); 
 201                         //Add imagick draw instance 
 202                         //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916 
 203                         $draw = new \
ImagickDraw(); 
 206                         $draw->setTextAntialias(true); 
 208                         //Set stroke antialias 
 209                         $draw->setStrokeAntialias(true); 
 212                         $draw->setTextAlignment(\Imagick
::ALIGN_CENTER
); 
 215                         $draw->setGravity(\Imagick
::GRAVITY_CENTER
); 
 218                         $draw->setFillColor('#cff'); 
 221                         $draw->setStrokeColor('#00c3f9'); 
 224                         $draw->setStrokeWidth(2); 
 227                         $draw->circle($width/2 - 5, $height/2 - 5, $width/2 + 
5, $height/2 + 
5); 
 230                         $image->drawImage($draw); 
 232                         //Strip image exif data and properties 
 233                         $image->stripImage(); 
 236                         //XXX: not supported by imagick :'( 
 237                         $image->setImageProperty('exif:GPSLatitude', $this->map
->latitudeToSexagesimal($latitude)); 
 240                         //XXX: not supported by imagick :'( 
 241                         $image->setImageProperty('exif:GPSLongitude', $this->map
->longitudeToSexagesimal($longitude)); 
 244                         //XXX: not supported by imagick :'( 
 245                         #$image->setImageProperty('exif:Description', $caption); 
 247                         //Set progressive jpeg 
 248                         $image->setInterlaceScheme(\Imagick
::INTERLACE_PLANE
); 
 250                         //Set compression quality 
 252                         $image->setImageCompressionQuality(70); 
 255                         if (!$image->writeImage($map)) { 
 257                                 throw new \
Exception(sprintf('Unable to write image "%s"', $path)); 
 261                         $mtime = stat($map)['mtime']; 
 264                 //Read map from cache 
 265                 $response = new BinaryFileResponse($map); 
 268                 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, 'map-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg'); 
 271                 $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height]))); 
 274                 $response->setLastModified(\DateTime
::createFromFormat('U', strval($mtime))); 
 277                 $response->setPublic(); 
 279                 //Return 304 response if not modified 
 280                 $response->isNotModified($request); 
 287          * Return multi map image 
 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 
 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)); 
 308                 $map = $this->path
.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$coordinate.'/'.$width.'x'.$height.'.jpeg'; 
 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(); 
 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) { 
 324                                         throw new \
Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e); 
 328                         //Create image instance 
 329                         $image = new \
Imagick(); 
 332                         $image->newImage($width, $height, new \
ImagickPixel('transparent'), 'jpeg'); 
 334                         //Create tile instance 
 335                         $tile = new \
Imagick(); 
 338                         $centerX = $this->map
->longitudeToX($longitude, $zoom); 
 339                         $centerY = $this->map
->latitudeToY($latitude, $zoom); 
 342                         $startX = floor(floor($centerX) - $width / MapUtil
::tz
); 
 343                         $startY = floor(floor($centerY) - $height / MapUtil
::tz
); 
 346                         $endX = ceil(ceil($centerX) + 
$width / MapUtil
::tz
); 
 347                         $endY = ceil(ceil($centerY) + 
$height / MapUtil
::tz
); 
 349                         for($x = $startX; $x <= $endX; $x++
) { 
 350                                 for($y = $startY; $y <= $endY; $y++
) { 
 352                                         $cache = $this->cache
.'/'.$zoom.'/'.$x.'/'.$y.'.png'; 
 354                                         //Without cache image 
 355                                         if (!is_file($cache)) { 
 357                                                 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url
); 
 360                                                 if (!is_dir($dir = dirname($cache))) { 
 361                                                         //Create filesystem object 
 362                                                         $filesystem = new Filesystem(); 
 366                                                                 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755) 
 367                                                                 $filesystem->mkdir($dir, 0775); 
 368                                                         } catch (IOExceptionInterface 
$e) { 
 370                                                                 throw new \
Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e); 
 374                                                 //Store tile in cache 
 375                                                 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx
)); 
 379                                         $destX = intval(floor($width / 2 - MapUtil
::tz 
* ($centerX - $x))); 
 382                                         $destY = intval(floor($height / 2 - MapUtil
::tz 
* ($centerY - $y))); 
 384                                         //Read tile from cache 
 385                                         $tile->readImage($cache); 
 388                                         $image->compositeImage($tile, \Imagick
::COMPOSITE_OVER
, $destX, $destY); 
 395                         //Add imagick draw instance 
 396                         //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916 
 397                         $draw = new \
ImagickDraw(); 
 400                         $draw->setTextAntialias(true); 
 402                         //Set stroke antialias 
 403                         $draw->setStrokeAntialias(true); 
 406                         $draw->setTextAlignment(\Imagick
::ALIGN_CENTER
); 
 409                         $draw->setGravity(\Imagick
::GRAVITY_CENTER
); 
 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); 
 414                         //Iterate on locations 
 415                         foreach($coordinates as $id => $coordinate) { 
 417                                 $destX = intval(floor($width / 2 - MapUtil
::tz 
* ($centerX - $this->map
->longitudeToX(floatval($coordinate['longitude']), $zoom)))); 
 420                                 $destY = intval(floor($height / 2 - MapUtil
::tz 
* ($centerY - $this->map
->latitudeToY(floatval($coordinate['latitude']), $zoom)))); 
 423                                 $draw->setFillColor($this->map
->fill
); 
 426                                 $draw->setFontSize($this->map
->fontSize
); 
 429                                 $draw->setStrokeColor($this->map
->stroke
); 
 432                                 $radius = $this->map
->radius
; 
 435                                 $stroke = $this->map
->strokeWidth
; 
 437                                 //With matching position 
 438                                 if ($coordinate['latitude'] === $latitude && $coordinate['longitude'] == $longitude) { 
 440                                         $draw->setFillColor($this->map
->highFill
); 
 443                                         $draw->setFontSize($this->map
->highFontSize
); 
 446                                         $draw->setStrokeColor($this->map
->highStroke
); 
 449                                         $radius = $this->map
->highRadius
; 
 452                                         $stroke = $this->map
->highStrokeWidth
; 
 456                                 $draw->setStrokeWidth($stroke); 
 459                                 $draw->circle($destX - $radius, $destY - $radius, $destX + 
$radius, $destY + 
$radius); 
 462                                 $draw->setFillColor($draw->getStrokeColor()); 
 465                                 $draw->setStrokeWidth($stroke / 4); 
 468                                 $metrics = $image->queryFontMetrics($draw, strval($id)); 
 471                                 $draw->annotation($destX - $radius, $destY + 
$stroke, strval($id)); 
 475                         $image->drawImage($draw); 
 477                         //Strip image exif data and properties 
 478                         $image->stripImage(); 
 481                         //XXX: not supported by imagick :'( 
 482                         $image->setImageProperty('exif:GPSLatitude', $this->map
->latitudeToSexagesimal($latitude)); 
 485                         //XXX: not supported by imagick :'( 
 486                         $image->setImageProperty('exif:GPSLongitude', $this->map
->longitudeToSexagesimal($longitude)); 
 489                         //XXX: not supported by imagick :'( 
 490                         #$image->setImageProperty('exif:Description', $caption); 
 492                         //Set progressive jpeg 
 493                         $image->setInterlaceScheme(\Imagick
::INTERLACE_PLANE
); 
 495                         //Set compression quality 
 497                         $image->setImageCompressionQuality(70); 
 500                         if (!$image->writeImage($map)) { 
 502                                 throw new \
Exception(sprintf('Unable to write image "%s"', $path)); 
 506                         $mtime = stat($map)['mtime']; 
 509                 //Read map from cache 
 510                 $response = new BinaryFileResponse($map); 
 513                 $response->setContentDisposition(HeaderUtils
::DISPOSITION_INLINE
, 'multimap-'.$latitude.','.$longitude.'-'.$zoom.'-'.$width.'x'.$height.'.jpeg'); 
 516                 $response->setEtag(md5(serialize([$updated, $latitude, $longitude, $zoom, $width, $height]))); 
 519                 $response->setLastModified(\DateTime
::createFromFormat('U', strval($mtime))); 
 522                 $response->setPublic(); 
 524                 //Return 304 response if not modified 
 525                 $response->isNotModified($request);