1 | <?php |
||
50 | class UploadImageBehavior extends UploadBehavior |
||
51 | { |
||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | public $placeholder; |
||
56 | /** |
||
57 | * create all thumbs profiles on image upload |
||
58 | * @var boolean |
||
59 | */ |
||
60 | public $createThumbsOnSave = true; |
||
61 | /** |
||
62 | * create thumb only for profile request by getThumbUploadUrl() method |
||
63 | * @var boolean |
||
64 | */ |
||
65 | public $createThumbsOnRequest = false; |
||
66 | /** |
||
67 | * Whether delete original uploaded image after thumbs generating. |
||
68 | * Defaults to FALSE |
||
69 | * @var boolean |
||
70 | */ |
||
71 | public $deleteOriginalFile = false; |
||
72 | /** |
||
73 | * @var array the thumbnail profiles |
||
74 | * - `width` |
||
75 | * - `height` |
||
76 | * - `quality` |
||
77 | */ |
||
78 | public $thumbs = [ |
||
79 | 'thumb' => ['width' => 200, 'height' => 200, 'quality' => 90], |
||
80 | ]; |
||
81 | /** |
||
82 | * @var string|null |
||
83 | */ |
||
84 | public $thumbPath; |
||
85 | /** |
||
86 | * @var string|null |
||
87 | */ |
||
88 | public $thumbUrl; |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | 5 | public function init() |
|
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | */ |
||
124 | 3 | protected function afterUpload() |
|
131 | |||
132 | /** |
||
133 | * @param string $needed_profile - profile name to create thumb |
||
134 | * @throws \yii\base\Exception |
||
135 | * @throws \yii\base\InvalidConfigException |
||
136 | */ |
||
137 | 2 | protected function createThumbs($needed_profile = false) |
|
163 | |||
164 | /** |
||
165 | * @param string $attribute |
||
166 | * @param string $profile |
||
167 | * @param boolean $old |
||
168 | * @return string |
||
169 | * @throws \yii\base\InvalidConfigException |
||
170 | */ |
||
171 | 2 | public function getThumbUploadPath($attribute, $profile = 'thumb', $old = false) |
|
181 | |||
182 | /** |
||
183 | * @param string $attribute |
||
184 | * @param string $profile |
||
185 | * @return string|null |
||
186 | * @throws \yii\base\Exception |
||
187 | * @throws \yii\base\InvalidConfigException |
||
188 | */ |
||
189 | 3 | public function getThumbUploadUrl($attribute, $profile = 'thumb') |
|
190 | { |
||
191 | /** @var BaseActiveRecord $model */ |
||
192 | 3 | $model = $this->owner; |
|
193 | |||
194 | 3 | if (!$model->getAttribute($attribute)) |
|
195 | { |
||
196 | 1 | if ($this->placeholder) { |
|
197 | 1 | return $this->getPlaceholderUrl($profile); |
|
198 | } |
||
199 | else { |
||
200 | return null; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | 2 | $path = $this->getUploadPath($attribute, true); |
|
205 | |||
206 | //if original file exist - generate profile thumb and generate url to thumb |
||
207 | 2 | if (is_file($path) || !$this->deleteOriginalFile) { |
|
208 | 2 | if ($this->createThumbsOnRequest) { |
|
209 | 2 | $this->createThumbs($profile); |
|
210 | } |
||
211 | 2 | return $this->getThumbProfileUrl($attribute, $profile, $model); |
|
212 | } //if original file is deleted generate url to thumb |
||
213 | elseif ($this->deleteOriginalFile) { |
||
214 | return $this->getThumbProfileUrl($attribute, $profile, $model); |
||
215 | } elseif ($this->placeholder) { |
||
216 | return $this->getPlaceholderUrl($profile); |
||
217 | } else { |
||
218 | return null; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param $profile |
||
224 | * @return string |
||
225 | */ |
||
226 | 1 | protected function getPlaceholderUrl($profile) |
|
227 | { |
||
228 | 1 | list ($path, $url) = Yii::$app->assetManager->publish($this->placeholder); |
|
229 | 1 | $filename = basename($path); |
|
230 | 1 | $thumb = $this->getThumbFileName($filename, $profile); |
|
231 | 1 | $thumbPath = dirname($path) . DIRECTORY_SEPARATOR . $thumb; |
|
232 | 1 | $thumbUrl = dirname($url) . '/' . $thumb; |
|
233 | |||
234 | 1 | if (!is_file($thumbPath)) { |
|
235 | 1 | $this->generateImageThumb($this->thumbs[$profile], $path, $thumbPath); |
|
236 | } |
||
237 | |||
238 | 1 | return $thumbUrl; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * @inheritdoc |
||
243 | */ |
||
244 | 1 | protected function delete($attribute, $old = false) |
|
253 | |||
254 | /** |
||
255 | * @param $filename |
||
256 | * @param string $profile |
||
257 | * @return string |
||
258 | */ |
||
259 | 3 | protected function getThumbFileName($filename, $profile = 'thumb') |
|
263 | |||
264 | /** |
||
265 | * @param $config |
||
266 | * @param $path |
||
267 | * @param $thumbPath |
||
268 | */ |
||
269 | 3 | protected function generateImageThumb($config, $path, $thumbPath) |
|
294 | |||
295 | /** |
||
296 | * @param $attribute |
||
297 | * @param $profile |
||
298 | * @param BaseActiveRecord $model |
||
299 | * @return bool|string |
||
300 | */ |
||
301 | 2 | protected function getThumbProfileUrl($attribute, $profile, BaseActiveRecord $model) |
|
309 | } |
||
310 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..