zend-blog-3-backend

Форк
0
77 строк · 2.1 Кб
1
<?php
2

3
namespace App\Model\Resizer;
4

5
use App\Model\ResizerInterface;
6
use Imagick;
7
use ImagickException;
8

9
class WebpResizer implements ResizerInterface
10
{
11
    use DebugAnnotation;
12

13
    /**
14
     * @throws ImagickException
15
     */
16
    public function resize(string $filePath, string $newFilePath, int $width, int $height)
17
    {
18
        if (!Imagick::queryFormats('WEBP')) {
19
            throw new \RuntimeException('Webp format is not supported by Imagick installation.');
20
        }
21

22
        $image = new Imagick($filePath);
23
        $image->stripImage();
24

25
        $image->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1);
26

27
        $image->setFormat($this->getFormat());
28
        $image->setImageFormat($this->getFormat());
29

30
        $image->setCompression(Imagick::COMPRESSION_JPEG);
31
        $image->setImageCompression(Imagick::COMPRESSION_JPEG);
32
        $image->setImageCompressionQuality(80);
33

34
        //$this->annotate($width, $height, $image);
35

36
        $image->writeImage($newFilePath);
37
        $image->clear();
38
    }
39

40
    /**
41
     * @throws ImagickException
42
     */
43
    public function convert(string $filePath, string $resourcePath): string
44
    {
45
        if (!Imagick::queryFormats('WEBP')) {
46
            throw new \RuntimeException('Webp format is not supported by Imagick installation.');
47
        }
48

49
        $pathInfo = pathinfo($filePath);
50
        $newFilePath = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.webp';
51
        if (file_exists($resourcePath . '/' . $newFilePath)) {
52
            return $newFilePath;
53
        }
54

55
        $image = new Imagick($resourcePath . '/' . $filePath);
56
        $image->stripImage();
57

58
        $image->setFormat($this->getFormat());
59
        $image->setImageFormat($this->getFormat());
60

61
        $image->setCompression(Imagick::COMPRESSION_JPEG);
62
        $image->setImageCompression(Imagick::COMPRESSION_JPEG);
63
        $image->setImageCompressionQuality(80);
64

65
        //$this->annotate(0, 0, $image);
66

67
        $image->writeImage($resourcePath . '/' . $newFilePath);
68
        $image->clear();
69

70
        return $newFilePath;
71
    }
72

73
    protected function getFormat(): string
74
    {
75
        return 'webp';
76
    }
77
}
78

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.