Failed Conditions
Pull Request — master (#11)
by Adrien
15:48 queued 12:33
created

ImageResizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 34
ccs 14
cts 15
cp 0.9333
rs 10
wmc 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix\Service;
6
7
use Ecodev\Felix\Model\Image;
8
use Imagine\Image\Box;
9
use Imagine\Image\ImagineInterface;
10
11
/**
12
 * Service to resize image's images.
13
 */
14
class ImageResizer
15
{
16
    private const CACHE_IMAGE_PATH = 'data/cache/images/';
17
18 18
    public function __construct(private readonly ImagineInterface $imagine)
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 18 at column 49
Loading history...
19
    {
20 18
    }
21
22
    /**
23
     * Resize image as JPG or WEBP and return the path to the resized version.
24
     */
25 18
    public function resize(Image $image, int $maxHeight, bool $useWebp): string
26
    {
27 18
        if ($image->getMime() === 'image/svg+xml') {
28 6
            return $image->getPath();
29
        }
30
31 12
        $maxHeight = min($maxHeight, $image->getHeight());
32
33 12
        $basename = pathinfo($image->getFilename(), PATHINFO_FILENAME);
34 12
        $extension = $useWebp ? '.webp' : '.jpg';
35 12
        $path = realpath('.') . '/' . self::CACHE_IMAGE_PATH . $basename . '-' . $maxHeight . $extension;
36
37 12
        if (file_exists($path)) {
38
            return $path;
39
        }
40
41 12
        $image = $this->imagine->open($image->getPath());
42 12
        $image->thumbnail(new Box(1_000_000, $maxHeight))->save($path);
43
44 12
        return $path;
45
    }
46
}
47