|
1 | 1 | <?php
|
2 | 2 | /*
|
3 |
| - * jQuery File Upload Plugin PHP Class 6.2 |
| 3 | + * jQuery File Upload Plugin PHP Class 6.4.1 |
4 | 4 | * https://github.com/blueimp/jQuery-File-Upload
|
5 | 5 | *
|
6 | 6 | * Copyright 2010, Sebastian Tschan
|
@@ -102,6 +102,9 @@ function __construct($options = null, $initialize = true, $error_messages = null
|
102 | 102 | ),
|
103 | 103 | */
|
104 | 104 | 'thumbnail' => array(
|
| 105 | + // Uncomment the following to force the max |
| 106 | + // dimensions and e.g. create square thumbnails: |
| 107 | + //'crop' => true, |
105 | 108 | 'max_width' => 80,
|
106 | 109 | 'max_height' => 80
|
107 | 110 | )
|
@@ -281,23 +284,48 @@ protected function create_scaled_image($file_name, $version, $options) {
|
281 | 284 | } else {
|
282 | 285 | $new_file_path = $file_path;
|
283 | 286 | }
|
| 287 | + if (!function_exists('getimagesize')) { |
| 288 | + error_log('Function not found: getimagesize'); |
| 289 | + return false; |
| 290 | + } |
284 | 291 | list($img_width, $img_height) = @getimagesize($file_path);
|
285 | 292 | if (!$img_width || !$img_height) {
|
286 | 293 | return false;
|
287 | 294 | }
|
| 295 | + $max_width = $options['max_width']; |
| 296 | + $max_height = $options['max_height']; |
288 | 297 | $scale = min(
|
289 |
| - $options['max_width'] / $img_width, |
290 |
| - $options['max_height'] / $img_height |
| 298 | + $max_width / $img_width, |
| 299 | + $max_height / $img_height |
291 | 300 | );
|
292 | 301 | if ($scale >= 1) {
|
293 | 302 | if ($file_path !== $new_file_path) {
|
294 | 303 | return copy($file_path, $new_file_path);
|
295 | 304 | }
|
296 | 305 | return true;
|
297 | 306 | }
|
298 |
| - $new_width = $img_width * $scale; |
299 |
| - $new_height = $img_height * $scale; |
300 |
| - $new_img = @imagecreatetruecolor($new_width, $new_height); |
| 307 | + if (!function_exists('imagecreatetruecolor')) { |
| 308 | + error_log('Function not found: imagecreatetruecolor'); |
| 309 | + return false; |
| 310 | + } |
| 311 | + if (empty($options['crop'])) { |
| 312 | + $new_width = $img_width * $scale; |
| 313 | + $new_height = $img_height * $scale; |
| 314 | + $dst_x = 0; |
| 315 | + $dst_y = 0; |
| 316 | + $new_img = @imagecreatetruecolor($new_width, $new_height); |
| 317 | + } else { |
| 318 | + if (($img_width / $img_height) >= ($max_width / $max_height)) { |
| 319 | + $new_width = $img_width / ($img_height / $max_height); |
| 320 | + $new_height = $max_height; |
| 321 | + } else { |
| 322 | + $new_width = $max_width; |
| 323 | + $new_height = $img_height / ($img_width / $max_width); |
| 324 | + } |
| 325 | + $dst_x = 0 - ($new_width - $max_width) / 2; |
| 326 | + $dst_y = 0 - ($new_height - $max_height) / 2; |
| 327 | + $new_img = @imagecreatetruecolor($max_width, $max_height); |
| 328 | + } |
301 | 329 | switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
|
302 | 330 | case 'jpg':
|
303 | 331 | case 'jpeg':
|
@@ -327,7 +355,10 @@ protected function create_scaled_image($file_name, $version, $options) {
|
327 | 355 | $success = $src_img && @imagecopyresampled(
|
328 | 356 | $new_img,
|
329 | 357 | $src_img,
|
330 |
| - 0, 0, 0, 0, |
| 358 | + $dst_x, |
| 359 | + $dst_y, |
| 360 | + 0, |
| 361 | + 0, |
331 | 362 | $new_width,
|
332 | 363 | $new_height,
|
333 | 364 | $img_width,
|
@@ -511,6 +542,38 @@ protected function orient_image($file_path) {
|
511 | 542 | return $success;
|
512 | 543 | }
|
513 | 544 |
|
| 545 | + protected function handle_image_file($file_path, $file) { |
| 546 | + if ($this->options['orient_image']) { |
| 547 | + $this->orient_image($file_path); |
| 548 | + } |
| 549 | + $failed_versions = array(); |
| 550 | + foreach($this->options['image_versions'] as $version => $options) { |
| 551 | + if ($this->create_scaled_image($file->name, $version, $options)) { |
| 552 | + if (!empty($version)) { |
| 553 | + $file->{$version.'_url'} = $this->get_download_url( |
| 554 | + $file->name, |
| 555 | + $version |
| 556 | + ); |
| 557 | + } else { |
| 558 | + $file->size = $this->get_file_size($file_path, true); |
| 559 | + } |
| 560 | + } else { |
| 561 | + $failed_versions[] = $version; |
| 562 | + } |
| 563 | + } |
| 564 | + switch (count($failed_versions)) { |
| 565 | + case 0: |
| 566 | + break; |
| 567 | + case 1: |
| 568 | + $file->error = 'Failed to create scaled version: ' |
| 569 | + .$failed_versions[0]; |
| 570 | + break; |
| 571 | + default: |
| 572 | + $file->error = 'Failed to create scaled versions: ' |
| 573 | + .implode($failed_versions,', '); |
| 574 | + } |
| 575 | + } |
| 576 | + |
514 | 577 | protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
|
515 | 578 | $index = null, $content_range = null) {
|
516 | 579 | $file = new stdClass();
|
@@ -547,27 +610,18 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
|
547 | 610 | }
|
548 | 611 | $file_size = $this->get_file_size($file_path, $append_file);
|
549 | 612 | if ($file_size === $file->size) {
|
550 |
| - if ($this->options['orient_image']) { |
551 |
| - $this->orient_image($file_path); |
552 |
| - } |
553 | 613 | $file->url = $this->get_download_url($file->name);
|
554 |
| - foreach($this->options['image_versions'] as $version => $options) { |
555 |
| - if ($this->create_scaled_image($file->name, $version, $options)) { |
556 |
| - if (!empty($version)) { |
557 |
| - $file->{$version.'_url'} = $this->get_download_url( |
558 |
| - $file->name, |
559 |
| - $version |
560 |
| - ); |
561 |
| - } else { |
562 |
| - $file_size = $this->get_file_size($file_path, true); |
563 |
| - } |
564 |
| - } |
| 614 | + list($img_width, $img_height) = @getimagesize($file_path); |
| 615 | + if (is_int($img_width)) { |
| 616 | + $this->handle_image_file($file_path, $file); |
| 617 | + } |
| 618 | + } else { |
| 619 | + $file->size = $file_size; |
| 620 | + if (!$content_range && $this->options['discard_aborted_uploads']) { |
| 621 | + unlink($file_path); |
| 622 | + $file->error = 'abort'; |
565 | 623 | }
|
566 |
| - } else if (!$content_range && $this->options['discard_aborted_uploads']) { |
567 |
| - unlink($file_path); |
568 |
| - $file->error = 'abort'; |
569 | 624 | }
|
570 |
| - $file->size = $file_size; |
571 | 625 | $this->set_file_delete_properties($file);
|
572 | 626 | }
|
573 | 627 | return $file;
|
|
0 commit comments