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 | /** |
||
36 | * Name of attribute to store the image |
||
37 | * @var string |
||
38 | */ |
||
39 | public $imageAttribute = "image"; |
||
40 | /** |
||
41 | * Default image name |
||
42 | * @var string |
||
43 | */ |
||
44 | public $imageDefault = "image.png"; |
||
45 | /** |
||
46 | * Path to store image files. |
||
47 | * If empty will be init to /images/<ActiveRecord Class Name> |
||
48 | * @var string |
||
49 | */ |
||
50 | public $imagePath; |
||
51 | /** |
||
52 | * Size to convert image |
||
53 | * If array: [width, height]. |
||
54 | * If integer: use value for with and height. |
||
55 | * If not set: image save as is. |
||
56 | * @var integer|array |
||
57 | */ |
||
58 | public $imageSize; |
||
59 | /** |
||
60 | * Image resize strategy |
||
61 | * @var int |
||
62 | */ |
||
63 | public $imageResizeStrategy = self::NONE; |
||
64 | /** |
||
65 | * Image frame color |
||
66 | * @var |
||
67 | */ |
||
68 | public $imageFrameColor = "#FFF"; |
||
69 | /** |
||
70 | * Calculated absolute image path relative to webroot |
||
71 | * @var |
||
72 | */ |
||
73 | protected $_imageAbsolutePath; |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | 3 | public function events() |
|
87 | |||
88 | /** |
||
89 | * Before update action |
||
90 | */ |
||
91 | public function beforeUpdate() |
||
96 | |||
97 | /** |
||
98 | * After save action |
||
99 | */ |
||
100 | 2 | public function afterSave() |
|
104 | |||
105 | /** |
||
106 | * After delete action |
||
107 | */ |
||
108 | 1 | public function afterDelete() |
|
112 | |||
113 | /** |
||
114 | * Get image path |
||
115 | * @return string |
||
116 | */ |
||
117 | 3 | public function getImagePath() |
|
124 | |||
125 | /** |
||
126 | * Get default image URL |
||
127 | * @return string |
||
128 | * @throws yii\base\InvalidConfigException |
||
129 | */ |
||
130 | public function getImageDefaultUrl() |
||
134 | |||
135 | /** |
||
136 | * Get absolute team image path in filesystem |
||
137 | * @return string |
||
138 | */ |
||
139 | 3 | public function getImageAbsolutePath() |
|
151 | |||
152 | /** |
||
153 | * Check team image |
||
154 | * @return bool |
||
155 | * @throws yii\base\InvalidConfigException |
||
156 | */ |
||
157 | 3 | public function hasImage() |
|
162 | |||
163 | /** |
||
164 | * Check that image file exists |
||
165 | */ |
||
166 | public function imageFileExists() |
||
170 | |||
171 | /** |
||
172 | * Get team image URL |
||
173 | * @return string |
||
174 | * @throws yii\base\InvalidConfigException |
||
175 | */ |
||
176 | public function getImageUrl() |
||
182 | |||
183 | /** |
||
184 | * Return filename in filesystem |
||
185 | * @return string |
||
186 | */ |
||
187 | 3 | public function getImageFile() |
|
191 | |||
192 | /** |
||
193 | * Change image by uploaded file |
||
194 | */ |
||
195 | 2 | public function imageChangeByUpload() |
|
204 | |||
205 | /** |
||
206 | * Change image |
||
207 | * @param string|array $sourceFile source file. if set as array ['fileName' => 'file_in_filesystem'] |
||
208 | * @return bool true, if image was changed and old image file was deleted. false, if image not changed |
||
209 | * @throws yii\base\InvalidConfigException |
||
210 | */ |
||
211 | 3 | public function imageChange($sourceFile) |
|
260 | |||
261 | /** |
||
262 | * Reset image to default |
||
263 | * @throws yii\base\InvalidConfigException |
||
264 | */ |
||
265 | 1 | public function imageReset() |
|
271 | |||
272 | /** |
||
273 | * Remove current image |
||
274 | * @throws yii\base\InvalidConfigException |
||
275 | */ |
||
276 | 3 | protected function imageRemoveFile() |
|
282 | |||
283 | /** |
||
284 | * Make image URL |
||
285 | * @param string $imageFileName image filename |
||
286 | * @return string |
||
287 | * @throws yii\base\InvalidConfigException |
||
288 | */ |
||
289 | protected function _imageUrl($imageFileName) |
||
293 | |||
294 | } |
||
295 |
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: