]> Raphaël G. Git Repositories - packbundle/blob - Util/ImageUtil.php
Php 8.x constructor style
[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 /**
88 * The captcha background
89 */
90 public string $captchaBackground;
91
92 /**
93 * The captcha fill
94 */
95 public string $captchaFill;
96
97 /**
98 * The captcha font size
99 */
100 public int $captchaFontSize;
101
102 /**
103 * The captcha stroke
104 */
105 public string $captchaStroke;
106
107 /**
108 * The captcha stroke width
109 */
110 public int $captchaStrokeWidth;
111
112 /**
113 * Creates a new image util
114 *
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
120 */
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) {
122 //Set cache
123 $this->cache = $cache.'/'.$prefix;
124
125 //Set captcha background
126 $this->captchaBackground = $captchaBackground;
127
128 //Set captcha fill
129 $this->captchaFill = $captchaFill;
130
131 //Set captcha font size
132 $this->captchaFontSize = $captchaFontSize;
133
134 //Set captcha stroke
135 $this->captchaStroke = $captchaStroke;
136
137 //Set captcha stroke width
138 $this->captchaStrokeWidth = $captchaStrokeWidth;
139
140 //Set path
141 $this->path = $path.'/'.$prefix;
142
143 //Set router
144 $this->router = $router;
145
146 //Set slugger
147 $this->slugger = $slugger;
148 }
149
150 /**
151 * Get captcha data
152 *
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
157 */
158 public function getCaptcha(int $updated, int $width = self::captchaWidth, int $height = self::captchaHeight): array {
159 //Set a
160 $a = rand(0, 9);
161
162 //Set b
163 $b = rand(0, 5);
164
165 //Set c
166 $c = rand(0, 9);
167
168 //Set equation
169 $equation = $a.' * '.$b.' + '.$c;
170
171 //Short path
172 $short = $this->slugger->short($equation);
173
174 //Set hash
175 $hash = $this->slugger->serialize([$updated, $short, $width, $height]);
176
177 //Return array
178 return [
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]),
183 'width' => $width,
184 'height' => $height
185 ];
186 }
187
188 /**
189 * Get thumb data
190 *
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
197 */
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);
201
202 //Short path
203 $short = $this->slugger->short($path);
204
205 //Set link hash
206 $link = $this->slugger->serialize([$updated, $short, $imageWidth, $imageHeight]);
207
208 //Set src hash
209 $src = $this->slugger->serialize([$updated, $short, $width, $height]);
210
211 //Return array
212 return [
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]),
216 'width' => $width,
217 'height' => $height
218 ];
219 }
220
221 /**
222 * Remove image
223 *
224 * @param int $updated The updated timestamp
225 * @param string $path The path
226 * @return array The thumb clear success
227 */
228 public function remove(int $updated, string $path): bool {
229 //Set hash tree
230 $hash = array_reverse(str_split(strval($updated)));
231
232 //Set dir
233 $dir = $this->path.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger->short($path);
234
235 //Set removes
236 $removes = [];
237
238 //With dir
239 if (is_dir($dir)) {
240 //Add tree to remove
241 $removes[] = $dir;
242
243 //Iterate on each file
244 foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) {
245 //With file
246 if (is_file($dir.'/'.$file)) {
247 //Add file to remove
248 $removes[] = $dir.'/'.$file;
249 }
250 }
251 }
252
253 //Create filesystem object
254 $filesystem = new Filesystem();
255
256 try {
257 //Remove list
258 $filesystem->remove($removes);
259 } catch (IOExceptionInterface $e) {
260 //Throw error
261 throw new \Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);
262 }
263
264 //Return success
265 return true;
266 }
267 }