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