Total Complexity | 158 |
Total Lines | 911 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
Complex classes like ImgPicker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImgPicker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class ImgPicker |
||
7 | { |
||
8 | protected $options; |
||
9 | |||
10 | protected $error_messages = [ |
||
11 | // File upload errors codes |
||
12 | // http://www.php.net/manual/en/features.file-upload.errors.php |
||
13 | 1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', |
||
14 | 2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', |
||
15 | 3 => 'The uploaded file was only partially uploaded', |
||
16 | 4 => 'No file was uploaded', |
||
17 | 6 => 'Missing a temporary folder', |
||
18 | 7 => 'Failed to write file to disk', |
||
19 | 8 => 'A PHP extension stopped the file upload', |
||
20 | 'gd' => 'PHP GD library is NOT installed on your web server', |
||
21 | 'post_max_size' => 'The uploaded file exceeds the post_max_size directive in php.ini', |
||
22 | 'max_file_size' => 'File is too big', |
||
23 | 'min_file_size' => 'File is too small', |
||
24 | 'accept_file_types' => 'Filetype not allowed', |
||
25 | 'max_width' => 'Image exceeds maximum width of ', |
||
26 | 'min_width' => 'Image requires a minimum width of ', |
||
27 | 'max_height' => 'Image exceeds maximum height of ', |
||
28 | 'min_height' => 'Image requires a minimum height of ', |
||
29 | 'upload_failed' => 'Failed to upload the file', |
||
30 | 'move_failed' => 'Failed to upload the file', |
||
31 | 'invalid_image' => 'Invalid image', |
||
32 | 'image_resize' => 'Failed to resize image', |
||
33 | 'not_exists' => 'Failed to load the image', |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param array $options |
||
40 | * @param array $error_messages |
||
41 | * @return void |
||
42 | */ |
||
43 | public function __construct($options = array(), $error_messages = array()) |
||
44 | { |
||
45 | $this->options = array( |
||
46 | // Upload directory path: |
||
47 | 'upload_dir' => dirname(__FILE__) . '/files/', |
||
48 | |||
49 | // Upload directory url: |
||
50 | 'upload_url' => $this->getFullUrl() . '/files/', |
||
51 | |||
52 | // Accepted file types: |
||
53 | 'accept_file_types' => 'png|jpg|jpeg|gif', |
||
54 | |||
55 | // Directory mode: |
||
56 | 'mkdir_mode' => 0755, |
||
57 | |||
58 | // File size restrictions (in bytes): |
||
59 | 'max_file_size' => null, |
||
60 | 'min_file_size' => 1, |
||
61 | |||
62 | // Image resolution restrictions (in px): |
||
63 | 'max_width' => null, |
||
64 | 'max_height' => null, |
||
65 | 'min_width' => 1, |
||
66 | 'min_height' => 1, |
||
67 | |||
68 | // Auto orient image based on EXIF data: |
||
69 | 'auto_orient' => true, |
||
70 | |||
71 | // Image versions |
||
72 | 'versions' => array( |
||
73 | //'' => array( |
||
74 | //'upload_dir' => '', |
||
75 | //'upload_url' => '', |
||
76 | // Create square images |
||
77 | // 'crop' => true, |
||
78 | // 'max_width' => 200, |
||
79 | // 'max_height' => 200, |
||
80 | //), |
||
81 | |||
82 | // 'avatar' => array( |
||
83 | // 'crop' => true, |
||
84 | // 'max_width' => 200, |
||
85 | // 'max_height' => 200 |
||
86 | // ), |
||
87 | |||
88 | // 'small' => array( |
||
89 | // 'crop' => true, |
||
90 | // 'max_width' => 100 |
||
91 | // ) |
||
92 | ), |
||
93 | ); |
||
94 | |||
95 | $this->options = $options + $this->options; |
||
96 | $this->error_messages = $error_messages + $this->error_messages; |
||
97 | |||
98 | $this->initialize(); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Initialize upload and crop actions |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | protected function initialize() |
||
107 | { |
||
108 | // GD extension check |
||
109 | // http://www.php.net/manual/en/book.image.php |
||
110 | if (!extension_loaded('gd') || !function_exists('gd_info')) { |
||
111 | $this->error = $this->getErrorMessage('gd'); |
||
|
|||
112 | |||
113 | return false; |
||
114 | } |
||
115 | |||
116 | switch (@$_POST['action']) { |
||
117 | case 'upload': |
||
118 | $this->upload(); |
||
119 | break; |
||
120 | |||
121 | case 'crop': |
||
122 | $this->crop(); |
||
123 | break; |
||
124 | |||
125 | case 'delete': |
||
126 | $this->delete(); |
||
127 | break; |
||
128 | } |
||
129 | |||
130 | switch (@$_GET['action']) { |
||
131 | case 'preview': |
||
132 | $this->preview(); |
||
133 | break; |
||
134 | |||
135 | case 'load': |
||
136 | $this->load(); |
||
137 | break; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Load action |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | protected function load() |
||
147 | { |
||
148 | if (!isset($this->options['load'])) { |
||
149 | return; |
||
150 | } |
||
151 | |||
152 | $files = call_user_func($this->options['load'], $this); |
||
153 | |||
154 | if (!$files) { |
||
155 | return; |
||
156 | } |
||
157 | |||
158 | if (!is_array($files)) { |
||
159 | $files = array($files); |
||
160 | $single = true; |
||
161 | } |
||
162 | |||
163 | $images = array(); |
||
164 | |||
165 | foreach ($files as $file) { |
||
166 | $image = new stdClass(); |
||
167 | $image->path = $this->getUploadPath($file); |
||
168 | |||
169 | if (!file_exists($image->path)) { |
||
170 | continue; |
||
171 | } |
||
172 | |||
173 | $image->name = $file; |
||
174 | $image->type = $this->getFileExtension($image->name); |
||
175 | $image->url = $this->getUploadUrl($image->name); |
||
176 | |||
177 | list($image->width, $image->height) = @getimagesize($image->path); |
||
178 | |||
179 | foreach ($this->options['versions'] as $version => $options) { |
||
180 | $filename = $this->getVersionFilename($image->name, $version); |
||
181 | $filepath = $this->getUploadPath($filename, $version); |
||
182 | |||
183 | list($width, $height) = @getimagesize($filepath); |
||
184 | |||
185 | $image->versions[$version] = array( |
||
186 | 'url' => $this->getUploadUrl($filename, $version), |
||
187 | 'width' => $width, |
||
188 | 'height' => $height, |
||
189 | ); |
||
190 | } |
||
191 | |||
192 | unset($image->path); |
||
193 | |||
194 | if (isset($single)) { |
||
195 | $images = $image; |
||
196 | } else { |
||
197 | $images[] = $image; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | $this->generateResponse($images); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Preview action |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | protected function preview() |
||
210 | { |
||
211 | $filename = basename(@$_GET['file']); |
||
212 | $width = @$_GET['width']; |
||
213 | $rotate = @$_GET['rotate']; |
||
214 | |||
215 | $filepath = $this->getUploadPath($filename); |
||
216 | $filetype = $this->getFileExtension($filename); |
||
217 | |||
218 | if (file_exists($filepath)) { |
||
219 | list($src_w, $src_h) = @getimagesize($filepath); |
||
220 | |||
221 | $dst_w = $src_w; |
||
222 | $dst_h = $src_h; |
||
223 | |||
224 | if (is_numeric($width) && $width > 0) { |
||
225 | $dst_w = $width; |
||
226 | $dst_h = $src_h / $src_w * $width; |
||
227 | } |
||
228 | |||
229 | $dst_path = $this->getUploadPath(md5($filename) . '.' . $filetype); |
||
230 | |||
231 | $this->resizeImage($filepath, $dst_path, 0, 0, $dst_w, $dst_h, $src_w, $src_h); |
||
232 | |||
233 | if (in_array(abs($rotate), array(90, 180, 270))) { |
||
234 | $angle = ($rotate < 0) ? abs($rotate) : 360 - $rotate; |
||
235 | $this->rotateImage($dst_path, $angle); |
||
236 | } |
||
237 | |||
238 | header('Content-Type: image/jpeg'); |
||
239 | header('Content-Length: ' . filesize($dst_path)); |
||
240 | readfile($dst_path); |
||
241 | @unlink($dst_path); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Delete action |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | protected function delete() |
||
251 | { |
||
252 | if (!isset($this->options['delete'])) { |
||
253 | return; |
||
254 | } |
||
255 | |||
256 | $filename = basename(@$_POST['file']); |
||
257 | $filepath = $this->getUploadPath($filename); |
||
258 | |||
259 | if (file_exists($filepath) && call_user_func($this->options['delete'], $filename, $this)) { |
||
260 | foreach ($this->options['versions'] as $version => $options) { |
||
261 | $name = $this->getVersionFilename($filename, $version); |
||
262 | $path = $this->getUploadPath($name, $version); |
||
263 | } |
||
264 | @unlink($filepath); |
||
265 | } |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Upload action |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | protected function upload() |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Handle file upload |
||
289 | * |
||
290 | * @param string $uploaded_file |
||
291 | * @param string $name |
||
292 | * @param integer $size |
||
293 | * @param integer $error |
||
294 | * @return stdClass |
||
295 | */ |
||
296 | protected function handleFileUpload($uploaded_file, $name, $size, $error) |
||
297 | { |
||
298 | $image = new stdClass(); |
||
299 | $image->name = $this->getFilename($name); |
||
300 | $image->type = $this->getFileExtension($name); |
||
301 | $image->size = $this->fixIntOverflow(intval($size)); |
||
302 | $image->path = $this->getUploadPath($image->name); |
||
303 | $image->url = $this->getUploadUrl($image->name); |
||
304 | list($image->width, $image->height) = @getimagesize($uploaded_file); |
||
305 | |||
306 | if (!$this->validate($uploaded_file, $image, $error)) { |
||
307 | return $image; |
||
308 | } |
||
309 | |||
310 | $upload_dir = $this->getUploadPath(); |
||
311 | if (!is_dir($upload_dir)) { |
||
312 | mkdir($upload_dir, $this->options['mkdir_mode'], true); |
||
313 | } |
||
314 | |||
315 | //Upload start callback |
||
316 | if (isset($this->options['upload_start'])) { |
||
317 | call_user_func($this->options['upload_start'], $image, $this); |
||
318 | } |
||
319 | |||
320 | $image->path = $this->getUploadPath($image->name); |
||
321 | $image->url = $this->getUploadUrl($image->name); |
||
322 | |||
323 | if (!move_uploaded_file($uploaded_file, $image->path)) { |
||
324 | $image->error = $this->getErrorMessage('move_failed'); |
||
325 | |||
326 | return $image; |
||
327 | } |
||
328 | |||
329 | // Orient the image |
||
330 | if (!empty($this->options['auto_orient'])) { |
||
331 | $this->orientImage($image->path); |
||
332 | } |
||
333 | |||
334 | // Generate image versions |
||
335 | $image->versions = $this->generateVersions($image, true); |
||
336 | |||
337 | // Upload complete callback |
||
338 | if (isset($this->options['upload_complete'])) { |
||
339 | call_user_func($this->options['upload_complete'], $image, $this); |
||
340 | } |
||
341 | |||
342 | unset($image->path); |
||
343 | |||
344 | return $image; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Crop action |
||
349 | * |
||
350 | * @return void |
||
351 | */ |
||
352 | protected function crop() |
||
353 | { |
||
354 | $filename = basename(@$_POST['image']); |
||
355 | $rotate = @$_POST['rotate']; |
||
356 | |||
357 | $image = new stdClass(); |
||
358 | $image->name = $filename; |
||
359 | $image->type = $this->getFileExtension($image->name); |
||
360 | $image->path = $this->getUploadPath($image->name); |
||
361 | $image->url = $this->getUploadUrl($image->name); |
||
362 | |||
363 | if (!file_exists($image->path)) { |
||
364 | return $this->generateResponse(array('error' => $this->getErrorMessage('not_exists'))); |
||
365 | } |
||
366 | |||
367 | if (!preg_match('/.(' . $this->options['accept_file_types'] . ')+$/i', $image->name)) { |
||
368 | return; |
||
369 | } |
||
370 | |||
371 | list($image->width, $image->height) = @getimagesize($image->path); |
||
372 | |||
373 | @list($src_x, $src_y, $x2, $y2, $src_w, $src_h) = @array_values(@$_POST['coords']); |
||
374 | |||
375 | if (isset($this->options['crop_start'])) { |
||
376 | call_user_func($this->options['crop_start'], $image, $this); |
||
377 | } |
||
378 | |||
379 | $image->url = $this->getUploadUrl($image->name); |
||
380 | |||
381 | if (empty($src_w) || empty($src_h)) { |
||
382 | $src_w = $image->width; |
||
383 | $src_h = $image->height; |
||
384 | } |
||
385 | |||
386 | if (empty($src_x) && empty($src_y)) { |
||
387 | $src_x = $src_y = 0; |
||
388 | } |
||
389 | |||
390 | $dst_w = $src_w; |
||
391 | $dst_h = $src_h; |
||
392 | |||
393 | $tmp = clone $image; |
||
394 | $tmp->path = $this->getUploadPath(md5($tmp->name) . '.' . $tmp->type); |
||
395 | |||
396 | @copy($image->path, $tmp->path); |
||
397 | |||
398 | if (in_array(abs($rotate), array(90, 180, 270))) { |
||
399 | $angle = ($rotate < 0) ? abs($rotate) : 360 - $rotate; |
||
400 | $this->rotateImage($tmp->path, $angle); |
||
401 | } |
||
402 | |||
403 | $this->resizeImage($tmp->path, null, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
||
404 | |||
405 | list($tmp->width, $tmp->height) = @getimagesize($tmp->path); |
||
406 | |||
407 | $image->versions = $this->generateVersions($tmp); |
||
408 | |||
409 | @unlink($tmp->path); |
||
410 | |||
411 | if (!isset($this->options['versions'][''])) { |
||
412 | @rename($image->path, $this->getUploadPath($image->name)); |
||
413 | } |
||
414 | |||
415 | list($image->width, $image->height) = @getimagesize($this->getUploadPath($image->name)); |
||
416 | |||
417 | if ($image->path != $this->getUploadPath($image->name)) { |
||
418 | foreach ($this->options['versions'] as $version => $options) { |
||
419 | $filename = $this->getVersionFilename(basename($image->path), $version); |
||
420 | @unlink($this->getUploadPath($filename, $version)); |
||
421 | } |
||
422 | } |
||
423 | |||
424 | // Crop complete callback |
||
425 | if (isset($this->options['crop_complete'])) { |
||
426 | call_user_func($this->options['crop_complete'], $image, $this); |
||
427 | } |
||
428 | |||
429 | unset($image->path); |
||
430 | |||
431 | // Generate json response |
||
432 | $this->generateResponse($image); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Generate image versions |
||
437 | * |
||
438 | * @param stdClass $image |
||
439 | * @param bool $is_upload |
||
440 | * @return array |
||
441 | */ |
||
442 | protected function generateVersions($image, $is_upload = false) |
||
443 | { |
||
444 | $versions = array(); |
||
445 | foreach ($this->options['versions'] as $version => $options) { |
||
446 | $dst_w = $src_w = $image->width; |
||
447 | $dst_h = $src_h = $image->height; |
||
448 | $src_x = $src_y = 0; |
||
449 | |||
450 | $max_width = @$options['max_width']; |
||
451 | $max_height = @$options['max_height']; |
||
452 | $crop = isset($options['crop']) && $options['crop'] === true; |
||
453 | |||
454 | if ($crop) { |
||
455 | $min = min($src_w, $src_h); |
||
456 | $src_x = ($src_w - $min) / 2; |
||
457 | $src_y = ($src_h - $min) / 2; |
||
458 | $dst_w = $dst_h = $src_w = $src_h = $min; |
||
459 | } |
||
460 | |||
461 | if (!empty($max_width) && $src_w > $max_width || ($src_w < $max_width && $crop)) { |
||
462 | $dst_w = $max_width; |
||
463 | $dst_h = $src_h / $src_w * $max_width; |
||
464 | } else { |
||
465 | if (!empty($max_height) && $src_h > $max_height || ($src_h < $max_height && $crop)) { |
||
466 | $dst_h = $max_height; |
||
467 | $dst_w = $src_w / $src_h * $max_height; |
||
468 | } |
||
469 | } |
||
470 | |||
471 | $filename = $this->getVersionFilename($image->name, $version); |
||
472 | $filepath = $this->getUploadPath($filename, $version); |
||
473 | $upload_dir = $this->getUploadPath('', $version); |
||
474 | |||
475 | if (!is_dir($upload_dir)) { |
||
476 | mkdir($upload_dir, $this->options['mkdir_mode'], true); |
||
477 | } |
||
478 | |||
479 | if (!$is_upload || ($is_upload && $version != '')) { |
||
480 | // var_dump($image->path, $filepath, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
||
481 | $success = $this->resizeImage($image->path, $filepath, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
||
482 | } |
||
483 | |||
484 | if (!empty($success)) { |
||
485 | $versions[$version] = array( |
||
486 | 'url' => $this->getUploadUrl($filename, $version), |
||
487 | 'width' => $dst_w, |
||
488 | 'height' => $dst_h, |
||
489 | ); |
||
490 | } |
||
491 | } |
||
492 | |||
493 | return $versions; |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Validate uploaded file |
||
498 | * |
||
499 | * @param string $uploaded_file |
||
500 | * @param stdClass $name |
||
501 | * @param string $error |
||
502 | * @return boolean |
||
503 | */ |
||
504 | protected function validate($uploaded_file, $file, $error) |
||
505 | { |
||
506 | if (!$uploaded_file) { |
||
507 | $file->error = $this->getErrorMessage(4); |
||
508 | |||
509 | return false; |
||
510 | } |
||
511 | |||
512 | if ($error) { |
||
513 | $file->error = $this->getErrorMessage($error); |
||
514 | |||
515 | return false; |
||
516 | } |
||
517 | |||
518 | $content_length = $this->fixIntOverflow(intval($_SERVER['CONTENT_LENGTH'])); |
||
519 | $post_max_size = $this->getConfigBytes(ini_get('post_max_size')); |
||
520 | |||
521 | if ($post_max_size && $content_length > $post_max_size) { |
||
522 | $file->error = $this->getErrorMessage('post_max_size'); |
||
523 | |||
524 | return false; |
||
525 | } |
||
526 | |||
527 | if ($this->options['max_file_size'] && $file->size > $this->options['max_file_size']) { |
||
528 | $file->error = $this->getErrorMessage('max_file_size'); |
||
529 | |||
530 | return false; |
||
531 | } |
||
532 | |||
533 | if ($this->options['min_file_size'] && $file->size < $this->options['min_file_size']) { |
||
534 | $file->error = $this->getErrorMessage('min_file_size'); |
||
535 | |||
536 | return false; |
||
537 | } |
||
538 | |||
539 | if (!preg_match('/.(' . $this->options['accept_file_types'] . ')+$/i', $file->name)) { |
||
540 | $file->error = $this->getErrorMessage('accept_file_types'); |
||
541 | |||
542 | return false; |
||
543 | } |
||
544 | |||
545 | if (empty($file->width) || empty($file->height)) { |
||
546 | $file->error = $this->getErrorMessage('invalid_image'); |
||
547 | |||
548 | return false; |
||
549 | } |
||
550 | |||
551 | $max_width = @$this->options['max_width']; |
||
552 | $max_height = @$this->options['max_height']; |
||
553 | $min_width = @$this->options['min_width']; |
||
554 | $min_height = @$this->options['min_height']; |
||
555 | |||
556 | if ($max_width || $max_height || $min_width || $min_height) { |
||
557 | if ($max_width && $file->width > $max_width) { |
||
558 | $file->error = $this->getErrorMessage('max_width') . $max_width . 'px'; |
||
559 | |||
560 | return false; |
||
561 | } |
||
562 | if ($max_height && $file->height > $max_height) { |
||
563 | $file->error = $this->getErrorMessage('max_height') . $max_height . 'px'; |
||
564 | |||
565 | return false; |
||
566 | } |
||
567 | if ($min_width && $file->width < $min_width) { |
||
568 | $file->error = $this->getErrorMessage('min_width') . $min_width . 'px'; |
||
569 | |||
570 | return false; |
||
571 | } |
||
572 | if ($min_height && $file->height < $min_height) { |
||
573 | $file->error = $this->getErrorMessage('min_height') . $min_height . 'px'; |
||
574 | |||
575 | return false; |
||
576 | } |
||
577 | } |
||
578 | |||
579 | return true; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * Get upload directory path |
||
584 | * |
||
585 | * @param string $filename |
||
586 | * @param string $version |
||
587 | * @return string |
||
588 | */ |
||
589 | public function getUploadPath($filename = '', $version = '') |
||
590 | { |
||
591 | $upload_dir = $this->options['upload_dir']; |
||
592 | |||
593 | if ($version != '') { |
||
594 | $dir = @$this->options['versions'][$version]['upload_dir']; |
||
595 | if (!empty($dir)) { |
||
596 | $upload_dir = $dir; |
||
597 | } |
||
598 | } |
||
599 | |||
600 | return $upload_dir . $filename; |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * Get upload directory url |
||
605 | * |
||
606 | * @param string $filename |
||
607 | * @param string $version |
||
608 | * @return string |
||
609 | */ |
||
610 | public function getUploadUrl($filename = '', $version = '') |
||
611 | { |
||
612 | $upload_url = $this->options['upload_url']; |
||
613 | |||
614 | if ($version != '') { |
||
615 | $url = @$this->options['versions'][$version]['upload_url']; |
||
616 | if (!empty($url)) { |
||
617 | $upload_url = $url; |
||
618 | } |
||
619 | } |
||
620 | |||
621 | return $upload_url . $filename; |
||
622 | } |
||
623 | |||
624 | /** |
||
625 | * Get full url |
||
626 | * |
||
627 | * @return string |
||
628 | */ |
||
629 | protected function getFullUrl() |
||
630 | { |
||
631 | $https = !empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'on') === 0; |
||
632 | |||
633 | return |
||
634 | ($https ? 'https://' : 'http://') . |
||
635 | (!empty($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] . '@' : '') . |
||
636 | (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'] . |
||
637 | ($https && $_SERVER['SERVER_PORT'] === 443 || |
||
638 | $_SERVER['SERVER_PORT'] === 80 ? '' : ':' . $_SERVER['SERVER_PORT']))) . |
||
639 | substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/')); |
||
640 | } |
||
641 | |||
642 | /** |
||
643 | * Get file name |
||
644 | * |
||
645 | * @param string |
||
646 | * @return string |
||
647 | */ |
||
648 | public function getFilename($name) |
||
649 | { |
||
650 | return $this->getUniqueFilename($name); |
||
651 | } |
||
652 | |||
653 | /** |
||
654 | * Get version name |
||
655 | * |
||
656 | * @param string |
||
657 | * @return string |
||
658 | */ |
||
659 | public function getVersionFilename($filename, $version) |
||
660 | { |
||
661 | $ext = $this->getFileExtension($filename); |
||
662 | if ($version == '') { |
||
663 | return $filename; |
||
664 | } |
||
665 | |||
666 | return str_replace('.' . $ext, "-$version.$ext", $filename); |
||
667 | } |
||
668 | |||
669 | /** |
||
670 | * Get unique file name |
||
671 | * |
||
672 | * @param string |
||
673 | * @return string |
||
674 | */ |
||
675 | public function getUniqueFilename($name) |
||
676 | { |
||
677 | while (is_dir($this->getUploadPath($name))) { |
||
678 | $name = $this->upcountName($name); |
||
679 | } |
||
680 | while (is_file($this->getUploadPath($name))) { |
||
681 | $name = $this->upcountName($name); |
||
682 | } |
||
683 | |||
684 | return $name; |
||
685 | } |
||
686 | |||
687 | /** |
||
688 | * Generate random file name |
||
689 | * |
||
690 | * @return string |
||
691 | */ |
||
692 | public function getRandFilename() |
||
693 | { |
||
694 | return md5(time() . rand()); |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * Get file extension |
||
699 | * |
||
700 | * @param string $filename |
||
701 | * @return string |
||
702 | */ |
||
703 | public function getFileExtension($filename) |
||
704 | { |
||
705 | return pathinfo(strtolower($filename), PATHINFO_EXTENSION); |
||
706 | } |
||
707 | |||
708 | /** |
||
709 | * Generate json response |
||
710 | * |
||
711 | * @param array $response |
||
712 | * @return string |
||
713 | */ |
||
714 | public function generateResponse($response) |
||
715 | { |
||
716 | echo json_encode($response); |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * Get error message |
||
721 | * |
||
722 | * @param string $error |
||
723 | * @return string |
||
724 | */ |
||
725 | public function getErrorMessage($error) |
||
726 | { |
||
727 | return array_key_exists($error, $this->error_messages) ? |
||
728 | $this->error_messages[$error] : $error; |
||
729 | } |
||
730 | |||
731 | /** |
||
732 | * Resize image |
||
733 | * |
||
734 | * @param string $src_path Source image path |
||
735 | * @param string|null $dst_path Destination image path |
||
736 | * @param integer $src_x x-coordinate of source point |
||
737 | * @param integer $src_y y-coordinate of source point |
||
738 | * @param integer $dst_w Destination width |
||
739 | * @param integer $dst_h Destination height |
||
740 | * @param integer $src_w Source width |
||
741 | * @param integer $src_h Source height |
||
742 | * @return bool |
||
743 | */ |
||
744 | public function resizeImage($src_path, $dst_path = null, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) |
||
796 | } |
||
797 | } |
||
798 | |||
799 | /** |
||
800 | * Rotate image |
||
801 | * |
||
802 | * @param string $src_path |
||
803 | * @param integer $angle |
||
804 | * @return void |
||
805 | */ |
||
806 | public function rotateImage($src_path, $angle) |
||
840 | } |
||
841 | |||
842 | /** |
||
843 | * Orient image based on EXIF orientation data |
||
844 | * |
||
845 | * @param string $filepath |
||
846 | * @return void |
||
847 | */ |
||
848 | protected function orientImage($filepath) |
||
870 | } |
||
871 | } |
||
872 | } |
||
873 | |||
874 | protected function upcountName($name) |
||
875 | { |
||
876 | return preg_replace_callback( |
||
877 | '/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', |
||
878 | array($this, 'upcountNameCallback'), |
||
879 | $name, |
||
880 | 1 |
||
881 | ); |
||
882 | } |
||
883 | |||
884 | protected function upcountNameCallback($matches) |
||
890 | } |
||
891 | |||
892 | protected function getConfigBytes($val) |
||
893 | { |
||
894 | $val = trim($val); |
||
895 | $last = strtolower($val[strlen($val) - 1]); |
||
896 | switch ($last) { |
||
897 | case 'g': |
||
898 | $val *= 1024; |
||
899 | // no break |
||
900 | case 'm': |
||
901 | $val *= 1024; |
||
902 | // no break |
||
903 | case 'k': |
||
904 | $val *= 1024; |
||
905 | } |
||
906 | |||
907 | return $this->fixIntOverflow($val); |
||
908 | } |
||
909 | |||
910 | protected function fixIntOverflow($size) |
||
917 | } |
||
918 | } |
||
919 |