ImagePreviewer::createPreviewWebp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 3
b 1
f 1
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace floor12\files\logic;
5
6
use floor12\files\components\SimpleImage;
7
use floor12\files\models\File;
8
use Yii;
9
use yii\base\ErrorException;
10
11
class ImagePreviewer
12
{
13
14
    protected $model;
15
    protected $width;
16
    protected $webp;
17
    protected $fileName;
18
    protected $fileNameWebp;
19
20
    /**
21
     * ImagePreviewer constructor.
22
     * @param File $model
23
     * @param int $width
24
     * @param bool $webp
25
     * @throws ErrorException
26
     */
27
    public function __construct(File $model, int $width, $webp = false)
28
    {
29
        $this->model = $model;
30
        $this->width = $width;
31
        $this->webp = $webp;
32
33
        if (!$this->model->isImage() && !$this->model->isVideo())
34
            throw new ErrorException('File is not an image or video.');
35
    }
36
37
    /**
38
     * @return string
39
     * @throws \ErrorException
40
     * @throws \yii\base\InvalidConfigException
41
     */
42
    public function getUrl()
43
    {
44
        if ($this->model->isSvg())
45
            return $this->model->getRootPath();
46
47
        $cachePath = Yii::$app->getModule('files')->cacheFullPath;
48
        $jpegName = $this->model->makeNameWithSize($this->model->filename, $this->width);
49
        $webpName = $this->model->makeNameWithSize($this->model->filename, $this->width, true);
50
51
        $this->fileName = $cachePath . DIRECTORY_SEPARATOR . $jpegName;
0 ignored issues
show
Bug introduced by
Are you sure $cachePath of type mixed|null|object can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->fileName = /** @scrutinizer ignore-type */ $cachePath . DIRECTORY_SEPARATOR . $jpegName;
Loading history...
52
        $this->fileNameWebp = $cachePath . DIRECTORY_SEPARATOR . $webpName;
53
54
        $this->prepareFolder();
55
56
        $sourceImagePath = $this->model->rootPath;
57
        if ($this->model->isVideo()) {
58
            $sourceImagePath = $this->fileName . '.jpeg';
59
            if (!is_file($sourceImagePath))
60
                Yii::createObject(VideoFrameExtractor::class, [
61
                    $this->model->rootPath,
62
                    $sourceImagePath
63
                ])->extract();
64
        }
65
66
        if (!is_file($this->fileName) || filesize($this->fileName) == 0)
67
            $this->createPreview($sourceImagePath, $this->model->getWatermark());
68
69
        if ($this->webp && !file_exists($this->fileNameWebp))
70
            $this->createPreviewWebp();
71
72
        if ($this->webp)
73
            return $this->fileNameWebp;
74
75
        return $this->fileName;
76
    }
77
78
    /**
79
     * Generate all folders for storing image thumbnails cache.
80
     */
81
    protected function prepareFolder()
82
    {
83
        if (!file_exists(Yii::$app->getModule('files')->cacheFullPath))
0 ignored issues
show
Bug introduced by
It seems like Yii::app->getModule('files')->cacheFullPath can also be of type null and object; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        if (!file_exists(/** @scrutinizer ignore-type */ Yii::$app->getModule('files')->cacheFullPath))
Loading history...
84
            mkdir(Yii::$app->getModule('files')->cacheFullPath);
0 ignored issues
show
Bug introduced by
It seems like Yii::app->getModule('files')->cacheFullPath can also be of type null and object; however, parameter $directory of mkdir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            mkdir(/** @scrutinizer ignore-type */ Yii::$app->getModule('files')->cacheFullPath);
Loading history...
85
        $lastFolder = '/';
86
        $explodes = explode('/', $this->fileName);
87
        array_pop($explodes);
88
        if (empty($explodes))
89
            return;
90
        foreach ($explodes as $folder) {
91
            if (empty($folder))
92
                continue;
93
            $lastFolder = $lastFolder . $folder . '/';
94
            if (!file_exists($lastFolder))
95
                mkdir($lastFolder);
96
        }
97
    }
98
99
    /**
100
     * Creat JPG preview
101
     * @param $sourceImagePath
102
     * @throws ErrorException
103
     */
104
    protected function createPreview($sourceImagePath, $watermarkInPng = null)
105
    {
106
        $img = new SimpleImage();
107
        $img->load($sourceImagePath);
108
109
        if ($watermarkInPng)
110
            $img->watermark($watermarkInPng);
111
112
        $imgWidth = $img->getWidth();
113
        $imgHeight = $img->getHeight();
0 ignored issues
show
Unused Code introduced by
The assignment to $imgHeight is dead and can be removed.
Loading history...
114
115
        if ($this->width && $this->width < $imgWidth) {
116
            $ratio = $this->width / $imgWidth;
0 ignored issues
show
Unused Code introduced by
The assignment to $ratio is dead and can be removed.
Loading history...
117
            $img->resizeToWidth($this->width);
118
        }
119
120
        $saveType = $img->image_type;
121
        if ($saveType == IMG_WEBP || $saveType == IMG_QUADRATIC) {
122
            $saveType = IMG_JPEG;
123
        }
124
125
        $img->save($this->fileName, $saveType);
126
    }
127
128
129
    /**
130
     *  Create webp from default preview
131
     */
132
    protected function createPreviewWebp()
133
    {
134
        $img = new SimpleImage();
135
        $img->load($this->fileName);
136
        $img->save($this->fileNameWebp, IMAGETYPE_WEBP, 70);
137
    }
138
}
139