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