1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mohorev\file; |
4
|
|
|
|
5
|
|
|
use Imagine\Image\ManipulatorInterface; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\base\InvalidArgumentException; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\base\NotSupportedException; |
10
|
|
|
use yii\db\BaseActiveRecord; |
11
|
|
|
use yii\helpers\ArrayHelper; |
12
|
|
|
use yii\helpers\FileHelper; |
13
|
|
|
use yii\imagine\Image; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* UploadImageBehavior automatically uploads image, creates thumbnails and fills |
17
|
|
|
* the specified attribute with a value of the name of the uploaded image. |
18
|
|
|
* |
19
|
|
|
* To use UploadImageBehavior, insert the following code to your ActiveRecord class: |
20
|
|
|
* |
21
|
|
|
* ```php |
22
|
|
|
* use mohorev\file\UploadImageBehavior; |
23
|
|
|
* |
24
|
|
|
* function behaviors() |
25
|
|
|
* { |
26
|
|
|
* return [ |
27
|
|
|
* [ |
28
|
|
|
* 'class' => UploadImageBehavior::class, |
29
|
|
|
* 'attribute' => 'file', |
30
|
|
|
* 'scenarios' => ['insert', 'update'], |
31
|
|
|
* 'placeholder' => '@app/modules/user/assets/images/userpic.jpg', |
32
|
|
|
* 'path' => '@webroot/upload/{id}/images', |
33
|
|
|
* 'url' => '@web/upload/{id}/images', |
34
|
|
|
* 'thumbPath' => '@webroot/upload/{id}/images/thumb', |
35
|
|
|
* 'thumbUrl' => '@web/upload/{id}/images/thumb', |
36
|
|
|
* 'thumbs' => [ |
37
|
|
|
* 'thumb' => ['width' => 400, 'quality' => 90], |
38
|
|
|
* 'preview' => ['width' => 200, 'height' => 200], |
39
|
|
|
* ], |
40
|
|
|
* ], |
41
|
|
|
* ]; |
42
|
|
|
* } |
43
|
|
|
* ``` |
44
|
|
|
* |
45
|
|
|
* @author Alexander Mohorev <[email protected]> |
46
|
|
|
* @author Alexey Samoylov <[email protected]> |
47
|
|
|
*/ |
48
|
|
|
class UploadImageBehavior extends UploadBehavior |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $placeholder; |
54
|
|
|
/** |
55
|
|
|
* @var boolean |
56
|
|
|
*/ |
57
|
|
|
public $createThumbsOnSave = true; |
58
|
|
|
/** |
59
|
|
|
* @var boolean |
60
|
|
|
*/ |
61
|
|
|
public $createThumbsOnRequest = false; |
62
|
|
|
/** |
63
|
|
|
* Whether delete original uploaded image after thumbs generating. |
64
|
|
|
* Defaults to FALSE |
65
|
|
|
* @var boolean |
66
|
|
|
*/ |
67
|
|
|
public $deleteOriginalFile = false; |
68
|
|
|
/** |
69
|
|
|
* @var array the thumbnail profiles |
70
|
|
|
* - `width` |
71
|
|
|
* - `height` |
72
|
|
|
* - `quality` |
73
|
|
|
*/ |
74
|
|
|
public $thumbs = [ |
75
|
|
|
'thumb' => ['width' => 200, 'height' => 200, 'quality' => 90], |
76
|
|
|
]; |
77
|
|
|
/** |
78
|
|
|
* @var string|null |
79
|
|
|
*/ |
80
|
|
|
public $thumbPath; |
81
|
|
|
/** |
82
|
|
|
* @var string|null |
83
|
|
|
*/ |
84
|
|
|
public $thumbUrl; |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
*/ |
90
|
|
|
public function init() |
91
|
|
|
{ |
92
|
|
|
if (!class_exists(Image::class)) { |
93
|
|
|
throw new NotSupportedException("Yii2-imagine extension is required to use the UploadImageBehavior"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
parent::init(); |
97
|
|
|
|
98
|
|
|
if ($this->createThumbsOnSave || $this->createThumbsOnRequest) { |
99
|
|
|
if ($this->thumbPath === null) { |
100
|
|
|
$this->thumbPath = $this->path; |
101
|
|
|
} |
102
|
|
|
if ($this->thumbUrl === null) { |
103
|
|
|
$this->thumbUrl = $this->url; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
foreach ($this->thumbs as $config) { |
107
|
|
|
$width = ArrayHelper::getValue($config, 'width'); |
108
|
|
|
$height = ArrayHelper::getValue($config, 'height'); |
109
|
|
|
if ($height < 1 && $width < 1) { |
110
|
|
|
throw new InvalidConfigException(sprintf( |
111
|
|
|
'Length of either side of thumb cannot be 0 or negative, current size ' . |
112
|
|
|
'is %sx%s', $width, $height |
113
|
|
|
)); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritdoc |
121
|
|
|
*/ |
122
|
|
|
protected function afterUpload() |
123
|
|
|
{ |
124
|
|
|
parent::afterUpload(); |
125
|
|
|
if ($this->createThumbsOnSave) { |
126
|
|
|
$this->createThumbs(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @throws \yii\base\InvalidArgumentException |
132
|
|
|
*/ |
133
|
|
|
protected function createThumbs() |
134
|
|
|
{ |
135
|
|
|
$path = $this->getUploadPath($this->attribute); |
136
|
|
|
if (!is_file($path)) { |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
foreach ($this->thumbs as $profile => $config) { |
141
|
|
|
$thumbPath = $this->getThumbUploadPath($this->attribute, $profile); |
142
|
|
|
if ($thumbPath !== null) { |
143
|
|
|
if (!FileHelper::createDirectory(dirname($thumbPath))) { |
144
|
|
|
throw new InvalidArgumentException( |
145
|
|
|
"Directory specified in 'thumbPath' attribute doesn't exist or cannot be created." |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
if (!is_file($thumbPath)) { |
149
|
|
|
$this->generateImageThumb($config, $path, $thumbPath); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($this->deleteOriginalFile) { |
155
|
|
|
parent::delete($this->attribute); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $attribute |
161
|
|
|
* @param string $profile |
162
|
|
|
* @param boolean $old |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getThumbUploadPath($attribute, $profile = 'thumb', $old = false) |
166
|
|
|
{ |
167
|
|
|
/** @var BaseActiveRecord $model */ |
168
|
|
|
$model = $this->owner; |
169
|
|
|
$path = $this->resolvePath($this->thumbPath); |
170
|
|
|
$attribute = ($old === true) ? $model->getOldAttribute($attribute) : $model->$attribute; |
171
|
|
|
$filename = $this->getThumbFileName($attribute, $profile); |
172
|
|
|
|
173
|
|
|
return $filename ? Yii::getAlias($path . '/' . $filename) : null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $attribute |
178
|
|
|
* @param string $profile |
179
|
|
|
* @return string|null |
180
|
|
|
*/ |
181
|
|
|
public function getThumbUploadUrl($attribute, $profile = 'thumb') |
182
|
|
|
{ |
183
|
|
|
/** @var BaseActiveRecord $model */ |
184
|
|
|
$model = $this->owner; |
185
|
|
|
|
186
|
|
|
if ($this->createThumbsOnRequest) { |
187
|
|
|
$this->createThumbs(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (is_file($this->getThumbUploadPath($attribute, $profile))) { |
191
|
|
|
$url = $this->resolvePath($this->thumbUrl); |
192
|
|
|
$fileName = $model->getOldAttribute($attribute); |
193
|
|
|
$thumbName = $this->getThumbFileName($fileName, $profile); |
194
|
|
|
|
195
|
|
|
return Yii::getAlias($url . '/' . $thumbName); |
196
|
|
|
} elseif ($this->placeholder) { |
197
|
|
|
return $this->getPlaceholderUrl($profile); |
198
|
|
|
} else { |
199
|
|
|
return null; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $profile |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
protected function getPlaceholderUrl($profile) |
208
|
|
|
{ |
209
|
|
|
list ($path, $url) = Yii::$app->assetManager->publish($this->placeholder); |
210
|
|
|
$filename = basename($path); |
211
|
|
|
$thumb = $this->getThumbFileName($filename, $profile); |
212
|
|
|
$thumbPath = dirname($path) . DIRECTORY_SEPARATOR . $thumb; |
213
|
|
|
$thumbUrl = dirname($url) . '/' . $thumb; |
214
|
|
|
|
215
|
|
|
if (!is_file($thumbPath)) { |
216
|
|
|
$this->generateImageThumb($this->thumbs[$profile], $path, $thumbPath); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $thumbUrl; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @inheritdoc |
224
|
|
|
*/ |
225
|
|
|
protected function delete($attribute, $old = false) |
226
|
|
|
{ |
227
|
|
|
parent::delete($attribute, $old); |
228
|
|
|
|
229
|
|
|
$profiles = array_keys($this->thumbs); |
230
|
|
|
foreach ($profiles as $profile) { |
231
|
|
|
$path = $this->getThumbUploadPath($attribute, $profile, $old); |
232
|
|
|
if (is_file($path)) { |
233
|
|
|
unlink($path); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param $filename |
240
|
|
|
* @param string $profile |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
protected function getThumbFileName($filename, $profile = 'thumb') |
244
|
|
|
{ |
245
|
|
|
return $profile . '-' . $filename; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param $config |
250
|
|
|
* @param $path |
251
|
|
|
* @param $thumbPath |
252
|
|
|
*/ |
253
|
|
|
protected function generateImageThumb($config, $path, $thumbPath) |
254
|
|
|
{ |
255
|
|
|
$width = ArrayHelper::getValue($config, 'width'); |
256
|
|
|
$height = ArrayHelper::getValue($config, 'height'); |
257
|
|
|
$quality = ArrayHelper::getValue($config, 'quality', 100); |
258
|
|
|
$mode = ArrayHelper::getValue($config, 'mode', ManipulatorInterface::THUMBNAIL_INSET); |
259
|
|
|
$bg_color = ArrayHelper::getValue($config, 'bg_color', 'FFF'); |
260
|
|
|
|
261
|
|
|
if (!$width || !$height) { |
262
|
|
|
$image = Image::getImagine()->open($path); |
263
|
|
|
$ratio = $image->getSize()->getWidth() / $image->getSize()->getHeight(); |
264
|
|
|
if ($width) { |
265
|
|
|
$height = ceil($width / $ratio); |
266
|
|
|
} else { |
267
|
|
|
$width = ceil($height * $ratio); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// Fix error "PHP GD Allowed memory size exhausted". |
272
|
|
|
ini_set('memory_limit', '512M'); |
273
|
|
|
Image::$thumbnailBackgroundColor = $bg_color; |
274
|
|
|
Image::thumbnail($path, $width, $height, $mode)->save($thumbPath, ['quality' => $quality]); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.