1 | <?php |
||
32 | class ImageBehavior extends Behavior |
||
33 | { |
||
34 | /** Just proportional resize image to fit size */ |
||
35 | const NONE = 0; |
||
36 | /** Crop image resize strategy */ |
||
37 | const CROP = 1; |
||
38 | /** Add frame image resize strategy */ |
||
39 | const FRAME = 2; |
||
40 | /** Transparent color */ |
||
41 | const COLOR_TRANSPARENT = null; |
||
42 | /** |
||
43 | * Name of attribute to store the image |
||
44 | * @var string |
||
45 | */ |
||
46 | public $imageAttribute = "image"; |
||
47 | /** |
||
48 | * Default image name |
||
49 | * @var string |
||
50 | */ |
||
51 | public $imageDefault = "image.png"; |
||
52 | /** |
||
53 | * Path to store image files. |
||
54 | * If empty will be init to /images/<ActiveRecord Class Name> |
||
55 | * @var string |
||
56 | */ |
||
57 | public $imagePath; |
||
58 | /** |
||
59 | * Size to convert image |
||
60 | * If array: [width, height]. |
||
61 | * If integer: use value for with and height. |
||
62 | * If not set: image save as is. |
||
63 | * @var integer|array |
||
64 | */ |
||
65 | public $imageSize; |
||
66 | /** |
||
67 | * Image resize strategy |
||
68 | * @var int |
||
69 | */ |
||
70 | public $imageResizeStrategy = self::NONE; |
||
71 | /** |
||
72 | * Image frame color |
||
73 | * @var |
||
74 | */ |
||
75 | public $imageFrameColor = "#FFF"; |
||
76 | /** |
||
77 | * Calculated absolute image path relative to webroot |
||
78 | * @var |
||
79 | */ |
||
80 | protected $_imageAbsolutePath; |
||
81 | |||
82 | /** |
||
83 | * @inheritdoc |
||
84 | */ |
||
85 | 3 | public function events() |
|
94 | |||
95 | /** |
||
96 | * Before update action |
||
97 | */ |
||
98 | public function beforeUpdate() |
||
103 | |||
104 | /** |
||
105 | * After save action |
||
106 | */ |
||
107 | 2 | public function afterSave() |
|
108 | { |
||
109 | 2 | $this->imageChangeByUpload(); |
|
110 | 2 | } |
|
111 | |||
112 | /** |
||
113 | * After delete action |
||
114 | */ |
||
115 | 1 | public function afterDelete() |
|
116 | { |
||
117 | 1 | $this->imageRemoveFile(); |
|
118 | 1 | } |
|
119 | |||
120 | /** |
||
121 | * Get image path |
||
122 | * @return string |
||
123 | */ |
||
124 | 3 | public function getImagePath() |
|
131 | |||
132 | /** |
||
133 | * Get default image URL |
||
134 | * @return string |
||
135 | * @throws yii\base\InvalidConfigException |
||
136 | */ |
||
137 | public function getImageDefaultUrl() |
||
141 | |||
142 | /** |
||
143 | * Get absolute team image path in filesystem |
||
144 | * @return string |
||
145 | */ |
||
146 | 3 | public function getImageAbsolutePath() |
|
158 | |||
159 | /** |
||
160 | * Check team image |
||
161 | * @return bool |
||
162 | * @throws yii\base\InvalidConfigException |
||
163 | */ |
||
164 | 3 | public function hasImage() |
|
169 | |||
170 | /** |
||
171 | * Check that image file exists |
||
172 | */ |
||
173 | public function imageFileExists() |
||
177 | |||
178 | /** |
||
179 | * Get team image URL |
||
180 | * @return string |
||
181 | * @throws yii\base\InvalidConfigException |
||
182 | */ |
||
183 | public function getImageUrl() |
||
189 | |||
190 | /** |
||
191 | * Return filename in filesystem |
||
192 | * @return string |
||
193 | */ |
||
194 | 3 | public function getImageFile() |
|
198 | |||
199 | /** |
||
200 | * Change image by uploaded file |
||
201 | */ |
||
202 | 2 | public function imageChangeByUpload() |
|
203 | { |
||
204 | 2 | $formName = $this->owner->formName(); |
|
205 | 2 | if (!empty($_POST[$formName][$this->imageAttribute]) && $_POST[$formName][$this->imageAttribute] == 'empty') { |
|
206 | // reset image |
||
207 | $this->imageReset(); |
||
208 | } else { |
||
209 | // upload new |
||
210 | 2 | if (!empty($_FILES[$formName]['tmp_name'][$this->imageAttribute])) { |
|
211 | $this->imageChange( |
||
212 | [$_FILES[$formName]['name'][$this->imageAttribute] => $_FILES[$formName]['tmp_name'][$this->imageAttribute]] |
||
213 | ); |
||
214 | } |
||
215 | } |
||
216 | 2 | } |
|
217 | |||
218 | /** |
||
219 | * Change image |
||
220 | * @param string|array $sourceFile source file. if set as array ['fileName' => 'file_in_filesystem'] |
||
221 | * @return bool true, if image was changed and old image file was deleted. false, if image not changed |
||
222 | * @throws yii\base\InvalidConfigException |
||
223 | */ |
||
224 | 3 | public function imageChange($sourceFile) |
|
225 | { |
||
226 | 3 | if (is_array($sourceFile)) { |
|
227 | $fileName = key($sourceFile); |
||
228 | $sourceFile = current($sourceFile); |
||
229 | } else { |
||
230 | 3 | $fileName = $sourceFile; |
|
231 | } |
||
232 | 3 | if (!file_exists($sourceFile)) { |
|
233 | 1 | return false; |
|
234 | } |
||
235 | 3 | $imageName = $this->imageAttribute . '_' . |
|
236 | 3 | md5(implode('-', (array)$this->owner->getPrimaryKey()) . microtime(true) . rand()) . |
|
237 | 3 | '.' . pathinfo($fileName)['extension']; |
|
238 | 3 | $destinationFile = $this->getImageAbsolutePath() . '/' . $imageName; |
|
239 | 3 | if (!copy($sourceFile, $destinationFile)) { |
|
240 | return false; |
||
241 | } |
||
242 | 3 | $this->imageRemoveFile(); |
|
243 | 3 | if (!empty($this->imageSize)) { |
|
244 | 1 | $size = $this->imageSize; |
|
245 | 1 | if (!is_array($size)) { |
|
246 | 1 | $size = [$size, $size]; |
|
247 | 1 | } |
|
248 | 1 | $newBox = new Box($size[0], $size[1]); |
|
249 | 1 | $image = (new Transformation())->thumbnail( |
|
250 | 1 | $newBox, |
|
251 | 1 | $this->imageResizeStrategy == self::CROP ? |
|
252 | 1 | ImageInterface::THUMBNAIL_OUTBOUND : ImageInterface::THUMBNAIL_INSET |
|
253 | 1 | )->apply(yii\imagine\Image::getImagine()->open($destinationFile)); |
|
254 | 1 | $currentBox = $image->getSize(); |
|
255 | 1 | if ($this->imageResizeStrategy === self::FRAME) { |
|
256 | if ($this->imageFrameColor === self::COLOR_TRANSPARENT) { |
||
257 | $frameColor = '#fff'; |
||
258 | $alpha = 100; |
||
259 | } else { |
||
260 | $frameColor = $this->imageFrameColor; |
||
261 | $alpha = 0; |
||
262 | } |
||
263 | $image = (new Transformation())->add( |
||
264 | new Canvas( |
||
265 | yii\imagine\Image::getImagine(), |
||
266 | $newBox, |
||
267 | $size[0] == $currentBox->getWidth() ? |
||
268 | new Point(0, ($size[1] - $currentBox->getHeight()) / 2) : |
||
269 | new Point(($size[0] - $currentBox->getWidth()) / 2, 0), |
||
270 | new Color($frameColor, $alpha) |
||
271 | ) |
||
272 | )->apply($image); |
||
273 | } |
||
274 | 1 | $image->save($destinationFile); |
|
275 | 1 | } |
|
276 | 3 | $this->owner->setAttribute($this->imageAttribute, $imageName); |
|
277 | 3 | $this->owner->updateAttributes([$this->imageAttribute]); |
|
278 | 3 | return true; |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * Reset image to default |
||
283 | * @throws yii\base\InvalidConfigException |
||
284 | */ |
||
285 | 1 | public function imageReset() |
|
291 | |||
292 | /** |
||
293 | * Remove current image |
||
294 | * @throws yii\base\InvalidConfigException |
||
295 | */ |
||
296 | 3 | protected function imageRemoveFile() |
|
302 | |||
303 | /** |
||
304 | * Make image URL |
||
305 | * @param string $imageFileName image filename |
||
306 | * @return string |
||
307 | * @throws yii\base\InvalidConfigException |
||
308 | */ |
||
309 | protected function _imageUrl($imageFileName) |
||
313 | |||
314 | } |
||
315 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: