]> Raphaël G. Git Repositories - packbundle/blob - Util/ImageUtil.php
Add class comment
[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 * Manages image
20 */
21 class ImageUtil {
22 /**
23 * The captcha width
24 */
25 const width = 192;
26
27 /**
28 * The captcha height
29 */
30 const height = 52;
31
32 /**
33 * The captcha background color
34 */
35 const background = 'white';
36
37 /**
38 * The captcha fill color
39 */
40 const fill = '#cff';
41
42 /**
43 * The captcha font size
44 */
45 const fontSize = 45;
46
47 /**
48 * The captcha stroke color
49 */
50 const stroke = '#00c3f9';
51
52 /**
53 * The captcha stroke width
54 */
55 const strokeWidth = 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 * Creates a new image util
69 *
70 * @param RouterInterface $router The RouterInterface instance
71 * @param SluggerUtil $slugger The SluggerUtil instance
72 * @param string $cache The cache directory
73 * @param string $path The public path
74 * @param string $prefix The prefix
75 */
76 function __construct(protected RouterInterface $router, protected SluggerUtil $slugger, protected string $cache = '../var/cache', protected string $path = './bundles/rapsyspack', protected string $prefix = 'image', protected string $background = self::background, protected string $fill = self::fill, protected int $fontSize = self::fontSize, protected string $stroke = self::stroke, protected int $strokeWidth = self::strokeWidth) {
77 }
78
79 /**
80 * Get captcha data
81 *
82 * @param int $updated The updated timestamp
83 * @param int $width The width
84 * @param int $height The height
85 * @return array The captcha data
86 */
87 public function getCaptcha(int $updated, int $width = self::width, int $height = self::height): array {
88 //Set a
89 $a = rand(0, 9);
90
91 //Set b
92 $b = rand(0, 5);
93
94 //Set c
95 $c = rand(0, 9);
96
97 //Set equation
98 $equation = $a.' * '.$b.' + '.$c;
99
100 //Short path
101 $short = $this->slugger->short($equation);
102
103 //Set hash
104 $hash = $this->slugger->serialize([$updated, $short, $width, $height]);
105
106 //Return array
107 return [
108 'token' => $this->slugger->hash(strval($a * $b + $c)),
109 'value' => strval($a * $b + $c),
110 'equation' => str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation),
111 'src' => $this->router->generate('rapsys_pack_captcha', ['hash' => $hash, 'updated' => $updated, 'equation' => $short, 'width' => $width, 'height' => $height]),
112 'width' => $width,
113 'height' => $height
114 ];
115 }
116
117 /**
118 * Get thumb data
119 *
120 * @param string $caption The caption
121 * @param int $updated The updated timestamp
122 * @param string $path The path
123 * @param int $width The width
124 * @param int $height The height
125 * @return array The thumb data
126 */
127 public function getThumb(string $caption, int $updated, string $path, int $width = self::thumbWidth, int $height = self::thumbHeight): array {
128 //Get image width and height
129 list($imageWidth, $imageHeight) = getimagesize($path);
130
131 //Short path
132 $short = $this->slugger->short($path);
133
134 //Set link hash
135 $link = $this->slugger->serialize([$updated, $short, $imageWidth, $imageHeight]);
136
137 //Set src hash
138 $src = $this->slugger->serialize([$updated, $short, $width, $height]);
139
140 //Return array
141 return [
142 'caption' => $caption,
143 'link' => $this->router->generate('rapsys_pack_thumb', ['hash' => $link, 'updated' => $updated, 'path' => $short, 'width' => $imageWidth, 'height' => $imageHeight]),
144 'src' => $this->router->generate('rapsys_pack_thumb', ['hash' => $src, 'updated' => $updated, 'path' => $short, 'width' => $width, 'height' => $height]),
145 'width' => $width,
146 'height' => $height
147 ];
148 }
149
150 /**
151 * Get captcha background color
152 */
153 public function getBackground() {
154 return $this->background;
155 }
156
157 /**
158 * Get captcha fill color
159 */
160 public function getFill() {
161 return $this->fill;
162 }
163
164 /**
165 * Get captcha font size
166 */
167 public function getFontSize() {
168 return $this->fontSize;
169 }
170
171 /**
172 * Get captcha stroke color
173 */
174 public function getStroke() {
175 return $this->stroke;
176 }
177
178 /**
179 * Get captcha stroke width
180 */
181 public function getStrokeWidth() {
182 return $this->strokeWidth;
183 }
184
185 /**
186 * Remove image
187 *
188 * @param int $updated The updated timestamp
189 * @param string $path The path
190 * @return array The thumb clear success
191 */
192 public function remove(int $updated, string $path): bool {
193 //Set hash tree
194 $hash = array_reverse(str_split(strval($updated)));
195
196 //Set dir
197 $dir = $this->path.'/'.$this->prefix.'/'.$hash[0].'/'.$hash[1].'/'.$hash[2].'/'.$updated.'/'.$this->slugger->short($path);
198
199 //Set removes
200 $removes = [];
201
202 //With dir
203 if (is_dir($dir)) {
204 //Add tree to remove
205 $removes[] = $dir;
206
207 //Iterate on each file
208 foreach(array_merge($removes, array_diff(scandir($dir), ['..', '.'])) as $file) {
209 //With file
210 if (is_file($dir.'/'.$file)) {
211 //Add file to remove
212 $removes[] = $dir.'/'.$file;
213 }
214 }
215 }
216
217 //Create filesystem object
218 $filesystem = new Filesystem();
219
220 try {
221 //Remove list
222 $filesystem->remove($removes);
223 } catch (IOExceptionInterface $e) {
224 //Throw error
225 throw new \Exception(sprintf('Unable to delete thumb directory "%s"', $dir), 0, $e);
226 }
227
228 //Return success
229 return true;
230 }
231 }