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          * Creates a new image util 
 105          * @param RouterInterface $router The RouterInterface instance 
 106          * @param SluggerUtil $slugger The SluggerUtil instance 
 107          * @param string $cache The cache directory 
 108          * @param string $path The public path 
 109          * @param string $prefix The prefix 
 111         function __construct(RouterInterface 
$router, SluggerUtil 
$slugger, string $cache = '../var/cache', string $path = './bundles/rapsyspack', string $prefix = 'image', $captchaBackground = self
::captchaBackground
, $captchaFill = self
::captchaFill
, $captchaFontSize = self
::captchaFontSize
, $captchaStroke = self
::captchaStroke
, $captchaStrokeWidth = self
::captchaStrokeWidth
) { 
 113                 $this->cache 
= $cache.'/'.$prefix; 
 115                 //set captcha background 
 116                 $this->captchaBackground 
= $captchaBackground; 
 119                 $this->captchaFill 
= $captchaFill; 
 121                 //set captcha font size 
 122                 $this->captchaFontSize 
= $captchaFontSize; 
 125                 $this->captchaStroke 
= $captchaStroke; 
 127                 //set captcha stroke width 
 128                 $this->captchaStrokeWidth 
= $captchaStrokeWidth; 
 131                 $this->path 
= $path.'/'.$prefix; 
 134                 $this->router 
= $router; 
 137                 $this->slugger 
= $slugger; 
 143          * @param int $updated The updated timestamp 
 144          * @param int $width The width 
 145          * @param int $height The height 
 146          * @return array The captcha data 
 148         public function getCaptcha(int $updated, int $width = self
::captchaWidth
, int $height = self
::captchaHeight
): array { 
 159                 $equation = $a.' * '.$b.' + '.$c; 
 162                 $short = $this->slugger
->short($equation); 
 165                 $hash = $this->slugger
->serialize([$updated, $short, $width, $height]); 
 169                         'token' => $this->slugger
->hash(strval($a * $b + 
$c)), 
 170                         'value' => strval($a * $b + 
$c), 
 171                         'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation), 
 172                         'src' => $this->router
->generate('rapsys_pack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]), 
 181          * @param string $caption The caption 
 182          * @param int $updated The updated timestamp 
 183          * @param string $path The path 
 184          * @param int $width The width 
 185          * @param int $height The height 
 186          * @return array The thumb data 
 188         public function getThumb(string $caption, int $updated, string $path, int $width = self
::thumbWidth
, int $height = self
::thumbHeight
): array { 
 189                 //Get image width and height 
 190                 list($imageWidth, $imageHeight) = getimagesize($path); 
 193                 $short = $this->slugger
->short($path); 
 196                 $link = $this->slugger
->serialize([$updated, $short, $imageWidth, $imageHeight]); 
 199                 $src = $this->slugger
->serialize([$updated, $short, $width, $height]); 
 203                         'caption' => $caption, 
 204                         'link' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]), 
 205                         'src' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]), 
 214          * @param int $updated The updated timestamp 
 215          * @param string $path The path 
 216          * @return array The thumb clear success 
 218         public function remove(int $updated, string $path): bool { 
 220                 $hash = array_reverse(str_split(strval($updated))); 
 223                 $dir = $this->path
.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger
->short($path); 
 233                         //Iterate on each file 
 234                         foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) { 
 236                                 if (is_file($dir.'/'.$file)) { 
 238                                         $removes[] = $dir.'/'.$file; 
 243                 //Create filesystem object 
 244                 $filesystem = new Filesystem(); 
 248                         $filesystem->remove($removes); 
 249                 } catch (IOExceptionInterface 
$e) { 
 251                         throw new \
Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);