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
|
|
|
* create all thumbs profiles on image upload |
56
|
|
|
* @var boolean |
57
|
|
|
*/ |
58
|
|
|
public $createThumbsOnSave = true; |
59
|
|
|
/** |
60
|
|
|
* create thumb only for profile request by getThumbUploadUrl() method |
61
|
|
|
* @var boolean |
62
|
|
|
*/ |
63
|
|
|
public $createThumbsOnRequest = false; |
64
|
|
|
/** |
65
|
|
|
* @var array the thumbnail profiles |
66
|
|
|
* - `width` |
67
|
|
|
* - `height` |
68
|
|
|
* - `quality` |
69
|
|
|
*/ |
70
|
|
|
public $thumbs = [ |
71
|
|
|
'thumb' => ['width' => 200, 'height' => 200, 'quality' => 90], |
72
|
|
|
]; |
73
|
|
|
/** |
74
|
|
|
* @var string|null |
75
|
|
|
*/ |
76
|
|
|
public $thumbPath; |
77
|
|
|
/** |
78
|
|
|
* @var string|null |
79
|
|
|
*/ |
80
|
|
|
public $thumbUrl; |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function init() |
87
|
|
|
{ |
88
|
|
|
if (!class_exists(Image::class)) { |
89
|
|
|
throw new NotSupportedException("Yii2-imagine extension is required to use the UploadImageBehavior"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
parent::init(); |
93
|
|
|
|
94
|
|
|
if ($this->thumbPath === null) { |
95
|
|
|
$this->thumbPath = $this->path; |
96
|
|
|
} |
97
|
|
|
if ($this->thumbUrl === null) { |
98
|
|
|
$this->thumbUrl = $this->url; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
foreach ($this->thumbs as $config) { |
102
|
|
|
$width = ArrayHelper::getValue($config, 'width'); |
103
|
|
|
$height = ArrayHelper::getValue($config, 'height'); |
104
|
|
|
if ($height < 1 && $width < 1) { |
105
|
|
|
throw new InvalidConfigException(sprintf( |
106
|
|
|
'Length of either side of thumb cannot be 0 or negative, current size ' . |
107
|
|
|
'is %sx%s', $width, $height |
108
|
|
|
)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritdoc |
115
|
|
|
*/ |
116
|
|
|
protected function afterUpload() |
117
|
|
|
{ |
118
|
|
|
parent::afterUpload(); |
119
|
|
|
if ($this->createThumbsOnSave) { |
120
|
|
|
$this->createThumbs(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $needed_profile - profile name to create thumb |
126
|
|
|
* @throws \yii\base\InvalidArgumentException |
127
|
|
|
*/ |
128
|
|
|
protected function createThumbs($needed_profile = false) |
129
|
|
|
{ |
130
|
|
|
$path = $this->getUploadPath($this->attribute); |
131
|
|
|
foreach ($this->thumbs as $profile => $config) { |
132
|
|
|
//skip profiles not needed now |
133
|
|
|
if ($needed_profile && $needed_profile != $profile) { |
|
|
|
|
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$thumbPath = $this->getThumbUploadPath($this->attribute, $profile); |
138
|
|
|
if ($thumbPath !== null) { |
139
|
|
|
if (!FileHelper::createDirectory(dirname($thumbPath))) { |
140
|
|
|
throw new InvalidArgumentException( |
141
|
|
|
"Directory specified in 'thumbPath' attribute doesn't exist or cannot be created." |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
if (!is_file($thumbPath)) { |
145
|
|
|
$this->generateImageThumb($config, $path, $thumbPath); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $attribute |
154
|
|
|
* @param string $profile |
155
|
|
|
* @param boolean $old |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getThumbUploadPath($attribute, $profile = 'thumb', $old = false) |
159
|
|
|
{ |
160
|
|
|
/** @var BaseActiveRecord $model */ |
161
|
|
|
$model = $this->owner; |
162
|
|
|
$path = $this->resolvePath($this->thumbPath); |
163
|
|
|
$attribute = ($old === true) ? $model->getOldAttribute($attribute) : $model->$attribute; |
164
|
|
|
$filename = $this->getThumbFileName($attribute, $profile); |
165
|
|
|
|
166
|
|
|
return $filename ? Yii::getAlias($path . '/' . $filename) : null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $attribute |
171
|
|
|
* @param string $profile |
172
|
|
|
* @return string|null |
173
|
|
|
*/ |
174
|
|
|
public function getThumbUploadUrl($attribute, $profile = 'thumb') |
175
|
|
|
{ |
176
|
|
|
/** @var BaseActiveRecord $model */ |
177
|
|
|
$model = $this->owner; |
178
|
|
|
$path = $this->getUploadPath($attribute, true); |
179
|
|
|
if (is_file($path)) { |
180
|
|
|
if ($this->createThumbsOnRequest) { |
181
|
|
|
$this->createThumbs($profile); |
182
|
|
|
} |
183
|
|
|
$url = $this->resolvePath($this->thumbUrl); |
184
|
|
|
$fileName = $model->getOldAttribute($attribute); |
185
|
|
|
$thumbName = $this->getThumbFileName($fileName, $profile); |
186
|
|
|
|
187
|
|
|
return Yii::getAlias($url . '/' . $thumbName); |
188
|
|
|
} elseif ($this->placeholder) { |
189
|
|
|
return $this->getPlaceholderUrl($profile); |
190
|
|
|
} else { |
191
|
|
|
return null; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param $profile |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
protected function getPlaceholderUrl($profile) |
200
|
|
|
{ |
201
|
|
|
list ($path, $url) = Yii::$app->assetManager->publish($this->placeholder); |
202
|
|
|
$filename = basename($path); |
203
|
|
|
$thumb = $this->getThumbFileName($filename, $profile); |
204
|
|
|
$thumbPath = dirname($path) . DIRECTORY_SEPARATOR . $thumb; |
205
|
|
|
$thumbUrl = dirname($url) . '/' . $thumb; |
206
|
|
|
|
207
|
|
|
if (!is_file($thumbPath)) { |
208
|
|
|
$this->generateImageThumb($this->thumbs[$profile], $path, $thumbPath); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $thumbUrl; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @inheritdoc |
216
|
|
|
*/ |
217
|
|
|
protected function delete($attribute, $old = false) |
218
|
|
|
{ |
219
|
|
|
parent::delete($attribute, $old); |
220
|
|
|
|
221
|
|
|
$profiles = array_keys($this->thumbs); |
222
|
|
|
foreach ($profiles as $profile) { |
223
|
|
|
$path = $this->getThumbUploadPath($attribute, $profile, $old); |
224
|
|
|
if (is_file($path)) { |
225
|
|
|
unlink($path); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param $filename |
232
|
|
|
* @param string $profile |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
protected function getThumbFileName($filename, $profile = 'thumb') |
236
|
|
|
{ |
237
|
|
|
return $profile . '-' . $filename; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $config |
242
|
|
|
* @param $path |
243
|
|
|
* @param $thumbPath |
244
|
|
|
*/ |
245
|
|
|
protected function generateImageThumb($config, $path, $thumbPath) |
246
|
|
|
{ |
247
|
|
|
$width = ArrayHelper::getValue($config, 'width'); |
248
|
|
|
$height = ArrayHelper::getValue($config, 'height'); |
249
|
|
|
$quality = ArrayHelper::getValue($config, 'quality', 100); |
250
|
|
|
$mode = ArrayHelper::getValue($config, 'mode', ManipulatorInterface::THUMBNAIL_INSET); |
251
|
|
|
$bg_color = ArrayHelper::getValue($config, 'bg_color', 'FFF'); |
252
|
|
|
|
253
|
|
|
if (!$width || !$height) { |
254
|
|
|
$image = Image::getImagine()->open($path); |
255
|
|
|
$ratio = $image->getSize()->getWidth() / $image->getSize()->getHeight(); |
256
|
|
|
if ($width) { |
257
|
|
|
$height = ceil($width / $ratio); |
258
|
|
|
} else { |
259
|
|
|
$width = ceil($height * $ratio); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
// Fix error "PHP GD Allowed memory size exhausted". |
264
|
|
|
ini_set('memory_limit', '512M'); |
265
|
|
|
Image::$thumbnailBackgroundColor = $bg_color; |
266
|
|
|
Image::thumbnail($path, $width, $height, $mode)->save($thumbPath, ['quality' => $quality]); |
267
|
|
|
} |
268
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: