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\Util
; 
  14 use Symfony\Component\Filesystem\Exception\IOExceptionInterface
; 
  15 use Symfony\Component\Filesystem\Filesystem
; 
  16 use Symfony\Component\Routing\RouterInterface
; 
  25         const captchaWidth 
= 192; 
  30         const captchaHeight 
= 52; 
  33          * The captcha background color 
  35         const captchaBackground 
= 'white'; 
  38          * The captcha fill color 
  40         const captchaFill 
= '#cff'; 
  43          * The captcha font size 
  45         const captchaFontSize 
= 45; 
  48          * The captcha stroke color 
  50         const captchaStroke 
= '#00c3f9'; 
  53          * The captcha stroke width 
  55         const captchaStrokeWidth 
= 2; 
  60         const thumbWidth 
= 640; 
  65         const thumbHeight 
= 640; 
  70         protected string $cache; 
  75         protected string $path; 
  78          * The RouterInterface instance 
  80         protected RouterInterface 
$router; 
  83          * The SluggerUtil instance 
  85         protected SluggerUtil 
$slugger; 
  88         protected string $captchaBackground; 
  91         protected string $captchaFill; 
  94         protected int $captchaFontSize; 
  97         protected string $captchaStroke; 
  99         //Captcha stroke width 
 100         protected int $captchaStrokeWidth; 
 103          * The captcha background 
 105         public string $captchaBackground; 
 110         public string $captchaFill; 
 113          * The captcha font size 
 115         public int $captchaFontSize; 
 120         public string $captchaStroke; 
 123          * The captcha stroke width 
 125         public int $captchaStrokeWidth; 
 128          * Creates a new image util 
 130          * @param RouterInterface $router The RouterInterface instance 
 131          * @param SluggerUtil $slugger The SluggerUtil instance 
 132          * @param string $cache The cache directory 
 133          * @param string $path The public path 
 134          * @param string $prefix The prefix 
 136         function __construct(RouterInterface 
$router, SluggerUtil 
$slugger, string $cache = '../var/cache', string $path = './bundles/rapsyspack', string $prefix = 'image', string $captchaBackground = self
::captchaBackground
, string $captchaFill = self
::captchaFill
, int $captchaFontSize = self
::captchaFontSize
, string $captchaStroke = self
::captchaStroke
, int $captchaStrokeWidth = self
::captchaStrokeWidth
) { 
 138                 $this->cache 
= $cache.'/'.$prefix; 
 140                 //Set captcha background 
 141                 $this->captchaBackground 
= $captchaBackground; 
 144                 $this->captchaFill 
= $captchaFill; 
 146                 //Set captcha font size 
 147                 $this->captchaFontSize 
= $captchaFontSize; 
 150                 $this->captchaStroke 
= $captchaStroke; 
 152                 //Set captcha stroke width 
 153                 $this->captchaStrokeWidth 
= $captchaStrokeWidth; 
 156                 $this->path 
= $path.'/'.$prefix; 
 159                 $this->router 
= $router; 
 162                 $this->slugger 
= $slugger; 
 168          * @param int $updated The updated timestamp 
 169          * @param int $width The width 
 170          * @param int $height The height 
 171          * @return array The captcha data 
 173         public function getCaptcha(int $updated, int $width = self
::captchaWidth
, int $height = self
::captchaHeight
): array { 
 184                 $equation = $a.' * '.$b.' + '.$c; 
 187                 $short = $this->slugger
->short($equation); 
 190                 $hash = $this->slugger
->serialize([$updated, $short, $width, $height]); 
 194                         'token' => $this->slugger
->hash(strval($a * $b + 
$c)), 
 195                         'value' => strval($a * $b + 
$c), 
 196                         'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation), 
 197                         'src' => $this->router
->generate('rapsys_pack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]), 
 206          * @param string $caption The caption 
 207          * @param int $updated The updated timestamp 
 208          * @param string $path The path 
 209          * @param int $width The width 
 210          * @param int $height The height 
 211          * @return array The thumb data 
 213         public function getThumb(string $caption, int $updated, string $path, int $width = self
::thumbWidth
, int $height = self
::thumbHeight
): array { 
 214                 //Get image width and height 
 215                 list($imageWidth, $imageHeight) = getimagesize($path); 
 218                 $short = $this->slugger
->short($path); 
 221                 $link = $this->slugger
->serialize([$updated, $short, $imageWidth, $imageHeight]); 
 224                 $src = $this->slugger
->serialize([$updated, $short, $width, $height]); 
 228                         'caption' => $caption, 
 229                         'link' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]), 
 230                         'src' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]), 
 239          * @param int $updated The updated timestamp 
 240          * @param string $path The path 
 241          * @return array The thumb clear success 
 243         public function remove(int $updated, string $path): bool { 
 245                 $hash = array_reverse(str_split(strval($updated))); 
 248                 $dir = $this->path
.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger
->short($path); 
 258                         //Iterate on each file 
 259                         foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) { 
 261                                 if (is_file($dir.'/'.$file)) { 
 263                                         $removes[] = $dir.'/'.$file; 
 268                 //Create filesystem object 
 269                 $filesystem = new Filesystem(); 
 273                         $filesystem->remove($removes); 
 274                 } catch (IOExceptionInterface 
$e) { 
 276                         throw new \
Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);