Skip to content

Commit

Permalink
Implemented improvements and fixes suggested by Claudio Rumolo:
Browse files Browse the repository at this point in the history
* If scaling factor is >= 1, don't do any image processing.
* Add JPEG/PNG quality parameter option.
* Recalculate the file size if the original file has been replaced by a
scaled version.
  • Loading branch information
blueimp committed Feb 28, 2012
1 parent 3f5c97b commit 35aa4f9
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions server/php/upload.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* jQuery File Upload Plugin PHP Class 5.7
* jQuery File Upload Plugin PHP Class 5.8
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
Expand Down Expand Up @@ -42,7 +42,8 @@ function __construct($options=null) {
'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
'upload_url' => $this->getFullUrl().'/files/',
'max_width' => 1920,
'max_height' => 1200
'max_height' => 1200,
'jpeg_quality' => 95
),
*/
'thumbnail' => array(
Expand Down Expand Up @@ -114,8 +115,11 @@ protected function create_scaled_image($file_name, $options) {
$options['max_width'] / $img_width,
$options['max_height'] / $img_height
);
if ($scale > 1) {
$scale = 1;
if ($scale >= 1) {
if ($file_path !== $new_file_path) {
return copy($file_path, $new_file_path);
}
return true;
}
$new_width = $img_width * $scale;
$new_height = $img_height * $scale;
Expand All @@ -125,21 +129,26 @@ protected function create_scaled_image($file_name, $options) {
case 'jpeg':
$src_img = @imagecreatefromjpeg($file_path);
$write_image = 'imagejpeg';
$image_quality = isset($options['jpeg_quality']) ?
$options['jpeg_quality'] : 75;
break;
case 'gif':
@imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
$src_img = @imagecreatefromgif($file_path);
$write_image = 'imagegif';
$image_quality = null;
break;
case 'png':
@imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
@imagealphablending($new_img, false);
@imagesavealpha($new_img, true);
$src_img = @imagecreatefrompng($file_path);
$write_image = 'imagepng';
$image_quality = isset($options['png_quality']) ?
$options['png_quality'] : 9;
break;
default:
$src_img = $image_method = null;
$src_img = null;
}
$success = $src_img && @imagecopyresampled(
$new_img,
Expand All @@ -149,7 +158,7 @@ protected function create_scaled_image($file_name, $options) {
$new_height,
$img_width,
$img_height
) && $write_image($new_img, $new_file_path);
) && $write_image($new_img, $new_file_path, $image_quality);
// Free up memory (imagedestroy does not delete files):
@imagedestroy($src_img);
@imagedestroy($new_img);
Expand Down Expand Up @@ -257,14 +266,19 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro
}
$file_size = filesize($file_path);
if ($file_size === $file->size) {
if ($this->options['orient_image']) {
$this->orient_image($file_path);
}
if ($this->options['orient_image']) {
$this->orient_image($file_path);
}
$file->url = $this->options['upload_url'].rawurlencode($file->name);
foreach($this->options['image_versions'] as $version => $options) {
if ($this->create_scaled_image($file->name, $options)) {
$file->{$version.'_url'} = $options['upload_url']
.rawurlencode($file->name);
if ($this->options['upload_dir'] !== $options['upload_dir']) {
$file->{$version.'_url'} = $options['upload_url']
.rawurlencode($file->name);
} else {
clearstatcache();
$file_size = filesize($file_path);
}
}
}
} else if ($this->options['discard_aborted_uploads']) {
Expand Down

0 comments on commit 35aa4f9

Please sign in to comment.