]> Raphaël G. Git Repositories - packbundle/blob - Util/ImageUtil.php
Strict types
[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 * Creates a new image util
104 *
105 * @param RouterInterface $router The RouterInterface instance
106 * @param SluggerUtil $slugger The SluggerUtil instance
107 * @param string $cache The cache directory
108 * @param string $path The public path
109 * @param string $prefix The prefix
110 */
111 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) {
112 //Set cache
113 $this->cache = $cache.'/'.$prefix;
114
115 //set captcha background
116 $this->captchaBackground = $captchaBackground;
117
118 //set captcha fill
119 $this->captchaFill = $captchaFill;
120
121 //set captcha font size
122 $this->captchaFontSize = $captchaFontSize;
123
124 //set captcha stroke
125 $this->captchaStroke = $captchaStroke;
126
127 //set captcha stroke width
128 $this->captchaStrokeWidth = $captchaStrokeWidth;
129
130 //Set path
131 $this->path = $path.'/'.$prefix;
132
133 //Set router
134 $this->router = $router;
135
136 //Set slugger
137 $this->slugger = $slugger;
138 }
139
140 /**
141 * Get captcha data
142 *
143 * @param int $updated The updated timestamp
144 * @param int $width The width
145 * @param int $height The height
146 * @return array The captcha data
147 */
148 public function getCaptcha(int $updated, int $width = self::captchaWidth, int $height = self::captchaHeight): array {
149 //Set a
150 $a = rand(0, 9);
151
152 //Set b
153 $b = rand(0, 5);
154
155 //Set c
156 $c = rand(0, 9);
157
158 //Set equation
159 $equation = $a.' * '.$b.' + '.$c;
160
161 //Short path
162 $short = $this->slugger->short($equation);
163
164 //Set hash
165 $hash = $this->slugger->serialize([$updated, $short, $width, $height]);
166
167 //Return array
168 return [
169 'token' => $this->slugger->hash(strval($a * $b + $c)),
170 'value' => strval($a * $b + $c),
171 'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation),
172 'src' => $this->router->generate('rapsys_pack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]),
173 'width' => $width,
174 'height' => $height
175 ];
176 }
177
178 /**
179 * Get thumb data
180 *
181 * @param string $caption The caption
182 * @param int $updated The updated timestamp
183 * @param string $path The path
184 * @param int $width The width
185 * @param int $height The height
186 * @return array The thumb data
187 */
188 public function getThumb(string $caption, int $updated, string $path, int $width = self::thumbWidth, int $height = self::thumbHeight): array {
189 //Get image width and height
190 list($imageWidth, $imageHeight) = getimagesize($path);
191
192 //Short path
193 $short = $this->slugger->short($path);
194
195 //Set link hash
196 $link = $this->slugger->serialize([$updated, $short, $imageWidth, $imageHeight]);
197
198 //Set src hash
199 $src = $this->slugger->serialize([$updated, $short, $width, $height]);
200
201 //Return array
202 return [
203 'caption' => $caption,
204 'link' => $this->router->generate('rapsys_pack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]),
205 'src' => $this->router->generate('rapsys_pack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]),
206 'width' => $width,
207 'height' => $height
208 ];
209 }
210
211 /**
212 * Remove image
213 *
214 * @param int $updated The updated timestamp
215 * @param string $path The path
216 * @return array The thumb clear success
217 */
218 public function remove(int $updated, string $path): bool {
219 //Set hash tree
220 $hash = array_reverse(str_split(strval($updated)));
221
222 //Set dir
223 $dir = $this->path.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger->short($path);
224
225 //Set removes
226 $removes = [];
227
228 //With dir
229 if (is_dir($dir)) {
230 //Add tree to remove
231 $removes[] = $dir;
232
233 //Iterate on each file
234 foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) {
235 //With file
236 if (is_file($dir.'/'.$file)) {
237 //Add file to remove
238 $removes[] = $dir.'/'.$file;
239 }
240 }
241 }
242
243 //Create filesystem object
244 $filesystem = new Filesystem();
245
246 try {
247 //Remove list
248 $filesystem->remove($removes);
249 } catch (IOExceptionInterface $e) {
250 //Throw error
251 throw new \Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);
252 }
253
254 //Return success
255 return true;
256 }
257 }