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 $public;
78 * The RouterInterface instance
80 protected RouterInterface
$router;
83 * The SluggerUtil instance
85 protected SluggerUtil
$slugger;
88 * Creates a new map util
90 * @param RouterInterface $router The RouterInterface instance
91 * @param SluggerUtil $slugger The SluggerUtil instance
93 function __construct(RouterInterface
$router, SluggerUtil
$slugger, string $cache = '../var/cache/image', string $public = './bundles/rapsyspack/image', $captchaBackground = self
::captchaBackground
, $captchaFill = self
::captchaFill
, $captchaFontSize = self
::captchaFontSize
, $captchaStroke = self
::captchaStroke
, $captchaStrokeWidth = self
::captchaStrokeWidth
) {
95 $this->cache
= $cache;
97 //set captcha background
98 $this->captchaBackground
= $captchaBackground;
101 $this->captchaFill
= $captchaFill;
103 //set captcha font size
104 $this->captchaFontSize
= $captchaFontSize;
107 $this->captchaStroke
= $captchaStroke;
109 //set captcha stroke width
110 $this->captchaStrokeWidth
= $captchaStrokeWidth;
113 $this->public = $public;
116 $this->router
= $router;
119 $this->slugger
= $slugger;
125 * @param int $updated The updated timestamp
126 * @param int $width The width
127 * @param int $height The height
128 * @return array The captcha data
130 public function getCaptcha(int $updated, int $width = self
::captchaWidth
, int $height = self
::captchaHeight
): array {
141 $equation = $a.' * '.$b.' + '.$c;
144 $short = $this->slugger
->short($equation);
147 $hash = $this->slugger
->serialize([$updated, $short, $width, $height]);
151 'token' => $this->slugger
->hash(strval($a * $b +
$c)),
152 'value' => strval($a * $b +
$c),
153 'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation),
154 'src' => $this->router
->generate('rapsys_pack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]),
163 * @param string $caption The caption
164 * @param int $updated The updated timestamp
165 * @param string $path The path
166 * @param int $width The width
167 * @param int $height The height
168 * @return array The thumb data
170 public function getThumb(string $caption, int $updated, string $path, int $width = self
::thumbWidth
, int $height = self
::thumbHeight
): array {
171 //Get image width and height
172 list($imageWidth, $imageHeight) = getimagesize($path);
175 $short = $this->slugger
->short($path);
178 $link = $this->slugger
->serialize([$updated, $short, $imageWidth, $imageHeight]);
181 $src = $this->slugger
->serialize([$updated, $short, $width, $height]);
185 'caption' => $caption,
186 'link' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]),
187 'src' => $this->router
->generate('rapsys_pack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]),
196 * @param int $updated The updated timestamp
197 * @param string $path The path
198 * @return array The thumb clear success
200 public function remove(int $updated, string $path): bool {
202 $hash = array_reverse(str_split(strval($updated)));
205 $dir = $this->public.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger
->short($path);
215 //Iterate on each file
216 foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) {
218 if (is_file($dir.'/'.$file)) {
220 $removes[] = $dir.'/'.$file;
225 //Create filesystem object
226 $filesystem = new Filesystem();
230 $filesystem->remove($removes);
231 } catch (IOExceptionInterface
$e) {
233 throw new \
Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);