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 * The captcha background
90 public string $captchaBackground;
95 public string $captchaFill;
98 * The captcha font size
100 public int $captchaFontSize;
105 public string $captchaStroke;
108 * The captcha stroke width
110 public int $captchaStrokeWidth;
113 * Creates a new image util
115 * @param RouterInterface $router The RouterInterface instance
116 * @param SluggerUtil $slugger The SluggerUtil instance
117 * @param string $cache The cache directory
118 * @param string $path The public path
119 * @param string $prefix The prefix
121 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
) {
123 $this->cache
= $cache.'/'.$prefix;
125 //Set captcha background
126 $this->captchaBackground
= $captchaBackground;
129 $this->captchaFill
= $captchaFill;
131 //Set captcha font size
132 $this->captchaFontSize
= $captchaFontSize;
135 $this->captchaStroke
= $captchaStroke;
137 //Set captcha stroke width
138 $this->captchaStrokeWidth
= $captchaStrokeWidth;
141 $this->path
= $path.'/'.$prefix;
144 $this->router
= $router;
147 $this->slugger
= $slugger;
153 * @param int $updated The updated timestamp
154 * @param int $width The width
155 * @param int $height The height
156 * @return array The captcha data
158 public function getCaptcha(int $updated, int $width = self
::captchaWidth
, int $height = self
::captchaHeight
): array {
169 $equation = $a.' * '.$b.' + '.$c;
172 $short = $this->slugger
->short($equation);
175 $hash = $this->slugger
->serialize([$updated, $short, $width, $height]);
179 'token' => $this->slugger
->hash(strval($a * $b +
$c)),
180 'value' => strval($a * $b +
$c),
181 'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation),
182 'src' => $this->router
->generate('rapsys_pack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]),
191 * @param string $caption The caption
192 * @param int $updated The updated timestamp
193 * @param string $path The path
194 * @param int $width The width
195 * @param int $height The height
196 * @return array The thumb data
198 public function getThumb(string $caption, int $updated, string $path, int $width = self
::thumbWidth
, int $height = self
::thumbHeight
): array {
199 //Get image width and height
200 list($imageWidth, $imageHeight) = getimagesize($path);
203 $short = $this->slugger
->short($path);
206 $link = $this->slugger
->serialize([$updated, $short, $imageWidth, $imageHeight]);
209 $src = $this->slugger
->serialize([$updated, $short, $width, $height]);
213 'caption' => $caption,
214 'link' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]),
215 'src' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]),
224 * @param int $updated The updated timestamp
225 * @param string $path The path
226 * @return array The thumb clear success
228 public function remove(int $updated, string $path): bool {
230 $hash = array_reverse(str_split(strval($updated)));
233 $dir = $this->path
.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger
->short($path);
243 //Iterate on each file
244 foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) {
246 if (is_file($dir.'/'.$file)) {
248 $removes[] = $dir.'/'.$file;
253 //Create filesystem object
254 $filesystem = new Filesystem();
258 $filesystem->remove($removes);
259 } catch (IOExceptionInterface
$e) {
261 throw new \
Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);