1 | <?php |
||
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() |
||
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) |
||
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) |
||
168 | |||
169 | /** |
||
170 | * @param string $attribute |
||
171 | * @param string $profile |
||
172 | * @return string|null |
||
173 | */ |
||
174 | public function getThumbUploadUrl($attribute, $profile = 'thumb') |
||
194 | |||
195 | /** |
||
196 | * @param $profile |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function getPlaceholderUrl($profile) |
||
213 | |||
214 | /** |
||
215 | * @inheritdoc |
||
216 | */ |
||
217 | protected function delete($attribute, $old = false) |
||
229 | |||
230 | /** |
||
231 | * @param $filename |
||
232 | * @param string $profile |
||
233 | * @return string |
||
234 | */ |
||
235 | protected function getThumbFileName($filename, $profile = 'thumb') |
||
239 | |||
240 | /** |
||
241 | * @param $config |
||
242 | * @param $path |
||
243 | * @param $thumbPath |
||
244 | */ |
||
245 | protected function generateImageThumb($config, $path, $thumbPath) |
||
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: