Completed
Push — master ( 50a99d...aba1e6 )
by Evgenii
02:53
created

ImagePreviewer::prepareFolder()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 17
rs 9.2222
cc 6
nc 10
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 floor12\files\models\FileType;
9
use Yii;
10
use yii\base\ErrorException;
11
12
class ImagePreviewer
13
{
14
15
    protected $model;
16
    protected $width;
17
    protected $webp;
18
    protected $fileName;
19
    protected $fileNameWebp;
20
21
    /**
22
     * ImagePreviewer constructor.
23
     * @param File $model
24
     * @param int $width
25
     * @param bool $webp
26
     * @throws ErrorException
27
     */
28
    public function __construct(File $model, int $width, $webp = false)
29
    {
30
        $this->model = $model;
31
        $this->width = $width;
32
        $this->webp = $webp;
33
34
        if ($this->model->type != FileType::IMAGE)
35
            throw new ErrorException('File is not an image.');
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getUrl()
42
    {
43
        if ($this->model->isSvg())
44
            return $this->model->getRootPath();
45
46
        $this->fileName = Yii::$app->getModule('files')->cacheFullPath . DIRECTORY_SEPARATOR . $this->model->makeNameWithSize($this->model->filename,
0 ignored issues
show
Bug introduced by
Are you sure Yii::app->getModule('files')->cacheFullPath 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

46
        $this->fileName = /** @scrutinizer ignore-type */ Yii::$app->getModule('files')->cacheFullPath . DIRECTORY_SEPARATOR . $this->model->makeNameWithSize($this->model->filename,
Loading history...
47
                $this->width, 0);
48
        $this->fileNameWebp = Yii::$app->getModule('files')->cacheFullPath . DIRECTORY_SEPARATOR . $this->model->makeNameWithSize($this->model->filename,
49
                $this->width, 0, true);
50
51
        $this->prepareFolder();
52
53
        if (!file_exists($this->fileName) || filesize($this->fileName) == 0)
54
            $this->createPreview();
55
56
        if ($this->webp && !file_exists($this->fileNameWebp))
57
            $this->createPreviewWebp();
58
59
        if ($this->webp)
60
            return $this->fileNameWebp;
61
62
        return $this->fileName;
63
    }
64
65
    /**
66
     * Creat JPG preview
67
     */
68
    protected function createPreview()
69
    {
70
        $img = new SimpleImage();
71
        $img->load($this->model->rootPath);
72
73
        $imgWidth = $img->getWidth();
74
        $imgHeight = $img->getHeight();
0 ignored issues
show
Unused Code introduced by
The assignment to $imgHeight is dead and can be removed.
Loading history...
75
76
        if ($this->width && $this->width < $imgWidth) {
77
            $ratio = $this->width / $imgWidth;
0 ignored issues
show
Unused Code introduced by
The assignment to $ratio is dead and can be removed.
Loading history...
78
            $img->resizeToWidth($this->width);
79
        }
80
81
        $img->save($this->fileName, $img->image_type);
82
    }
83
84
    /**
85
     *  Create webp from default preview
86
     */
87
    protected function createPreviewWebp()
88
    {
89
        $img = new SimpleImage();
90
        $img->load($this->fileName);
91
        $img->save($this->fileNameWebp, IMAGETYPE_WEBP, 70);
92
    }
93
94
    /**
95
     * Generate all folders for storing image thumbnails cache.
96
     */
97
    protected function prepareFolder()
98
    {
99
        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 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

99
        if (!file_exists(/** @scrutinizer ignore-type */ Yii::$app->getModule('files')->cacheFullPath))
Loading history...
100
            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 object; however, parameter $pathname 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

100
            mkdir(/** @scrutinizer ignore-type */ Yii::$app->getModule('files')->cacheFullPath);
Loading history...
101
        $folders = [];
102
        $lastFolder = '/';
103
        $explodes = explode('/', $this->fileName);
104
        array_pop($explodes);
105
        if (empty($explodes))
106
            return;
107
        foreach ($explodes as $folder) {
108
            if (empty($folder))
109
                continue;
110
            $lastFolder = $lastFolder . $folder . '/';
111
            if (!file_exists($lastFolder))
112
                mkdir($lastFolder);
113
            $folders[] = $lastFolder;
114
        }
115
    }
116
}