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, |
|
|
|
|
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(); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if ($this->width && $this->width < $imgWidth) { |
77
|
|
|
$ratio = $this->width / $imgWidth; |
|
|
|
|
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)) |
|
|
|
|
100
|
|
|
mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
|
|
|
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
|
|
|
} |