1 | <?php |
||
27 | class ImageBehavior extends Behavior |
||
28 | { |
||
29 | /** Just proportional resize image to fit size */ |
||
30 | const NONE = 0; |
||
31 | /** Crop image resize strategy */ |
||
32 | const CROP = 1; |
||
33 | /** Add frame image resize strategy */ |
||
34 | const FRAME = 2; |
||
35 | /** Transparent color */ |
||
36 | const COLOR_TRANSPARENT = null; |
||
37 | /** |
||
38 | * Name of attribute to store the image |
||
39 | * @var string |
||
40 | */ |
||
41 | public $imageAttribute = "image"; |
||
42 | /** |
||
43 | * Default image name |
||
44 | * @var string |
||
45 | */ |
||
46 | public $imageDefault = "image.png"; |
||
47 | /** |
||
48 | * Path to store image files. |
||
49 | * If empty will be init to /images/<ActiveRecord Class Name> |
||
50 | * @var string |
||
51 | */ |
||
52 | public $imagePath; |
||
53 | /** |
||
54 | * Size to convert image |
||
55 | * If array: [width, height]. |
||
56 | * If integer: use value for with and height. |
||
57 | * If not set: image save as is. |
||
58 | * @var integer|array |
||
59 | */ |
||
60 | public $imageSize; |
||
61 | /** |
||
62 | * Image resize strategy |
||
63 | * @var int |
||
64 | */ |
||
65 | public $imageResizeStrategy = self::NONE; |
||
66 | /** |
||
67 | * Image frame color |
||
68 | * @var |
||
69 | */ |
||
70 | public $imageFrameColor = "#FFF"; |
||
71 | /** |
||
72 | * Calculated absolute image path relative to webroot |
||
73 | * @var |
||
74 | */ |
||
75 | protected $_imageAbsolutePath; |
||
76 | |||
77 | /** |
||
78 | * @inheritdoc |
||
79 | */ |
||
80 | 3 | public function events() |
|
89 | |||
90 | /** |
||
91 | * Before update action |
||
92 | */ |
||
93 | public function beforeUpdate() |
||
98 | |||
99 | /** |
||
100 | * After save action |
||
101 | */ |
||
102 | 2 | public function afterSave() |
|
106 | |||
107 | /** |
||
108 | * After delete action |
||
109 | */ |
||
110 | 1 | public function afterDelete() |
|
114 | |||
115 | /** |
||
116 | * Get image path |
||
117 | * @return string |
||
118 | */ |
||
119 | 3 | public function getImagePath() |
|
126 | |||
127 | /** |
||
128 | * Get default image URL |
||
129 | * @return string |
||
130 | * @throws yii\base\InvalidConfigException |
||
131 | */ |
||
132 | public function getImageDefaultUrl() |
||
136 | |||
137 | /** |
||
138 | * Get absolute team image path in filesystem |
||
139 | * @return string |
||
140 | */ |
||
141 | 3 | public function getImageAbsolutePath() |
|
153 | |||
154 | /** |
||
155 | * Check team image |
||
156 | * @return bool |
||
157 | * @throws yii\base\InvalidConfigException |
||
158 | */ |
||
159 | 3 | public function hasImage() |
|
164 | |||
165 | /** |
||
166 | * Check that image file exists |
||
167 | */ |
||
168 | public function imageFileExists() |
||
172 | |||
173 | /** |
||
174 | * Get team image URL |
||
175 | * @return string |
||
176 | * @throws yii\base\InvalidConfigException |
||
177 | */ |
||
178 | public function getImageUrl() |
||
184 | |||
185 | /** |
||
186 | * Return filename in filesystem |
||
187 | * @return string |
||
188 | */ |
||
189 | 3 | public function getImageFile() |
|
193 | |||
194 | /** |
||
195 | * Change image by uploaded file |
||
196 | */ |
||
197 | 2 | public function imageChangeByUpload() |
|
206 | |||
207 | /** |
||
208 | * Change image |
||
209 | * @param string|array $sourceFile source file. if set as array ['fileName' => 'file_in_filesystem'] |
||
210 | * @return bool true, if image was changed and old image file was deleted. false, if image not changed |
||
211 | * @throws yii\base\InvalidConfigException |
||
212 | */ |
||
213 | 3 | public function imageChange($sourceFile) |
|
269 | |||
270 | /** |
||
271 | * Reset image to default |
||
272 | * @throws yii\base\InvalidConfigException |
||
273 | */ |
||
274 | 1 | public function imageReset() |
|
280 | |||
281 | /** |
||
282 | * Remove current image |
||
283 | * @throws yii\base\InvalidConfigException |
||
284 | */ |
||
285 | 3 | protected function imageRemoveFile() |
|
291 | |||
292 | /** |
||
293 | * Make image URL |
||
294 | * @param string $imageFileName image filename |
||
295 | * @return string |
||
296 | * @throws yii\base\InvalidConfigException |
||
297 | */ |
||
298 | protected function _imageUrl($imageFileName) |
||
302 | |||
303 | } |
||
304 |
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: