]> Raphaël G. Git Repositories - packbundle/blob - Util/ImageUtil.php
c43c96d8d5998ccf845e1b8c30c8ec10635f8c4d
[packbundle] / Util / ImageUtil.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Util;
13
14 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
15 use Symfony\Component\Filesystem\Filesystem;
16 use Symfony\Component\Routing\RouterInterface;
17
18 /**
19 * Helps manage map
20 */
21 class ImageUtil {
22 /**
23 * The captcha width
24 */
25 const captchaWidth = 192;
26
27 /**
28 * The captcha height
29 */
30 const captchaHeight = 52;
31
32 /**
33 * The captcha background color
34 */
35 const captchaBackground = 'white';
36
37 /**
38 * The captcha fill color
39 */
40 const captchaFill = '#cff';
41
42 /**
43 * The captcha font size
44 */
45 const captchaFontSize = 45;
46
47 /**
48 * The captcha stroke color
49 */
50 const captchaStroke = '#00c3f9';
51
52 /**
53 * The captcha stroke width
54 */
55 const captchaStrokeWidth = 2;
56
57 /**
58 * The thumb width
59 */
60 const thumbWidth = 640;
61
62 /**
63 * The thumb height
64 */
65 const thumbHeight = 640;
66
67 /**
68 * The cache path
69 */
70 protected string $cache;
71
72 /**
73 * The path
74 */
75 protected string $path;
76
77 /**
78 * The RouterInterface instance
79 */
80 protected RouterInterface $router;
81
82 /**
83 * The SluggerUtil instance
84 */
85 protected SluggerUtil $slugger;
86
87 //Captcha background
88 protected string $captchaBackground;
89
90 //Captcha fill
91 protected string $captchaFill;
92
93 //Captcha font size
94 protected int $captchaFontSize;
95
96 //Captcha stroke
97 protected string $captchaStroke;
98
99 //Captcha stroke width
100 protected int $captchaStrokeWidth;
101
102 /**
103 * The captcha background
104 */
105 public string $captchaBackground;
106
107 /**
108 * The captcha fill
109 */
110 public string $captchaFill;
111
112 /**
113 * The captcha font size
114 */
115 public int $captchaFontSize;
116
117 /**
118 * The captcha stroke
119 */
120 public string $captchaStroke;
121
122 /**
123 * The captcha stroke width
124 */
125 public int $captchaStrokeWidth;
126
127 /**
128 * Creates a new image util
129 *
130 * @param RouterInterface $router The RouterInterface instance
131 * @param SluggerUtil $slugger The SluggerUtil instance
132 * @param string $cache The cache directory
133 * @param string $path The public path
134 * @param string $prefix The prefix
135 */
136 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) {
137 //Set cache
138 $this->cache = $cache.'/'.$prefix;
139
140 //Set captcha background
141 $this->captchaBackground = $captchaBackground;
142
143 //Set captcha fill
144 $this->captchaFill = $captchaFill;
145
146 //Set captcha font size
147 $this->captchaFontSize = $captchaFontSize;
148
149 //Set captcha stroke
150 $this->captchaStroke = $captchaStroke;
151
152 //Set captcha stroke width
153 $this->captchaStrokeWidth = $captchaStrokeWidth;
154
155 //Set path
156 $this->path = $path.'/'.$prefix;
157
158 //Set router
159 $this->router = $router;
160
161 //Set slugger
162 $this->slugger = $slugger;
163 }
164
165 /**
166 * Get captcha data
167 *
168 * @param int $updated The updated timestamp
169 * @param int $width The width
170 * @param int $height The height
171 * @return array The captcha data
172 */
173 public function getCaptcha(int $updated, int $width = self::captchaWidth, int $height = self::captchaHeight): array {
174 //Set a
175 $a = rand(0, 9);
176
177 //Set b
178 $b = rand(0, 5);
179
180 //Set c
181 $c = rand(0, 9);
182
183 //Set equation
184 $equation = $a.' * '.$b.' + '.$c;
185
186 //Short path
187 $short = $this->slugger->short($equation);
188
189 //Set hash
190 $hash = $this->slugger->serialize([$updated, $short, $width, $height]);
191
192 //Return array
193 return [
194 'token' => $this->slugger->hash(strval($a * $b + $c)),
195 'value' => strval($a * $b + $c),
196 'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation),
197 'src' => $this->router->generate('rapsys_pack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]),
198 'width' => $width,
199 'height' => $height
200 ];
201 }
202
203 /**
204 * Get thumb data
205 *
206 * @param string $caption The caption
207 * @param int $updated The updated timestamp
208 * @param string $path The path
209 * @param int $width The width
210 * @param int $height The height
211 * @return array The thumb data
212 */
213 public function getThumb(string $caption, int $updated, string $path, int $width = self::thumbWidth, int $height = self::thumbHeight): array {
214 //Get image width and height
215 list($imageWidth, $imageHeight) = getimagesize($path);
216
217 //Short path
218 $short = $this->slugger->short($path);
219
220 //Set link hash
221 $link = $this->slugger->serialize([$updated, $short, $imageWidth, $imageHeight]);
222
223 //Set src hash
224 $src = $this->slugger->serialize([$updated, $short, $width, $height]);
225
226 //Return array
227 return [
228 'caption' => $caption,
229 'link' => $this->router->generate('rapsys_pack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]),
230 'src' => $this->router->generate('rapsys_pack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]),
231 'width' => $width,
232 'height' => $height
233 ];
234 }
235
236 /**
237 * Remove image
238 *
239 * @param int $updated The updated timestamp
240 * @param string $path The path
241 * @return array The thumb clear success
242 */
243 public function remove(int $updated, string $path): bool {
244 //Set hash tree
245 $hash = array_reverse(str_split(strval($updated)));
246
247 //Set dir
248 $dir = $this->path.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger->short($path);
249
250 //Set removes
251 $removes = [];
252
253 //With dir
254 if (is_dir($dir)) {
255 //Add tree to remove
256 $removes[] = $dir;
257
258 //Iterate on each file
259 foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) {
260 //With file
261 if (is_file($dir.'/'.$file)) {
262 //Add file to remove
263 $removes[] = $dir.'/'.$file;
264 }
265 }
266 }
267
268 //Create filesystem object
269 $filesystem = new Filesystem();
270
271 try {
272 //Remove list
273 $filesystem->remove($removes);
274 } catch (IOExceptionInterface $e) {
275 //Throw error
276 throw new \Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);
277 }
278
279 //Return success
280 return true;
281 }
282 }