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; |
|
|
|
|
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)) |
|
|
|
|
84
|
|
|
mkdir(Yii::$app->getModule('files')->cacheFullPath); |
|
|
|
|
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(); |
|
|
|
|
114
|
|
|
|
115
|
|
|
if ($this->width && $this->width < $imgWidth) { |
116
|
|
|
$ratio = $this->width / $imgWidth; |
|
|
|
|
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
|
|
|
|