ImageController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 38
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A resizeImage() 0 26 3
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