Total Complexity | 5 |
Total Lines | 34 |
Duplicated Lines | 0 % |
Coverage | 93.33% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
14 | class ImageResizer |
||
15 | { |
||
16 | private const CACHE_IMAGE_PATH = 'data/cache/images/'; |
||
17 | |||
18 | 18 | public function __construct(private readonly ImagineInterface $imagine) |
|
|
|||
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 |