]> Raphaël G. Git Repositories - packbundle/blob - Controller/ImageController.php
Add image controller and util classes
[packbundle] / Controller / ImageController.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\Controller;
13
14 use Symfony\Component\HttpFoundation\HeaderUtils;
15 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
18 use Symfony\Component\Filesystem\Filesystem;
19 use Symfony\Component\HttpFoundation\BinaryFileResponse;
20 use Symfony\Component\HttpFoundation\Request;
21 use Symfony\Component\HttpFoundation\Response;
22 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23 use Symfony\Component\Routing\RequestContext;
24 use Symfony\Contracts\Service\ServiceSubscriberInterface;
25
26 use Rapsys\PackBundle\Util\ImageUtil;
27 use Rapsys\PackBundle\Util\SluggerUtil;
28
29 /**
30 * {@inheritdoc}
31 */
32 class ImageController extends AbstractController implements ServiceSubscriberInterface {
33 /**
34 * The cache path
35 */
36 protected string $cache;
37
38 /**
39 * The ContainerInterface instance
40 *
41 * @var ContainerInterface
42 */
43 protected $container;
44
45 /**
46 * The ImageUtil instance
47 */
48 protected ImageUtil $image;
49
50 /**
51 * The public path
52 */
53 protected string $public;
54
55 /**
56 * The SluggerUtil instance
57 */
58 protected SluggerUtil $slugger;
59
60 /**
61 * Creates a new image controller
62 *
63 * @param ContainerInterface $container The ContainerInterface instance
64 * @param ImageUtil $image The MapUtil instance
65 * @param SluggerUtil $slugger The SluggerUtil instance
66 * @param string $cache The cache path
67 * @param string $public The public path
68 */
69 function __construct(ContainerInterface $container, ImageUtil $image, SluggerUtil $slugger, string $cache = '../var/cache/image', string $public = './bundles/rapsyspack/image') {
70 //Set cache
71 $this->cache = $cache;
72
73 //Set container
74 $this->container = $container;
75
76 //Set image
77 $this->image = $image;
78
79 //Set public
80 $this->public = $public;
81
82 //Set slugger
83 $this->slugger = $slugger;
84 }
85
86 /**
87 * Return captcha image
88 *
89 * @param Request $request The Request instance
90 * @param string $hash The hash
91 * @param int $updated The updated timestamp
92 * @param string $equation The shorted equation
93 * @param int $width The width
94 * @param int $height The height
95 * @return Response The rendered image
96 */
97 public function captcha(Request $request, string $hash, int $updated, string $equation, int $width, int $height): Response {
98 //Without matching hash
99 if ($hash !== $this->slugger->serialize([$updated, $equation, $width, $height])) {
100 //Throw new exception
101 throw new NotFoundHttpException(sprintf('Unable to match captcha hash: %s', $hash));
102 }
103
104 //Set hashed tree
105 $hashed = array_reverse(str_split(strval($updated)));
106
107 //Set captcha
108 $captcha = $this->public.'/'.$hashed[0].'/'.$hashed[1].'/'.$hashed[2].'/'.$updated.'/'.$equation.'/'.$width.'x'.$height.'.jpeg';
109
110 //Unshort equation
111 $equation = $this->slugger->unshort($equation);
112
113 //Without captcha up to date file
114 if (!is_file($captcha) || !($mtime = stat($captcha)['mtime']) || $mtime < $updated) {
115 //Without existing captcha path
116 if (!is_dir($dir = dirname($captcha))) {
117 //Create filesystem object
118 $filesystem = new Filesystem();
119
120 try {
121 //Create path
122 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
123 //XXX: on CoW filesystems execute a chattr +C before filling
124 $filesystem->mkdir($dir, 0775);
125 } catch (IOExceptionInterface $e) {
126 //Throw error
127 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
128 }
129 }
130
131 //Create image instance
132 $image = new \Imagick();
133
134 //Add imagick draw instance
135 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
136 $draw = new \ImagickDraw();
137
138 //Set text antialias
139 $draw->setTextAntialias(true);
140
141 //Set stroke antialias
142 $draw->setStrokeAntialias(true);
143
144 //Set text alignment
145 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
146
147 //Set gravity
148 $draw->setGravity(\Imagick::GRAVITY_CENTER);
149
150 //Set fill color
151 $draw->setFillColor($this->image->captchaFill);
152
153 //Set stroke color
154 $draw->setStrokeColor($this->image->captchaStroke);
155
156 //Set font size
157 $draw->setFontSize($this->image->captchaFontSize/1.5);
158
159 //Set stroke width
160 $draw->setStrokeWidth($this->image->captchaStrokeWidth / 2);
161
162 //Set rotation
163 $draw->rotate($rotate = (rand(25, 75)*(rand(0,1)?-.1:.1)));
164
165 //Get font metrics
166 $metrics2 = $image->queryFontMetrics($draw, strval('stop spam'));
167
168 //Add annotation
169 $draw->annotation($width / 2 - ceil(rand(intval(-$metrics2['textWidth']), intval($metrics2['textWidth'])) / 2) - abs($rotate), ceil($metrics2['textHeight'] + $metrics2['descender'] + $metrics2['ascender']) / 2 - $this->image->captchaStrokeWidth - $rotate, strval('stop spam'));
170
171 //Set rotation
172 $draw->rotate(-$rotate);
173
174 //Set font size
175 $draw->setFontSize($this->image->captchaFontSize);
176
177 //Set stroke width
178 $draw->setStrokeWidth($this->image->captchaStrokeWidth);
179
180 //Set rotation
181 $draw->rotate($rotate = (rand(25, 50)*(rand(0,1)?-.1:.1)));
182
183 //Get font metrics
184 $metrics = $image->queryFontMetrics($draw, strval($equation));
185
186 //Add annotation
187 $draw->annotation($width / 2, ceil($metrics['textHeight'] + $metrics['descender'] + $metrics['ascender']) / 2 - $this->image->captchaStrokeWidth, strval($equation));
188
189 //Set rotation
190 $draw->rotate(-$rotate);
191
192 //Add new image
193 #$image->newImage(intval(ceil($metrics['textWidth'])), intval(ceil($metrics['textHeight'] + $metrics['descender'])), new \ImagickPixel($this->image->captchaBackground), 'jpeg');
194 $image->newImage($width, $height, new \ImagickPixel($this->image->captchaBackground), 'jpeg');
195
196 //Draw on image
197 $image->drawImage($draw);
198
199 //Strip image exif data and properties
200 $image->stripImage();
201
202 //Set compression quality
203 $image->setImageCompressionQuality(70);
204
205 //Save captcha
206 if (!$image->writeImage($captcha)) {
207 //Throw error
208 throw new \Exception(sprintf('Unable to write image "%s"', $captcha));
209 }
210
211 //Set mtime
212 $mtime = stat($captcha)['mtime'];
213 }
214
215 //Read captcha from cache
216 $response = new BinaryFileResponse($captcha);
217
218 //Set file name
219 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'captcha-stop-spam-'.str_replace([' ', '*', '+'], ['-', 'mul', 'add'], $equation).'-'.$width.'x'.$height.'.jpeg');
220
221 //Set etag
222 $response->setEtag(md5($hash));
223
224 //Set last modified
225 $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
226
227 //Set as public
228 $response->setPublic();
229
230 //Return 304 response if not modified
231 $response->isNotModified($request);
232
233 //Return response
234 return $response;
235 }
236
237 /**
238 * Return thumb image
239 *
240 * @param Request $request The Request instance
241 * @param string $hash The hash
242 * @param int $updated The updated timestamp
243 * @param string $path The image path
244 * @param int $width The width
245 * @param int $height The height
246 * @return Response The rendered image
247 */
248 public function thumb(Request $request, string $hash, int $updated, string $path, int $width, int $height): Response {
249 //Without matching hash
250 if ($hash !== $this->slugger->serialize([$updated, $path, $width, $height])) {
251 //Throw new exception
252 throw new NotFoundHttpException(sprintf('Unable to match thumb hash: %s', $hash));
253 }
254
255 //Set hashed tree
256 $hashed = array_reverse(str_split(strval($updated)));
257
258 //Set thumb
259 $thumb = $this->public.'/'.$hashed[0].'/'.$hashed[1].'/'.$hashed[2].'/'.$updated.'/'.$path.'/'.$width.'x'.$height.'.jpeg';
260
261 //Unshort path
262 $path = $this->slugger->unshort($path);
263
264 //Without thumb up to date file
265 if (!is_file($thumb) || !($mtime = stat($thumb)['mtime']) || $mtime < $updated) {
266 //Without existing thumb path
267 if (!is_dir($dir = dirname($thumb))) {
268 //Create filesystem object
269 $filesystem = new Filesystem();
270
271 try {
272 //Create path
273 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
274 //XXX: on CoW filesystems execute a chattr +C before filling
275 $filesystem->mkdir($dir, 0775);
276 } catch (IOExceptionInterface $e) {
277 //Throw error
278 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
279 }
280 }
281
282 //Create image instance
283 $image = new \Imagick();
284
285 //Read image
286 $image->readImage(realpath($path));
287
288 //Crop using aspect ratio
289 //XXX: for better result upload image directly in aspect ratio :)
290 $image->cropThumbnailImage($width, $height);
291
292 //Strip image exif data and properties
293 $image->stripImage();
294
295 //Set compression quality
296 //TODO: ajust that
297 $image->setImageCompressionQuality(70);
298
299 //Save thumb
300 if (!$image->writeImage($thumb)) {
301 //Throw error
302 throw new \Exception(sprintf('Unable to write image "%s"', $thumb));
303 }
304
305 //Set mtime
306 $mtime = stat($thumb)['mtime'];
307 }
308
309 //Read thumb from cache
310 $response = new BinaryFileResponse($thumb);
311
312 //Set file name
313 $response->setContentDisposition(HeaderUtils::DISPOSITION_INLINE, 'thumb-'.str_replace('/', '_', $path).'-'.$width.'x'.$height.'.jpeg');
314
315 //Set etag
316 $response->setEtag(md5($hash));
317
318 //Set last modified
319 $response->setLastModified(\DateTime::createFromFormat('U', strval($mtime)));
320
321 //Set as public
322 $response->setPublic();
323
324 //Return 304 response if not modified
325 $response->isNotModified($request);
326
327 //Return response
328 return $response;
329 }
330 }