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