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() |
|
111 | |||
112 | /** |
||
113 | * After delete action |
||
114 | */ |
||
115 | public function afterDelete() |
||
119 | |||
120 | /** |
||
121 | * Get image path |
||
122 | * @return string |
||
123 | */ |
||
124 | 1 | 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 | 1 | public function getImageAbsolutePath() |
|
158 | |||
159 | /** |
||
160 | * Check team image |
||
161 | * @return bool |
||
162 | * @throws yii\base\InvalidConfigException |
||
163 | */ |
||
164 | 1 | 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 | 1 | public function getImageFile() |
|
198 | |||
199 | /** |
||
200 | * Change image by uploaded file |
||
201 | */ |
||
202 | 2 | public function imageChangeByUpload() |
|
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 | 1 | public function imageChange($sourceFile) |
|
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 | 1 | 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: