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          * Creates a new image util 
  90          * @param RouterInterface $router The RouterInterface instance 
  91          * @param SluggerUtil $slugger The SluggerUtil instance 
  92          * @param string $cache The cache directory 
  93          * @param string $path The public path 
  94          * @param string $prefix The prefix 
  96         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
) { 
  98                 $this->cache 
= $cache.'/'.$prefix; 
 100                 //set captcha background 
 101                 $this->captchaBackground 
= $captchaBackground; 
 104                 $this->captchaFill 
= $captchaFill; 
 106                 //set captcha font size 
 107                 $this->captchaFontSize 
= $captchaFontSize; 
 110                 $this->captchaStroke 
= $captchaStroke; 
 112                 //set captcha stroke width 
 113                 $this->captchaStrokeWidth 
= $captchaStrokeWidth; 
 116                 $this->path 
= $path.'/'.$prefix; 
 119                 $this->router 
= $router; 
 122                 $this->slugger 
= $slugger; 
 128          * @param int $updated The updated timestamp 
 129          * @param int $width The width 
 130          * @param int $height The height 
 131          * @return array The captcha data 
 133         public function getCaptcha(int $updated, int $width = self
::captchaWidth
, int $height = self
::captchaHeight
): array { 
 144                 $equation = $a.' * '.$b.' + '.$c; 
 147                 $short = $this->slugger
->short($equation); 
 150                 $hash = $this->slugger
->serialize([$updated, $short, $width, $height]); 
 154                         'token' => $this->slugger
->hash(strval($a * $b + 
$c)), 
 155                         'value' => strval($a * $b + 
$c), 
 156                         'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation), 
 157                         'src' => $this->router
->generate('rapsys_pack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]), 
 166          * @param string $caption The caption 
 167          * @param int $updated The updated timestamp 
 168          * @param string $path The path 
 169          * @param int $width The width 
 170          * @param int $height The height 
 171          * @return array The thumb data 
 173         public function getThumb(string $caption, int $updated, string $path, int $width = self
::thumbWidth
, int $height = self
::thumbHeight
): array { 
 174                 //Get image width and height 
 175                 list($imageWidth, $imageHeight) = getimagesize($path); 
 178                 $short = $this->slugger
->short($path); 
 181                 $link = $this->slugger
->serialize([$updated, $short, $imageWidth, $imageHeight]); 
 184                 $src = $this->slugger
->serialize([$updated, $short, $width, $height]); 
 188                         'caption' => $caption, 
 189                         'link' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]), 
 190                         'src' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]), 
 199          * @param int $updated The updated timestamp 
 200          * @param string $path The path 
 201          * @return array The thumb clear success 
 203         public function remove(int $updated, string $path): bool { 
 205                 $hash = array_reverse(str_split(strval($updated))); 
 208                 $dir = $this->path
.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger
->short($path); 
 218                         //Iterate on each file 
 219                         foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) { 
 221                                 if (is_file($dir.'/'.$file)) { 
 223                                         $removes[] = $dir.'/'.$file; 
 228                 //Create filesystem object 
 229                 $filesystem = new Filesystem(); 
 233                         $filesystem->remove($removes); 
 234                 } catch (IOExceptionInterface 
$e) { 
 236                         throw new \
Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);