-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageAction.php
141 lines (116 loc) · 5.17 KB
/
ImageAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Created by PhpStorm.
* User: Maksim Morozov <[email protected]>
* Date: 09.11.2020
* Time: 10:55
*/
namespace mxmorozov\thumbnail;
use Closure;
use Imagine\Image\Box;
use Yii;
use yii\base\Action;
use yii\base\Exception;
use yii\db\ActiveRecord;
use yii\helpers\FileHelper;
use yii\imagine\Image;
use yii\web\NotFoundHttpException;
class ImageAction extends Action
{
const TYPE_QUAD = 'quad';
const TYPE_WIDTH = 'width';
const TYPE_HEIGHT = 'height';
const RATIO_1X = '1x';
const RATIO_2X = '2x';
const RATIO_3X = '3x';
const RATIOS = [self::RATIO_1X => 1, self::RATIO_2X => 2, self::RATIO_3X => 3];
public string $type = self::TYPE_QUAD;
public array $sizes;
public Closure $getOriginImageFileName;
public Closure $getImageBaseName;
public ?Closure $handleImageNotFound = null;
public string $cachePath;
public string $modelClass;
private string $_sizeName;
private int $_size;
private ?string $_ratio = null;
private ?ActiveRecord $_model;
private string $_originImageFileName;
private string $_targetImagePath;
private string $_targetImageFileName;
private ?string $_targetImageFileNameWebp = null;
public function init()
{
$this->_model = $this->modelClass::findOne((int)Yii::$app->request->get('id'));
if (!($this->_model instanceof ActiveRecord)) {
throw new NotFoundHttpException();
}
$this->_sizeName = (string)Yii::$app->request->get('size');
if (array_key_exists($this->_sizeName, $this->sizes)) {
$this->_size = $this->sizes[$this->_sizeName];
} else {
throw new NotFoundHttpException();
}
if ($this->_ratio = (string)Yii::$app->request->get('ratio')) {
if (!array_key_exists($this->_ratio, self::RATIOS)) {
throw new NotFoundHttpException();
}
$this->_targetImagePath = $this->cachePath . DIRECTORY_SEPARATOR . $this->_model::tableName() . DIRECTORY_SEPARATOR . $this->_sizeName . DIRECTORY_SEPARATOR . $this->_ratio;
} else {
$this->_targetImagePath = $this->cachePath . DIRECTORY_SEPARATOR . $this->_model::tableName() . DIRECTORY_SEPARATOR . $this->_sizeName;
}
if (!is_dir($this->_targetImagePath)) {
FileHelper::createDirectory($this->_targetImagePath);
}
if ($imageBaseName = call_user_func($this->getImageBaseName, $this->_model)) {
$this->_targetImageFileName = $this->_targetImagePath . DIRECTORY_SEPARATOR . $imageBaseName;
} else {
throw new NotFoundHttpException();
}
$requestedFilename = (string)Yii::$app->request->get('filename');
if (pathinfo($requestedFilename, PATHINFO_EXTENSION) == 'webp') {
$this->_targetImageFileNameWebp = $this->_targetImagePath . DIRECTORY_SEPARATOR . $requestedFilename;
}
}
public function run()
{
if (!file_exists($this->_targetImageFileName) || ($this->_targetImageFileNameWebp && !file_exists($this->_targetImageFileNameWebp))) {
if ($this->getOriginImageFileName) {
$this->_originImageFileName = call_user_func($this->getOriginImageFileName, $this->_model);
}
if (!file_exists($this->_originImageFileName)) {
if ($this->handleImageNotFound) {
call_user_func($this->handleImageNotFound, $this->_model);
}
}
$imgsize = getimagesize($this->_originImageFileName);
$originWidth = $imgsize[0];
$originHeight = $imgsize[1];
$ratioMultiplyer = $this->_ratio ? self::RATIOS[$this->_ratio] : 1;
if ($this->type == self::TYPE_QUAD) {
$minOriginSideSize = min($originWidth, $originHeight);
$image = Image::crop($this->_originImageFileName, $minOriginSideSize, $minOriginSideSize);
$newSideSize = $this->_size * $ratioMultiplyer;
if ($newSideSize < $minOriginSideSize) {
$image->resize(new Box($newSideSize, $newSideSize));
}
} elseif ($this->type == self::TYPE_WIDTH) {
$factor = $originWidth / $this->_size;
$image = Image::resize($this->_originImageFileName, $this->_size * $ratioMultiplyer, $originHeight * $factor * $ratioMultiplyer);
} elseif ($this->type == self::TYPE_HEIGHT) {
$factor = $originHeight / $this->_size;
$image = Image::resize($this->_originImageFileName, $originWidth * $factor * $ratioMultiplyer, $this->_size * $ratioMultiplyer);
} else {
throw new Exception('unknown type');
}
$image->save($this->_targetImageFileName);
if ($this->_targetImageFileNameWebp) {
shell_exec("cwebp -q 80 {$this->_targetImageFileName} -o {$this->_targetImageFileNameWebp}");
}
}
return Yii::$app->response->sendFile($this->_targetImageFileNameWebp ?? $this->_targetImageFileName, null, [
'mimeType' => mime_content_type($this->_targetImageFileName),
'inline' => true,
]);
}
}