1 | <?php |
||
26 | class ImageBehavior extends Behavior |
||
27 | { |
||
28 | /** Just proportional resize image to fit size */ |
||
29 | const NONE = 0; |
||
30 | /** Crop image resize strategy */ |
||
31 | const CROP = 1; |
||
32 | /** Add frame image resize strategy */ |
||
33 | const FRAME = 2; |
||
34 | /** |
||
35 | * Name of attribute to store the image |
||
36 | * @var string |
||
37 | */ |
||
38 | public $imageAttribute = "image"; |
||
39 | /** |
||
40 | * Default image name |
||
41 | * @var string |
||
42 | */ |
||
43 | public $imageDefault = "image.png"; |
||
44 | /** |
||
45 | * Path to store image files. |
||
46 | * If empty will be init to /images/<ActiveRecord Class Name> |
||
47 | * @var string |
||
48 | */ |
||
49 | public $imagePath; |
||
50 | /** |
||
51 | * Size to convert image |
||
52 | * If array: [width, height]. |
||
53 | * If integer: use value for with and height. |
||
54 | * If not set: image save as is. |
||
55 | * @var integer|array |
||
56 | */ |
||
57 | public $imageSize; |
||
58 | /** |
||
59 | * Image resize strategy |
||
60 | * @var int |
||
61 | */ |
||
62 | public $imageResizeStrategy = self::NONE; |
||
63 | /** |
||
64 | * Image frame color |
||
65 | * @var |
||
66 | */ |
||
67 | public $imageFrameColor = "#FFF"; |
||
68 | /** |
||
69 | * Calculated absolute image path relative to webroot |
||
70 | * @var |
||
71 | */ |
||
72 | protected $_imageAbsolutePath; |
||
73 | |||
74 | /** |
||
75 | * @inheritdoc |
||
76 | */ |
||
77 | 3 | public function events() |
|
85 | |||
86 | /** |
||
87 | * After save action |
||
88 | */ |
||
89 | 2 | public function afterSave() |
|
93 | |||
94 | /** |
||
95 | * After delete action |
||
96 | */ |
||
97 | 1 | public function afterDelete() |
|
101 | |||
102 | /** |
||
103 | * Get image path |
||
104 | * @return string |
||
105 | */ |
||
106 | 3 | public function getImagePath() |
|
113 | |||
114 | /** |
||
115 | * Get default image URL |
||
116 | * @return string |
||
117 | * @throws yii\base\InvalidConfigException |
||
118 | */ |
||
119 | public function getImageDefaultUrl() |
||
123 | |||
124 | /** |
||
125 | * Get absolute team image path in filesystem |
||
126 | * @return string |
||
127 | */ |
||
128 | 3 | public function getImageAbsolutePath() |
|
140 | |||
141 | /** |
||
142 | * Check team image |
||
143 | * @return bool |
||
144 | * @throws yii\base\InvalidConfigException |
||
145 | */ |
||
146 | 3 | public function hasImage() |
|
151 | |||
152 | /** |
||
153 | * Check that image file exists |
||
154 | */ |
||
155 | public function imageFileExists() |
||
159 | |||
160 | /** |
||
161 | * Get team image URL |
||
162 | * @return string |
||
163 | * @throws yii\base\InvalidConfigException |
||
164 | */ |
||
165 | public function getImageUrl() |
||
171 | |||
172 | /** |
||
173 | * Return filename in filesystem |
||
174 | * @return string |
||
175 | */ |
||
176 | 3 | public function getImageFile() |
|
180 | |||
181 | /** |
||
182 | * Change image by uploaded file |
||
183 | */ |
||
184 | 2 | public function imageChangeByUpload() |
|
193 | |||
194 | /** |
||
195 | * Change image |
||
196 | * @param string|array $sourceFile source file. if set as array ['fileName' => 'file_in_filesystem'] |
||
197 | * @return bool true, if image was changed and old image file was deleted. false, if image not changed |
||
198 | * @throws yii\base\InvalidConfigException |
||
199 | */ |
||
200 | 3 | public function imageChange($sourceFile) |
|
249 | |||
250 | /** |
||
251 | * Reset image to default |
||
252 | * @throws yii\base\InvalidConfigException |
||
253 | */ |
||
254 | 1 | public function imageReset() |
|
260 | |||
261 | /** |
||
262 | * Remove current image |
||
263 | * @throws yii\base\InvalidConfigException |
||
264 | */ |
||
265 | 3 | protected function imageRemoveFile() |
|
271 | |||
272 | /** |
||
273 | * Make image URL |
||
274 | * @param string $imageFileName image filename |
||
275 | * @return string |
||
276 | * @throws yii\base\InvalidConfigException |
||
277 | */ |
||
278 | protected function _imageUrl($imageFileName) |
||
282 | |||
283 | } |
||
284 |
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: