@@ -357,7 +357,7 @@ discard block |
||
357 | 357 | /** |
358 | 358 | * Validate directory/permission before creating a folder |
359 | 359 | * |
360 | - * @param $dir string the folder name to check |
|
360 | + * @param string $dir string the folder name to check |
|
361 | 361 | * @return bool |
362 | 362 | */ |
363 | 363 | private function isDirectoryValid($dir) |
@@ -462,7 +462,7 @@ discard block |
||
462 | 462 | * Final upload method to be called, isolated for testing purposes |
463 | 463 | * |
464 | 464 | * @param $tmp_name int the temporary location of the image file |
465 | - * @param $destination int upload destination |
|
465 | + * @param string $destination int upload destination |
|
466 | 466 | * |
467 | 467 | * @return bool |
468 | 468 | */ |
@@ -17,448 +17,448 @@ |
||
17 | 17 | |
18 | 18 | class Image implements \ArrayAccess |
19 | 19 | { |
20 | - /** |
|
21 | - * @var string The new image name, to be provided or will be generated. |
|
22 | - */ |
|
23 | - protected $name; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var int The image width in pixels |
|
27 | - */ |
|
28 | - protected $width; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var int The image height in pixels |
|
32 | - */ |
|
33 | - protected $height; |
|
34 | - |
|
35 | - /** |
|
36 | - * @var string The image mime type (extension) |
|
37 | - */ |
|
38 | - protected $mime; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string The full image path (dir + image + mime) |
|
42 | - */ |
|
43 | - protected $fullPath; |
|
44 | - |
|
45 | - /** |
|
46 | - * @var string The folder or image storage location |
|
47 | - */ |
|
48 | - protected $location; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var array The min and max image size allowed for upload (in bytes) |
|
52 | - */ |
|
53 | - protected $size = array(100, 500000); |
|
54 | - |
|
55 | - /** |
|
56 | - * @var array The max height and width image allowed |
|
57 | - */ |
|
58 | - protected $dimensions = array(5000, 5000); |
|
59 | - |
|
60 | - /** |
|
61 | - * @var array The mime types allowed for upload |
|
62 | - */ |
|
63 | - protected $mimeTypes = array('jpeg', 'png', 'gif', 'jpg'); |
|
64 | - |
|
65 | - /** |
|
66 | - * @var array list of known image types |
|
67 | - */ |
|
68 | - protected $acceptedMimes = array( |
|
69 | - 1 => 'gif', 'jpeg', 'png', 'swf', 'psd', |
|
70 | - 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpx', |
|
71 | - 'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico' |
|
72 | - ); |
|
73 | - |
|
74 | - /** |
|
75 | - * @var array error messages strings |
|
76 | - */ |
|
77 | - protected $commonUploadErrors = array( |
|
78 | - UPLOAD_ERR_OK => '', |
|
79 | - UPLOAD_ERR_INI_SIZE => 'Image is larger than the specified amount set by the server', |
|
80 | - UPLOAD_ERR_FORM_SIZE => 'Image is larger than the specified amount specified by browser', |
|
81 | - UPLOAD_ERR_PARTIAL => 'Image could not be fully uploaded. Please try again later', |
|
82 | - UPLOAD_ERR_NO_FILE => 'Image is not found', |
|
83 | - UPLOAD_ERR_NO_TMP_DIR => 'Can\'t write to disk, due to server configuration ( No tmp dir found )', |
|
84 | - UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Please check you file permissions', |
|
85 | - UPLOAD_ERR_EXTENSION => 'A PHP extension has halted this file upload process' |
|
86 | - ); |
|
20 | + /** |
|
21 | + * @var string The new image name, to be provided or will be generated. |
|
22 | + */ |
|
23 | + protected $name; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var int The image width in pixels |
|
27 | + */ |
|
28 | + protected $width; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var int The image height in pixels |
|
32 | + */ |
|
33 | + protected $height; |
|
34 | + |
|
35 | + /** |
|
36 | + * @var string The image mime type (extension) |
|
37 | + */ |
|
38 | + protected $mime; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string The full image path (dir + image + mime) |
|
42 | + */ |
|
43 | + protected $fullPath; |
|
44 | + |
|
45 | + /** |
|
46 | + * @var string The folder or image storage location |
|
47 | + */ |
|
48 | + protected $location; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var array The min and max image size allowed for upload (in bytes) |
|
52 | + */ |
|
53 | + protected $size = array(100, 500000); |
|
54 | + |
|
55 | + /** |
|
56 | + * @var array The max height and width image allowed |
|
57 | + */ |
|
58 | + protected $dimensions = array(5000, 5000); |
|
59 | + |
|
60 | + /** |
|
61 | + * @var array The mime types allowed for upload |
|
62 | + */ |
|
63 | + protected $mimeTypes = array('jpeg', 'png', 'gif', 'jpg'); |
|
64 | + |
|
65 | + /** |
|
66 | + * @var array list of known image types |
|
67 | + */ |
|
68 | + protected $acceptedMimes = array( |
|
69 | + 1 => 'gif', 'jpeg', 'png', 'swf', 'psd', |
|
70 | + 'bmp', 'tiff', 'tiff', 'jpc', 'jp2', 'jpx', |
|
71 | + 'jb2', 'swc', 'iff', 'wbmp', 'xbm', 'ico' |
|
72 | + ); |
|
73 | + |
|
74 | + /** |
|
75 | + * @var array error messages strings |
|
76 | + */ |
|
77 | + protected $commonUploadErrors = array( |
|
78 | + UPLOAD_ERR_OK => '', |
|
79 | + UPLOAD_ERR_INI_SIZE => 'Image is larger than the specified amount set by the server', |
|
80 | + UPLOAD_ERR_FORM_SIZE => 'Image is larger than the specified amount specified by browser', |
|
81 | + UPLOAD_ERR_PARTIAL => 'Image could not be fully uploaded. Please try again later', |
|
82 | + UPLOAD_ERR_NO_FILE => 'Image is not found', |
|
83 | + UPLOAD_ERR_NO_TMP_DIR => 'Can\'t write to disk, due to server configuration ( No tmp dir found )', |
|
84 | + UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk. Please check you file permissions', |
|
85 | + UPLOAD_ERR_EXTENSION => 'A PHP extension has halted this file upload process' |
|
86 | + ); |
|
87 | 87 | |
88 | - /** |
|
89 | - * @var array storage for the $_FILES global array |
|
90 | - */ |
|
91 | - private $_files = array(); |
|
92 | - |
|
93 | - /** |
|
94 | - * @var string storage for any errors |
|
95 | - */ |
|
96 | - private $error = ''; |
|
97 | - |
|
98 | - /** |
|
99 | - * @param array $_files represents the $_FILES array passed as dependency |
|
100 | - */ |
|
101 | - public function __construct(array $_files = array()) |
|
102 | - { |
|
103 | - /* check if php_exif is enabled */ |
|
104 | - if (!function_exists('exif_imagetype')) { |
|
105 | - $this->error = 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your php.ini'; |
|
106 | - } |
|
107 | - |
|
108 | - $this->_files = $_files; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param mixed $offset |
|
113 | - * @param mixed $value |
|
114 | - * |
|
115 | - * @return null |
|
116 | - */ |
|
117 | - public function offsetSet($offset, $value){} |
|
118 | - |
|
119 | - /** |
|
120 | - * @param mixed $offset |
|
121 | - * |
|
122 | - * @return null |
|
123 | - */ |
|
124 | - public function offsetExists($offset){} |
|
125 | - |
|
126 | - /** |
|
127 | - * @param mixed $offset |
|
128 | - * |
|
129 | - * @return null |
|
130 | - */ |
|
131 | - public function offsetUnset($offset){} |
|
132 | - |
|
133 | - /** |
|
134 | - * Gets array value \ArrayAccess |
|
135 | - * |
|
136 | - * @param mixed $offset |
|
137 | - * @return string|boolean |
|
138 | - */ |
|
139 | - public function offsetGet($offset) |
|
140 | - { |
|
88 | + /** |
|
89 | + * @var array storage for the $_FILES global array |
|
90 | + */ |
|
91 | + private $_files = array(); |
|
92 | + |
|
93 | + /** |
|
94 | + * @var string storage for any errors |
|
95 | + */ |
|
96 | + private $error = ''; |
|
97 | + |
|
98 | + /** |
|
99 | + * @param array $_files represents the $_FILES array passed as dependency |
|
100 | + */ |
|
101 | + public function __construct(array $_files = array()) |
|
102 | + { |
|
103 | + /* check if php_exif is enabled */ |
|
104 | + if (!function_exists('exif_imagetype')) { |
|
105 | + $this->error = 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your php.ini'; |
|
106 | + } |
|
107 | + |
|
108 | + $this->_files = $_files; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param mixed $offset |
|
113 | + * @param mixed $value |
|
114 | + * |
|
115 | + * @return null |
|
116 | + */ |
|
117 | + public function offsetSet($offset, $value){} |
|
118 | + |
|
119 | + /** |
|
120 | + * @param mixed $offset |
|
121 | + * |
|
122 | + * @return null |
|
123 | + */ |
|
124 | + public function offsetExists($offset){} |
|
125 | + |
|
126 | + /** |
|
127 | + * @param mixed $offset |
|
128 | + * |
|
129 | + * @return null |
|
130 | + */ |
|
131 | + public function offsetUnset($offset){} |
|
132 | + |
|
133 | + /** |
|
134 | + * Gets array value \ArrayAccess |
|
135 | + * |
|
136 | + * @param mixed $offset |
|
137 | + * @return string|boolean |
|
138 | + */ |
|
139 | + public function offsetGet($offset) |
|
140 | + { |
|
141 | 141 | |
142 | - // return false if $image['key'] isn't found |
|
143 | - if (!isset($this->_files[$offset])) { |
|
144 | - $this->error = sprintf('No file input found with name: (%s)', $offset); |
|
145 | - return false; |
|
146 | - } |
|
147 | - |
|
148 | - $this->_files = $this->_files[$offset]; |
|
149 | - |
|
150 | - // check for common upload errors |
|
151 | - if(isset($this->_files['error'])){ |
|
152 | - $this->error = $this->commonUploadErrors[$this->_files['error']]; |
|
153 | - } |
|
154 | - |
|
155 | - return true; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Sets max image height and width limit |
|
160 | - * |
|
161 | - * @param $maxWidth int max width value |
|
162 | - * @param $maxHeight int max height value |
|
163 | - * |
|
164 | - * @return $this |
|
165 | - */ |
|
166 | - public function setDimension($maxWidth, $maxHeight) |
|
167 | - { |
|
168 | - $this->dimensions = array($maxWidth, $maxHeight); |
|
169 | - return $this; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Returns the full path of the image ex 'location/image.mime' |
|
174 | - * |
|
175 | - * @return string |
|
176 | - */ |
|
177 | - public function getFullPath() |
|
178 | - { |
|
179 | - return $this->fullPath = $this->getLocation() . '/' . $this->getName() . '.' . $this->getMime(); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Returns the image size in bytes |
|
184 | - * |
|
185 | - * @return int |
|
186 | - */ |
|
187 | - public function getSize() |
|
188 | - { |
|
189 | - return (int) $this->_files['size']; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Define a min and max image size for uploading |
|
194 | - * |
|
195 | - * @param $min int minimum value in bytes |
|
196 | - * @param $max int maximum value in bytes |
|
197 | - * |
|
198 | - * @return $this |
|
199 | - */ |
|
200 | - public function setSize($min, $max) |
|
201 | - { |
|
202 | - $this->size = array($min, $max); |
|
203 | - return $this; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Returns a JSON format of the image width, height, name, mime ... |
|
208 | - * |
|
209 | - * @return string |
|
210 | - */ |
|
211 | - public function getJson() |
|
212 | - { |
|
213 | - return json_encode ( |
|
214 | - array( |
|
215 | - 'name' => $this->name, |
|
216 | - 'mime' => $this->mime, |
|
217 | - 'height' => $this->height, |
|
218 | - 'width' => $this->width, |
|
219 | - 'size' => $this->_files['size'], |
|
220 | - 'location' => $this->location, |
|
221 | - 'fullpath' => $this->fullPath |
|
222 | - ) |
|
223 | - ); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Returns the image mime type |
|
228 | - * |
|
229 | - * @return null|string |
|
230 | - */ |
|
231 | - public function getMime() |
|
232 | - { |
|
233 | - if(!$this->mime){ |
|
234 | - $this->mime = $this->getImageMime($this->_files['tmp_name']); |
|
235 | - } |
|
142 | + // return false if $image['key'] isn't found |
|
143 | + if (!isset($this->_files[$offset])) { |
|
144 | + $this->error = sprintf('No file input found with name: (%s)', $offset); |
|
145 | + return false; |
|
146 | + } |
|
147 | + |
|
148 | + $this->_files = $this->_files[$offset]; |
|
149 | + |
|
150 | + // check for common upload errors |
|
151 | + if(isset($this->_files['error'])){ |
|
152 | + $this->error = $this->commonUploadErrors[$this->_files['error']]; |
|
153 | + } |
|
154 | + |
|
155 | + return true; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Sets max image height and width limit |
|
160 | + * |
|
161 | + * @param $maxWidth int max width value |
|
162 | + * @param $maxHeight int max height value |
|
163 | + * |
|
164 | + * @return $this |
|
165 | + */ |
|
166 | + public function setDimension($maxWidth, $maxHeight) |
|
167 | + { |
|
168 | + $this->dimensions = array($maxWidth, $maxHeight); |
|
169 | + return $this; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Returns the full path of the image ex 'location/image.mime' |
|
174 | + * |
|
175 | + * @return string |
|
176 | + */ |
|
177 | + public function getFullPath() |
|
178 | + { |
|
179 | + return $this->fullPath = $this->getLocation() . '/' . $this->getName() . '.' . $this->getMime(); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Returns the image size in bytes |
|
184 | + * |
|
185 | + * @return int |
|
186 | + */ |
|
187 | + public function getSize() |
|
188 | + { |
|
189 | + return (int) $this->_files['size']; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Define a min and max image size for uploading |
|
194 | + * |
|
195 | + * @param $min int minimum value in bytes |
|
196 | + * @param $max int maximum value in bytes |
|
197 | + * |
|
198 | + * @return $this |
|
199 | + */ |
|
200 | + public function setSize($min, $max) |
|
201 | + { |
|
202 | + $this->size = array($min, $max); |
|
203 | + return $this; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Returns a JSON format of the image width, height, name, mime ... |
|
208 | + * |
|
209 | + * @return string |
|
210 | + */ |
|
211 | + public function getJson() |
|
212 | + { |
|
213 | + return json_encode ( |
|
214 | + array( |
|
215 | + 'name' => $this->name, |
|
216 | + 'mime' => $this->mime, |
|
217 | + 'height' => $this->height, |
|
218 | + 'width' => $this->width, |
|
219 | + 'size' => $this->_files['size'], |
|
220 | + 'location' => $this->location, |
|
221 | + 'fullpath' => $this->fullPath |
|
222 | + ) |
|
223 | + ); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Returns the image mime type |
|
228 | + * |
|
229 | + * @return null|string |
|
230 | + */ |
|
231 | + public function getMime() |
|
232 | + { |
|
233 | + if(!$this->mime){ |
|
234 | + $this->mime = $this->getImageMime($this->_files['tmp_name']); |
|
235 | + } |
|
236 | 236 | |
237 | - return $this->mime; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * Define a mime type for uploading |
|
242 | - * |
|
243 | - * @param array $fileTypes |
|
244 | - * |
|
245 | - * @return $this |
|
246 | - */ |
|
247 | - public function setMime(array $fileTypes) |
|
248 | - { |
|
249 | - $this->mimeTypes = $fileTypes; |
|
250 | - return $this; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Gets the real image mime type |
|
255 | - * |
|
256 | - * @param $tmp_name string The upload tmp directory |
|
257 | - * |
|
258 | - * @return null|string |
|
259 | - */ |
|
260 | - protected function getImageMime($tmp_name) |
|
261 | - { |
|
262 | - $this->mime = @$this->acceptedMimes[exif_imagetype($tmp_name)]; |
|
263 | - if (!$this->mime) { |
|
264 | - return null; |
|
265 | - } |
|
266 | - |
|
267 | - return $this->mime; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Returns error string or false if no errors occurred |
|
272 | - * |
|
273 | - * @return string|false |
|
274 | - */ |
|
275 | - public function getError() |
|
276 | - { |
|
277 | - return $this->error; |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * Returns the image name |
|
282 | - * |
|
283 | - * @return string |
|
284 | - */ |
|
285 | - public function getName() |
|
286 | - { |
|
287 | - if (!$this->name) { |
|
288 | - $this->name = uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q'))); |
|
289 | - } |
|
290 | - |
|
291 | - return $this->name; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Provide image name if not provided |
|
296 | - * |
|
297 | - * @param null $isNameProvided |
|
298 | - * @return $this |
|
299 | - */ |
|
300 | - public function setName($isNameProvided = null) |
|
301 | - { |
|
302 | - if ($isNameProvided) { |
|
303 | - $this->name = filter_var($isNameProvided, FILTER_SANITIZE_STRING); |
|
304 | - } |
|
305 | - |
|
306 | - return $this; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Returns the image width |
|
311 | - * |
|
312 | - * @return int |
|
313 | - */ |
|
314 | - public function getWidth() |
|
315 | - { |
|
316 | - if ($this->width != null) { |
|
317 | - return $this->width; |
|
318 | - } |
|
319 | - |
|
320 | - list($width) = getImageSize($this->_files['tmp_name']); |
|
321 | - return $width; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Returns the image height in pixels |
|
326 | - * |
|
327 | - * @return int |
|
328 | - */ |
|
329 | - public function getHeight() |
|
330 | - { |
|
331 | - if ($this->height != null) { |
|
332 | - return $this->height; |
|
333 | - } |
|
334 | - |
|
335 | - list(, $height) = getImageSize($this->_files['tmp_name']); |
|
336 | - return $height; |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Returns the storage / folder name |
|
341 | - * |
|
342 | - * @return string |
|
343 | - */ |
|
344 | - public function getLocation() |
|
345 | - { |
|
346 | - if (!$this->location) { |
|
347 | - $this->setLocation(); |
|
348 | - } |
|
349 | - |
|
350 | - return $this->location; |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Validate directory/permission before creating a folder |
|
355 | - * |
|
356 | - * @param $dir string the folder name to check |
|
357 | - * @return bool |
|
358 | - */ |
|
359 | - private function isDirectoryValid($dir) |
|
360 | - { |
|
361 | - return !file_exists($dir) && !is_dir($dir) || is_writable($dir); |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * Creates a location for upload storage |
|
366 | - * |
|
367 | - * @param $dir string the folder name to create |
|
368 | - * @param int $permission chmod permission |
|
369 | - * @return $this |
|
370 | - */ |
|
371 | - public function setLocation($dir = 'bulletproof', $permission = 0666) |
|
372 | - { |
|
373 | - $isDirectoryValid = $this->isDirectoryValid($dir); |
|
374 | - |
|
375 | - if(!$isDirectoryValid){ |
|
376 | - $this->error = 'Can not create a directory \'' . $dir . '\', please check your dir permission'; |
|
377 | - return false; |
|
378 | - } |
|
237 | + return $this->mime; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * Define a mime type for uploading |
|
242 | + * |
|
243 | + * @param array $fileTypes |
|
244 | + * |
|
245 | + * @return $this |
|
246 | + */ |
|
247 | + public function setMime(array $fileTypes) |
|
248 | + { |
|
249 | + $this->mimeTypes = $fileTypes; |
|
250 | + return $this; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Gets the real image mime type |
|
255 | + * |
|
256 | + * @param $tmp_name string The upload tmp directory |
|
257 | + * |
|
258 | + * @return null|string |
|
259 | + */ |
|
260 | + protected function getImageMime($tmp_name) |
|
261 | + { |
|
262 | + $this->mime = @$this->acceptedMimes[exif_imagetype($tmp_name)]; |
|
263 | + if (!$this->mime) { |
|
264 | + return null; |
|
265 | + } |
|
266 | + |
|
267 | + return $this->mime; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Returns error string or false if no errors occurred |
|
272 | + * |
|
273 | + * @return string|false |
|
274 | + */ |
|
275 | + public function getError() |
|
276 | + { |
|
277 | + return $this->error; |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * Returns the image name |
|
282 | + * |
|
283 | + * @return string |
|
284 | + */ |
|
285 | + public function getName() |
|
286 | + { |
|
287 | + if (!$this->name) { |
|
288 | + $this->name = uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q'))); |
|
289 | + } |
|
290 | + |
|
291 | + return $this->name; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Provide image name if not provided |
|
296 | + * |
|
297 | + * @param null $isNameProvided |
|
298 | + * @return $this |
|
299 | + */ |
|
300 | + public function setName($isNameProvided = null) |
|
301 | + { |
|
302 | + if ($isNameProvided) { |
|
303 | + $this->name = filter_var($isNameProvided, FILTER_SANITIZE_STRING); |
|
304 | + } |
|
305 | + |
|
306 | + return $this; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Returns the image width |
|
311 | + * |
|
312 | + * @return int |
|
313 | + */ |
|
314 | + public function getWidth() |
|
315 | + { |
|
316 | + if ($this->width != null) { |
|
317 | + return $this->width; |
|
318 | + } |
|
319 | + |
|
320 | + list($width) = getImageSize($this->_files['tmp_name']); |
|
321 | + return $width; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Returns the image height in pixels |
|
326 | + * |
|
327 | + * @return int |
|
328 | + */ |
|
329 | + public function getHeight() |
|
330 | + { |
|
331 | + if ($this->height != null) { |
|
332 | + return $this->height; |
|
333 | + } |
|
334 | + |
|
335 | + list(, $height) = getImageSize($this->_files['tmp_name']); |
|
336 | + return $height; |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Returns the storage / folder name |
|
341 | + * |
|
342 | + * @return string |
|
343 | + */ |
|
344 | + public function getLocation() |
|
345 | + { |
|
346 | + if (!$this->location) { |
|
347 | + $this->setLocation(); |
|
348 | + } |
|
349 | + |
|
350 | + return $this->location; |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Validate directory/permission before creating a folder |
|
355 | + * |
|
356 | + * @param $dir string the folder name to check |
|
357 | + * @return bool |
|
358 | + */ |
|
359 | + private function isDirectoryValid($dir) |
|
360 | + { |
|
361 | + return !file_exists($dir) && !is_dir($dir) || is_writable($dir); |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * Creates a location for upload storage |
|
366 | + * |
|
367 | + * @param $dir string the folder name to create |
|
368 | + * @param int $permission chmod permission |
|
369 | + * @return $this |
|
370 | + */ |
|
371 | + public function setLocation($dir = 'bulletproof', $permission = 0666) |
|
372 | + { |
|
373 | + $isDirectoryValid = $this->isDirectoryValid($dir); |
|
374 | + |
|
375 | + if(!$isDirectoryValid){ |
|
376 | + $this->error = 'Can not create a directory \'' . $dir . '\', please check your dir permission'; |
|
377 | + return false; |
|
378 | + } |
|
379 | 379 | |
380 | - $create = !is_dir($dir) ? @mkdir('' . $dir, (int) $permission, true) : true; |
|
381 | - |
|
382 | - if (!$create) { |
|
383 | - $this->error = 'Error! directory \'' . $dir . '\' could not be created'; |
|
384 | - return false; |
|
385 | - } |
|
386 | - |
|
387 | - $this->location = $dir; |
|
388 | - return $this; |
|
389 | - } |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * Validate image and upload |
|
394 | - * |
|
395 | - * @return false|null|Image |
|
396 | - */ |
|
397 | - public function upload() |
|
398 | - { |
|
399 | - $image = $this; |
|
400 | - $files = $this->_files; |
|
401 | - |
|
402 | - if ($this->error || !isset($files['tmp_name'])) { |
|
403 | - return false; |
|
404 | - } |
|
405 | - |
|
406 | - /* check image for valid mime types and return mime */ |
|
407 | - $image->getImageMime($files['tmp_name']); |
|
408 | - /* validate image mime type */ |
|
409 | - if (!in_array($image->mime, $image->mimeTypes)) { |
|
410 | - $ext = implode(', ', $image->mimeTypes); |
|
411 | - $image->error = sprintf('Invalid File! Only (%s) image types are allowed', $ext); |
|
412 | - return false; |
|
413 | - } |
|
414 | - |
|
415 | - /* initialize image properties */ |
|
416 | - $image->name = $image->getName(); |
|
417 | - $image->width = $image->getWidth(); |
|
418 | - $image->height = $image->getHeight(); |
|
419 | - $image->getFullPath(); |
|
380 | + $create = !is_dir($dir) ? @mkdir('' . $dir, (int) $permission, true) : true; |
|
381 | + |
|
382 | + if (!$create) { |
|
383 | + $this->error = 'Error! directory \'' . $dir . '\' could not be created'; |
|
384 | + return false; |
|
385 | + } |
|
386 | + |
|
387 | + $this->location = $dir; |
|
388 | + return $this; |
|
389 | + } |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * Validate image and upload |
|
394 | + * |
|
395 | + * @return false|null|Image |
|
396 | + */ |
|
397 | + public function upload() |
|
398 | + { |
|
399 | + $image = $this; |
|
400 | + $files = $this->_files; |
|
401 | + |
|
402 | + if ($this->error || !isset($files['tmp_name'])) { |
|
403 | + return false; |
|
404 | + } |
|
405 | + |
|
406 | + /* check image for valid mime types and return mime */ |
|
407 | + $image->getImageMime($files['tmp_name']); |
|
408 | + /* validate image mime type */ |
|
409 | + if (!in_array($image->mime, $image->mimeTypes)) { |
|
410 | + $ext = implode(', ', $image->mimeTypes); |
|
411 | + $image->error = sprintf('Invalid File! Only (%s) image types are allowed', $ext); |
|
412 | + return false; |
|
413 | + } |
|
414 | + |
|
415 | + /* initialize image properties */ |
|
416 | + $image->name = $image->getName(); |
|
417 | + $image->width = $image->getWidth(); |
|
418 | + $image->height = $image->getHeight(); |
|
419 | + $image->getFullPath(); |
|
420 | 420 | |
421 | 421 | |
422 | - /* get image sizes */ |
|
423 | - list($minSize, $maxSize) = $image->size; |
|
424 | - |
|
425 | - /* check image size based on the settings */ |
|
426 | - if ($files['size'] < $minSize || $files['size'] > $maxSize) { |
|
427 | - $min = intval($minSize / 1000) ?: 1; |
|
428 | - $max = intval($maxSize / 1000) ?: 1; |
|
429 | - $image->error = 'Image size should be at least ' . $min . ' KB, and no more than ' . $max . ' KB'; |
|
430 | - return false; |
|
431 | - } |
|
432 | - |
|
433 | - /* check image dimension */ |
|
434 | - list($allowedWidth, $allowedHeight) = $image->dimensions; |
|
435 | - |
|
436 | - if ( |
|
437 | - $image->height > $allowedHeight || |
|
438 | - $image->width > $allowedWidth || |
|
439 | - $image->height < 2 || |
|
440 | - $image->width < 2 |
|
441 | - ) { |
|
442 | - $image->error = 'Image height/width should be less than ' . $allowedHeight . '/' . $allowedWidth . ' pixels'; |
|
443 | - return false; |
|
444 | - } |
|
445 | - |
|
446 | - $isSaved = $image->isSaved($files['tmp_name'], $image->fullPath); |
|
447 | - |
|
448 | - return $isSaved ? $image : false; |
|
449 | - } |
|
450 | - |
|
451 | - |
|
452 | - /** |
|
453 | - * Final upload method to be called, isolated for testing purposes |
|
454 | - * |
|
455 | - * @param $tmp_name int the temporary location of the image file |
|
456 | - * @param $destination int upload destination |
|
457 | - * |
|
458 | - * @return bool |
|
459 | - */ |
|
460 | - protected function isSaved($tmp_name, $destination) |
|
461 | - { |
|
462 | - return move_uploaded_file($tmp_name, $destination); |
|
463 | - } |
|
422 | + /* get image sizes */ |
|
423 | + list($minSize, $maxSize) = $image->size; |
|
424 | + |
|
425 | + /* check image size based on the settings */ |
|
426 | + if ($files['size'] < $minSize || $files['size'] > $maxSize) { |
|
427 | + $min = intval($minSize / 1000) ?: 1; |
|
428 | + $max = intval($maxSize / 1000) ?: 1; |
|
429 | + $image->error = 'Image size should be at least ' . $min . ' KB, and no more than ' . $max . ' KB'; |
|
430 | + return false; |
|
431 | + } |
|
432 | + |
|
433 | + /* check image dimension */ |
|
434 | + list($allowedWidth, $allowedHeight) = $image->dimensions; |
|
435 | + |
|
436 | + if ( |
|
437 | + $image->height > $allowedHeight || |
|
438 | + $image->width > $allowedWidth || |
|
439 | + $image->height < 2 || |
|
440 | + $image->width < 2 |
|
441 | + ) { |
|
442 | + $image->error = 'Image height/width should be less than ' . $allowedHeight . '/' . $allowedWidth . ' pixels'; |
|
443 | + return false; |
|
444 | + } |
|
445 | + |
|
446 | + $isSaved = $image->isSaved($files['tmp_name'], $image->fullPath); |
|
447 | + |
|
448 | + return $isSaved ? $image : false; |
|
449 | + } |
|
450 | + |
|
451 | + |
|
452 | + /** |
|
453 | + * Final upload method to be called, isolated for testing purposes |
|
454 | + * |
|
455 | + * @param $tmp_name int the temporary location of the image file |
|
456 | + * @param $destination int upload destination |
|
457 | + * |
|
458 | + * @return bool |
|
459 | + */ |
|
460 | + protected function isSaved($tmp_name, $destination) |
|
461 | + { |
|
462 | + return move_uploaded_file($tmp_name, $destination); |
|
463 | + } |
|
464 | 464 | } |
465 | 465 | \ No newline at end of file |
@@ -98,8 +98,7 @@ discard block |
||
98 | 98 | /** |
99 | 99 | * @param array $_files represents the $_FILES array passed as dependency |
100 | 100 | */ |
101 | - public function __construct(array $_files = array()) |
|
102 | - { |
|
101 | + public function __construct(array $_files = array()) { |
|
103 | 102 | /* check if php_exif is enabled */ |
104 | 103 | if (!function_exists('exif_imagetype')) { |
105 | 104 | $this->error = 'Function \'exif_imagetype\' Not found. Please enable \'php_exif\' in your php.ini'; |
@@ -136,8 +135,7 @@ discard block |
||
136 | 135 | * @param mixed $offset |
137 | 136 | * @return string|boolean |
138 | 137 | */ |
139 | - public function offsetGet($offset) |
|
140 | - { |
|
138 | + public function offsetGet($offset) { |
|
141 | 139 | |
142 | 140 | // return false if $image['key'] isn't found |
143 | 141 | if (!isset($this->_files[$offset])) { |
@@ -163,8 +161,7 @@ discard block |
||
163 | 161 | * |
164 | 162 | * @return $this |
165 | 163 | */ |
166 | - public function setDimension($maxWidth, $maxHeight) |
|
167 | - { |
|
164 | + public function setDimension($maxWidth, $maxHeight) { |
|
168 | 165 | $this->dimensions = array($maxWidth, $maxHeight); |
169 | 166 | return $this; |
170 | 167 | } |
@@ -174,8 +171,7 @@ discard block |
||
174 | 171 | * |
175 | 172 | * @return string |
176 | 173 | */ |
177 | - public function getFullPath() |
|
178 | - { |
|
174 | + public function getFullPath() { |
|
179 | 175 | return $this->fullPath = $this->getLocation() . '/' . $this->getName() . '.' . $this->getMime(); |
180 | 176 | } |
181 | 177 | |
@@ -184,8 +180,7 @@ discard block |
||
184 | 180 | * |
185 | 181 | * @return int |
186 | 182 | */ |
187 | - public function getSize() |
|
188 | - { |
|
183 | + public function getSize() { |
|
189 | 184 | return (int) $this->_files['size']; |
190 | 185 | } |
191 | 186 | |
@@ -197,8 +192,7 @@ discard block |
||
197 | 192 | * |
198 | 193 | * @return $this |
199 | 194 | */ |
200 | - public function setSize($min, $max) |
|
201 | - { |
|
195 | + public function setSize($min, $max) { |
|
202 | 196 | $this->size = array($min, $max); |
203 | 197 | return $this; |
204 | 198 | } |
@@ -208,8 +202,7 @@ discard block |
||
208 | 202 | * |
209 | 203 | * @return string |
210 | 204 | */ |
211 | - public function getJson() |
|
212 | - { |
|
205 | + public function getJson() { |
|
213 | 206 | return json_encode ( |
214 | 207 | array( |
215 | 208 | 'name' => $this->name, |
@@ -228,8 +221,7 @@ discard block |
||
228 | 221 | * |
229 | 222 | * @return null|string |
230 | 223 | */ |
231 | - public function getMime() |
|
232 | - { |
|
224 | + public function getMime() { |
|
233 | 225 | if(!$this->mime){ |
234 | 226 | $this->mime = $this->getImageMime($this->_files['tmp_name']); |
235 | 227 | } |
@@ -244,8 +236,7 @@ discard block |
||
244 | 236 | * |
245 | 237 | * @return $this |
246 | 238 | */ |
247 | - public function setMime(array $fileTypes) |
|
248 | - { |
|
239 | + public function setMime(array $fileTypes) { |
|
249 | 240 | $this->mimeTypes = $fileTypes; |
250 | 241 | return $this; |
251 | 242 | } |
@@ -257,8 +248,7 @@ discard block |
||
257 | 248 | * |
258 | 249 | * @return null|string |
259 | 250 | */ |
260 | - protected function getImageMime($tmp_name) |
|
261 | - { |
|
251 | + protected function getImageMime($tmp_name) { |
|
262 | 252 | $this->mime = @$this->acceptedMimes[exif_imagetype($tmp_name)]; |
263 | 253 | if (!$this->mime) { |
264 | 254 | return null; |
@@ -272,8 +262,7 @@ discard block |
||
272 | 262 | * |
273 | 263 | * @return string|false |
274 | 264 | */ |
275 | - public function getError() |
|
276 | - { |
|
265 | + public function getError() { |
|
277 | 266 | return $this->error; |
278 | 267 | } |
279 | 268 | |
@@ -282,8 +271,7 @@ discard block |
||
282 | 271 | * |
283 | 272 | * @return string |
284 | 273 | */ |
285 | - public function getName() |
|
286 | - { |
|
274 | + public function getName() { |
|
287 | 275 | if (!$this->name) { |
288 | 276 | $this->name = uniqid('', true) . '_' . str_shuffle(implode(range('e', 'q'))); |
289 | 277 | } |
@@ -297,8 +285,7 @@ discard block |
||
297 | 285 | * @param null $isNameProvided |
298 | 286 | * @return $this |
299 | 287 | */ |
300 | - public function setName($isNameProvided = null) |
|
301 | - { |
|
288 | + public function setName($isNameProvided = null) { |
|
302 | 289 | if ($isNameProvided) { |
303 | 290 | $this->name = filter_var($isNameProvided, FILTER_SANITIZE_STRING); |
304 | 291 | } |
@@ -311,8 +298,7 @@ discard block |
||
311 | 298 | * |
312 | 299 | * @return int |
313 | 300 | */ |
314 | - public function getWidth() |
|
315 | - { |
|
301 | + public function getWidth() { |
|
316 | 302 | if ($this->width != null) { |
317 | 303 | return $this->width; |
318 | 304 | } |
@@ -326,8 +312,7 @@ discard block |
||
326 | 312 | * |
327 | 313 | * @return int |
328 | 314 | */ |
329 | - public function getHeight() |
|
330 | - { |
|
315 | + public function getHeight() { |
|
331 | 316 | if ($this->height != null) { |
332 | 317 | return $this->height; |
333 | 318 | } |
@@ -341,8 +326,7 @@ discard block |
||
341 | 326 | * |
342 | 327 | * @return string |
343 | 328 | */ |
344 | - public function getLocation() |
|
345 | - { |
|
329 | + public function getLocation() { |
|
346 | 330 | if (!$this->location) { |
347 | 331 | $this->setLocation(); |
348 | 332 | } |
@@ -356,8 +340,7 @@ discard block |
||
356 | 340 | * @param $dir string the folder name to check |
357 | 341 | * @return bool |
358 | 342 | */ |
359 | - private function isDirectoryValid($dir) |
|
360 | - { |
|
343 | + private function isDirectoryValid($dir) { |
|
361 | 344 | return !file_exists($dir) && !is_dir($dir) || is_writable($dir); |
362 | 345 | } |
363 | 346 | |
@@ -368,8 +351,7 @@ discard block |
||
368 | 351 | * @param int $permission chmod permission |
369 | 352 | * @return $this |
370 | 353 | */ |
371 | - public function setLocation($dir = 'bulletproof', $permission = 0666) |
|
372 | - { |
|
354 | + public function setLocation($dir = 'bulletproof', $permission = 0666) { |
|
373 | 355 | $isDirectoryValid = $this->isDirectoryValid($dir); |
374 | 356 | |
375 | 357 | if(!$isDirectoryValid){ |
@@ -394,8 +376,7 @@ discard block |
||
394 | 376 | * |
395 | 377 | * @return false|null|Image |
396 | 378 | */ |
397 | - public function upload() |
|
398 | - { |
|
379 | + public function upload() { |
|
399 | 380 | $image = $this; |
400 | 381 | $files = $this->_files; |
401 | 382 | |
@@ -457,8 +438,7 @@ discard block |
||
457 | 438 | * |
458 | 439 | * @return bool |
459 | 440 | */ |
460 | - protected function isSaved($tmp_name, $destination) |
|
461 | - { |
|
441 | + protected function isSaved($tmp_name, $destination) { |
|
462 | 442 | return move_uploaded_file($tmp_name, $destination); |
463 | 443 | } |
464 | 444 | } |
465 | 445 | \ No newline at end of file |