1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Images\Controller; |
4
|
|
|
|
5
|
|
|
use App\Controller\BaseController; |
6
|
|
|
use Gumlet\ImageResize; |
7
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
10
|
|
|
|
11
|
|
|
class ImageController extends BaseController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @Route("/f/{image}", methods={"GET"}, name="resizeImage", requirements={"image"=".+"}) |
15
|
|
|
* |
16
|
|
|
* @param string $image |
17
|
|
|
* |
18
|
|
|
* @throws \Gumlet\ImageResizeException |
19
|
|
|
* |
20
|
|
|
* @return BinaryFileResponse|Response |
21
|
|
|
*/ |
22
|
|
|
public function resizeImage(string $image) |
23
|
|
|
{ |
24
|
|
|
$pathToFile = __DIR__.'/../../../public/f/'.$image; |
25
|
|
|
$imagePathParts = explode('/', $image); |
26
|
|
|
$filename = end($imagePathParts); |
27
|
|
|
$filenameParts = explode('.', $filename); |
28
|
|
|
$ext = end($filenameParts); |
29
|
|
|
|
30
|
|
|
if (\in_array($ext, ['jpg'], true) === false) { |
31
|
|
|
return new Response('Image not found (404)', 404); |
32
|
|
|
} |
33
|
|
|
$size = $filenameParts[1]; // string '64x64' |
34
|
|
|
$originalFile = str_replace(".$size", '', $pathToFile); |
35
|
|
|
|
36
|
|
|
if (file_exists($originalFile) === false) { |
37
|
|
|
return new Response('Image not found (404)', 404); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// resize image and then return |
41
|
|
|
list($width, $height) = explode('x', $size); |
42
|
|
|
$resizer = new ImageResize($originalFile); |
43
|
|
|
$resizer->crop($width, $height); |
44
|
|
|
$resizer->save($pathToFile); |
45
|
|
|
|
46
|
|
|
return new BinaryFileResponse($pathToFile); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|