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
; 
  33          * The captcha background color 
  35         const background 
= 'white'; 
  38          * The captcha fill color 
  43          * The captcha font size 
  48          * The captcha stroke color 
  50         const stroke 
= '#00c3f9'; 
  53          * The captcha stroke width 
  55         const strokeWidth 
= 2; 
  60         const thumbWidth 
= 640; 
  65         const thumbHeight 
= 640; 
  68          * Creates a new image util 
  70          * @param RouterInterface $router The RouterInterface instance 
  71          * @param SluggerUtil $slugger The SluggerUtil instance 
  72          * @param string $cache The cache directory 
  73          * @param string $path The public path 
  74          * @param string $prefix The prefix 
  76         function __construct(protected RouterInterface 
$router, protected SluggerUtil 
$slugger, protected string $cache = '../var/cache', protected string $path = './bundles/rapsyspack', protected string $prefix = 'image', protected string $background = self
::background
, protected string $fill = self
::fill
, protected int $fontSize = self
::fontSize
, protected string $stroke = self
::stroke
, protected int $strokeWidth = self
::strokeWidth
) { 
  82          * @param int $updated The updated timestamp 
  83          * @param int $width The width 
  84          * @param int $height The height 
  85          * @return array The captcha data 
  87         public function getCaptcha(int $updated, int $width = self
::width
, int $height = self
::height
): array { 
  98                 $equation = $a.' * '.$b.' + '.$c; 
 101                 $short = $this->slugger
->short($equation); 
 104                 $hash = $this->slugger
->serialize([$updated, $short, $width, $height]); 
 108                         'token' => $this->slugger
->hash(strval($a * $b + 
$c)), 
 109                         'value' => strval($a * $b + 
$c), 
 110                         'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation), 
 111                         'src' => $this->router
->generate('rapsyspack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]), 
 120          * @param string $caption The caption 
 121          * @param int $updated The updated timestamp 
 122          * @param string $path The path 
 123          * @param int $width The width 
 124          * @param int $height The height 
 125          * @return array The thumb data 
 127         public function getThumb(string $caption, int $updated, string $path, int $width = self
::thumbWidth
, int $height = self
::thumbHeight
): array { 
 128                 //Get image width and height 
 129                 list($imageWidth, $imageHeight) = getimagesize($path); 
 132                 $short = $this->slugger
->short($path); 
 135                 $link = $this->slugger
->serialize([$updated, $short, $imageWidth, $imageHeight]); 
 138                 $src = $this->slugger
->serialize([$updated, $short, $width, $height]); 
 142                         'caption' => $caption, 
 143                         'link' => $this->router
->generate('rapsyspack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]), 
 144                         'src' => $this->router
->generate('rapsyspack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]), 
 151          * Get captcha background color 
 153         public function getBackground() { 
 154                 return $this->background
; 
 158          * Get captcha fill color 
 160         public function getFill() { 
 165          * Get captcha font size 
 167         public function getFontSize() { 
 168                 return $this->fontSize
; 
 172          * Get captcha stroke color 
 174         public function getStroke() { 
 175                 return $this->stroke
; 
 179          * Get captcha stroke width 
 181         public function getStrokeWidth() { 
 182                 return $this->strokeWidth
; 
 188          * @param int $updated The updated timestamp 
 189          * @param string $path The path 
 190          * @return array The thumb clear success 
 192         public function remove(int $updated, string $path): bool { 
 194                 $hash = array_reverse(str_split(strval($updated))); 
 197                 $dir = $this->path
.'/'.$this->prefix
.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger
->short($path); 
 207                         //Iterate on each file 
 208                         foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) { 
 210                                 if (is_file($dir.'/'.$file)) { 
 212                                         $removes[] = $dir.'/'.$file; 
 217                 //Create filesystem object 
 218                 $filesystem = new Filesystem(); 
 222                         $filesystem->remove($removes); 
 223                 } catch (IOExceptionInterface 
$e) { 
 225                         throw new \
Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);