Skip to content
Browse files

Replaced Image Processing plugin with a more generic File Processing …

…plugin.

The File Processing plugin exposes the "process" option, which allows
to define a list of process actions to apply to the selected files.
  • Loading branch information...
1 parent 3245d0a commit 12ca7c40d065bb37d8999363da115bc52b86909f @blueimp committed
Showing with 267 additions and 195 deletions.
  1. +4 −4 README.md
  2. +4 −4 index.html
  3. +219 −0 js/jquery.fileupload-fp.js
  4. +0 −160 js/jquery.fileupload-ip.js
  5. +18 −18 js/jquery.fileupload-ui.js
  6. +16 −3 js/main.js
  7. +3 −3 package.json
  8. +3 −3 test/index.html
View
8 README.md
@@ -43,11 +43,11 @@
## Requirements
* [jQuery](http://jquery.com/) v. 1.6+
-* [jQuery UI widget factory](http://wiki.jqueryui.com/w/page/12138135/Widget%20factory) v. 1.8.16+
+* [jQuery UI widget factory](http://wiki.jqueryui.com/w/page/12138135/Widget%20factory) v. 1.8+
* [jQuery Iframe Transport plugin](https://github.com/blueimp/jQuery-File-Upload/blob/master/jquery.iframe-transport.js) (included)
-* [JavaScript Templates engine](https://github.com/blueimp/JavaScript-Templates) v. 1.0.2+ (optional)
-* [JavaScript Load Image function](https://github.com/blueimp/JavaScript-Load-Image) v. 1.1.4+ (optional)
-* [JavaScript Canvas to Blob function](https://github.com/blueimp/JavaScript-Canvas-to-Blob) v. 1.0.1+ (optional)
+* [JavaScript Templates engine](https://github.com/blueimp/JavaScript-Templates) v. 2.1.0+ (optional)
+* [JavaScript Load Image function](https://github.com/blueimp/JavaScript-Load-Image) v. 1.1.6+ (optional)
+* [JavaScript Canvas to Blob function](https://github.com/blueimp/JavaScript-Canvas-to-Blob) v. 2.0.0+ (optional)
* [Bootstrap CSS Toolkit](https://github.com/twitter/bootstrap/) v. 2.0+ (optional)
The jQuery UI widget factory is a requirement for the basic File Upload plugin, but very lightweight without any other dependencies.
View
8 index.html
@@ -1,7 +1,7 @@
<!DOCTYPE HTML>
<!--
/*
- * jQuery File Upload Plugin Demo 6.5.3
+ * jQuery File Upload Plugin Demo 6.7
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
@@ -98,7 +98,7 @@
</div>
</div>
</div>
- <!-- The loading indicator is shown during image processing -->
+ <!-- The loading indicator is shown during file processing -->
<div class="fileupload-loading"></div>
<br>
<!-- The table listing the files available for upload/download -->
@@ -219,8 +219,8 @@ <h3 class="modal-title"></h3>
<script src="js/jquery.iframe-transport.js"></script>
<!-- The basic File Upload plugin -->
<script src="js/jquery.fileupload.js"></script>
-<!-- The File Upload image processing plugin -->
-<script src="js/jquery.fileupload-ip.js"></script>
+<!-- The File Upload file processing plugin -->
+<script src="js/jquery.fileupload-fp.js"></script>
<!-- The File Upload user interface plugin -->
<script src="js/jquery.fileupload-ui.js"></script>
<!-- The localization script -->
View
219 js/jquery.fileupload-fp.js
@@ -0,0 +1,219 @@
+/*
+ * jQuery File Upload File Processing Plugin 1.0
+ * https://github.com/blueimp/jQuery-File-Upload
+ *
+ * Copyright 2012, Sebastian Tschan
+ * https://blueimp.net
+ *
+ * Licensed under the MIT license:
+ * http://www.opensource.org/licenses/MIT
+ */
+
+/*jslint nomen: true, unparam: true, regexp: true */
+/*global define, window, document */
+
+(function (factory) {
+ 'use strict';
+ if (typeof define === 'function' && define.amd) {
+ // Register as an anonymous AMD module:
+ define([
+ 'jquery',
+ 'load-image',
+ 'canvas-to-blob',
+ './jquery.fileupload'
+ ], factory);
+ } else {
+ // Browser globals:
+ factory(
+ window.jQuery,
+ window.loadImage
+ );
+ }
+}(function ($, loadImage) {
+ 'use strict';
+
+ // The File Upload IP version extends the basic fileupload widget
+ // with file processing functionality:
+ $.widget('blueimpFP.fileupload', $.blueimp.fileupload, {
+
+ options: {
+ // The list of file processing actions:
+ process: [
+ /*
+ {
+ action: 'load',
+ fileTypes: /^image\/(gif|jpeg|png)$/,
+ maxFileSize: 20000000 // 20MB
+ },
+ {
+ action: 'resize',
+ maxWidth: 1920,
+ maxHeight: 1200,
+ minWidth: 800,
+ minHeight: 600
+ },
+ {
+ action: 'save'
+ }
+ */
+ ],
+
+ // The add callback is invoked as soon as files are added to the
+ // fileupload widget (via file input selection, drag & drop or add
+ // API call). See the basic file upload widget for more information:
+ add: function (e, data) {
+ $(this).fileupload('process', data).done(function () {
+ data.submit();
+ });
+ }
+ },
+
+ processActions: {
+ // Loads the image given via data.files and data.index
+ // as canvas element.
+ // Accepts the options fileTypes (regular expression)
+ // and maxFileSize (integer) to limit the files to load:
+ load: function (data, options) {
+ var that = this,
+ file = data.files[data.index],
+ dfd = $.Deferred();
+ if (window.HTMLCanvasElement &&
+ window.HTMLCanvasElement.prototype.toBlob &&
+ ($.type(options.maxFileSize) !== 'number' ||
+ file.size < options.maxFileSize) &&
+ (!options.fileTypes ||
+ options.fileTypes.test(file.type))) {
+ loadImage(
+ file,
+ function (canvas) {
+ data.canvas = canvas;
+ dfd.resolveWith(that, [data]);
+ },
+ {canvas: true}
+ );
+ } else {
+ dfd.rejectWith(that, [data]);
+ }
+ return dfd.promise();
+ },
+ // Resizes the image given as data.canvas and updates
+ // data.canvas with the resized image.
+ // Accepts the options maxWidth, maxHeight, minWidth and
+ // minHeight to scale the given image:
+ resize: function (data, options) {
+ if (data.canvas) {
+ var canvas = loadImage.scale(data.canvas, options);
+ if (canvas.width !== data.canvas.width ||
+ canvas.height !== data.canvas.height) {
+ data.canvas = canvas;
+ data.processed = true;
+ }
+ }
+ return data;
+ },
+ // Saves the processed image given as data.canvas
+ // inplace at data.index of data.files:
+ save: function (data, options) {
+ // Do nothing if no processing has happened:
+ if (!data.canvas || !data.processed) {
+ return data;
+ }
+ var that = this,
+ file = data.files[data.index],
+ name = file.name,
+ dfd = $.Deferred(),
+ callback = function (blob) {
+ if (!blob.name) {
+ if (file.type === blob.type) {
+ blob.name = file.name;
+ } else if (file.name) {
+ blob.name = file.name.replace(
+ /\..+$/,
+ '.' + blob.type.substr(6)
+ );
+ }
+ }
+ // Store the created blob at the position
+ // of the original file in the files list:
+ data.files[data.index] = blob;
+ dfd.resolveWith(that, [data]);
+ };
+ // Use canvas.mozGetAsFile directly, to retain the filename, as
+ // Gecko doesn't support the filename option for FormData.append:
+ if (data.canvas.mozGetAsFile) {
+ callback(data.canvas.mozGetAsFile(
+ (/^image\/(jpeg|png)$/.test(file.type) && name) ||
+ ((name && name.replace(/\..+$/, '')) ||
+ 'blob') + '.png',
+ file.type
+ ));
+ } else {
+ data.canvas.toBlob(callback, file.type);
+ }
+ return dfd.promise();
+ }
+ },
+
+ // Resizes the file at the given index and stores the created blob at
+ // the original position of the files list, returns a Promise object:
+ _processFile: function (files, index, options) {
+ var that = this,
+ dfd = $.Deferred().resolveWith(that, [{
+ files: files,
+ index: index
+ }]),
+ chain = dfd.promise();
+ that._processing += 1;
+ $.each(options.process, function (i, settings) {
+ chain = chain.pipe(function (data) {
+ return that.processActions[settings.action]
+ .call(this, data, settings);
+ });
+ });
+ chain.always(function () {
+ that._processing -= 1;
+ if (that._processing === 0) {
+ that.element
+ .removeClass('fileupload-processing');
+ }
+ });
+ if (that._processing === 1) {
+ that.element.addClass('fileupload-processing');
+ }
+ return chain;
+ },
+
+ // Processes the files given as files property of the data parameter,
+ // returns a Promise object that allows to bind a done handler, which
+ // will be invoked after processing all files (inplace) is done:
+ process: function (data) {
+ var that = this,
+ options = $.extend({}, this.options, data);
+ if (options.process && options.process.length &&
+ this._isXHRUpload(options)) {
+ $.each(data.files, function (index, file) {
+ that._processingQueue = that._processingQueue.pipe(
+ function () {
+ var dfd = $.Deferred();
+ that._processFile(data.files, index, options)
+ .always(function () {
+ dfd.resolveWith(that);
+ });
+ return dfd.promise();
+ }
+ );
+ });
+ }
+ return this._processingQueue;
+ },
+
+ _create: function () {
+ $.blueimp.fileupload.prototype._create.call(this);
+ this._processing = 0;
+ this._processingQueue = $.Deferred().resolveWith(this)
+ .promise();
+ }
+
+ });
+
+}));
View
160 js/jquery.fileupload-ip.js
@@ -1,160 +0,0 @@
-/*
- * jQuery File Upload Image Processing Plugin 1.0.6
- * https://github.com/blueimp/jQuery-File-Upload
- *
- * Copyright 2012, Sebastian Tschan
- * https://blueimp.net
- *
- * Licensed under the MIT license:
- * http://www.opensource.org/licenses/MIT
- */
-
-/*jslint nomen: true, unparam: true, regexp: true */
-/*global define, window, document */
-
-(function (factory) {
- 'use strict';
- if (typeof define === 'function' && define.amd) {
- // Register as an anonymous AMD module:
- define([
- 'jquery',
- 'load-image',
- 'canvas-to-blob',
- './jquery.fileupload'
- ], factory);
- } else {
- // Browser globals:
- factory(
- window.jQuery,
- window.loadImage,
- window.canvasToBlob
- );
- }
-}(function ($, loadImage, canvasToBlob) {
- 'use strict';
-
- // The File Upload IP version extends the basic fileupload widget
- // with image processing functionality:
- $.widget('blueimpIP.fileupload', $.blueimp.fileupload, {
-
- options: {
- // The regular expression to define which image files are to be
- // resized, given that the browser supports the operation:
- resizeSourceFileTypes: /^image\/(gif|jpeg|png)$/,
- // The maximum file size of images that are to be resized:
- resizeSourceMaxFileSize: 20000000, // 20MB
- // The maximum width of the resized images:
- resizeMaxWidth: undefined,
- // The maximum height of the resized images:
- resizeMaxHeight: undefined,
- // The minimum width of the resized images:
- resizeMinWidth: undefined,
- // The minimum height of the resized images:
- resizeMinHeight: undefined,
-
- // The add callback is invoked as soon as files are added to the fileupload
- // widget (via file input selection, drag & drop or add API call).
- // See the basic file upload widget for more information:
- add: function (e, data) {
- $(this).fileupload('resize', data).done(function () {
- data.submit();
- });
- }
- },
-
- // Resizes the image file at the given index and stores the created blob
- // at the original position of the files list, returns a Promise object:
- _resizeImage: function (files, index, options) {
- var that = this,
- file = files[index],
- deferred = $.Deferred(),
- canvas,
- blob;
- options = options || this.options;
- loadImage(
- file,
- function (img) {
- var width = img.width,
- height = img.height;
- canvas = loadImage.scale(img, {
- maxWidth: options.resizeMaxWidth,
- maxHeight: options.resizeMaxHeight,
- minWidth: options.resizeMinWidth,
- minHeight: options.resizeMinHeight,
- canvas: true
- });
- if (width !== canvas.width || height !== canvas.height) {
- canvasToBlob(canvas, function (blob) {
- if (!blob.name) {
- if (file.type === blob.type) {
- blob.name = file.name;
- } else if (file.name) {
- blob.name = file.name.replace(
- /\..+$/,
- '.' + blob.type.substr(6)
- );
- }
- }
- files[index] = blob;
- deferred.resolveWith(that);
- }, file);
- } else {
- deferred.resolveWith(that);
- }
- }
- );
- return deferred.promise();
- },
-
- // Resizes the images given as files property of the data parameter,
- // returns a Promise object that allows to bind a done handler, which
- // will be invoked after processing all images is done:
- resize: function (data) {
- var that = this,
- options = $.extend({}, this.options, data),
- resizeAll = $.type(options.resizeSourceMaxFileSize) !== 'number',
- isXHRUpload = this._isXHRUpload(options);
- $.each(data.files, function (index, file) {
- if (isXHRUpload && that._resizeSupport &&
- (options.resizeMaxWidth || options.resizeMaxHeight ||
- options.resizeMinWidth || options.resizeMinHeight) &&
- (resizeAll || file.size < options.resizeSourceMaxFileSize) &&
- options.resizeSourceFileTypes.test(file.type)) {
- that._processing += 1;
- if (that._processing === 1) {
- that.element.addClass('fileupload-processing');
- }
- that._processingQueue = that._processingQueue.pipe(function () {
- var deferred = $.Deferred();
- that._resizeImage(
- data.files,
- index,
- options
- ).done(function () {
- that._processing -= 1;
- if (that._processing === 0) {
- that.element
- .removeClass('fileupload-processing');
- }
- deferred.resolveWith(that);
- });
- return deferred.promise();
- });
- }
- });
- return this._processingQueue;
- },
-
- _create: function () {
- $.blueimp.fileupload.prototype._create.call(this);
- this._processing = 0;
- this._processingQueue = $.Deferred().resolveWith(this).promise();
- this._resizeSupport = canvasToBlob && canvasToBlob(
- document.createElement('canvas'),
- $.noop
- );
- }
-
- });
-
-}));
View
36 js/jquery.fileupload-ui.js
@@ -1,5 +1,5 @@
/*
- * jQuery File Upload User Interface Plugin 6.6.5
+ * jQuery File Upload User Interface Plugin 6.7
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
@@ -20,7 +20,7 @@
'jquery',
'tmpl',
'load-image',
- './jquery.fileupload-ip'
+ './jquery.fileupload-fp'
], factory);
} else {
// Browser globals:
@@ -33,9 +33,9 @@
}(function ($, tmpl, loadImage) {
'use strict';
- // The UI version extends the IP (image processing) version or the basic
+ // The UI version extends the FP (file processing) version or the basic
// file upload widget and adds complete user interface interaction:
- var parentWidget = ($.blueimpIP || $.blueimp).fileupload;
+ var parentWidget = ($.blueimpFP || $.blueimp).fileupload;
$.widget('blueimpUI.fileupload', parentWidget, {
options: {
@@ -87,7 +87,7 @@
var that = $(this).data('fileupload'),
options = that.options,
files = data.files;
- $(this).fileupload('resize', data).done(data, function () {
+ $(this).fileupload('process', data).done(function () {
that._adjustMaxNumberOfFiles(-files.length);
data.isAdjusted = true;
data.files.valid = data.isValidated = that._validate(files);
@@ -375,20 +375,20 @@
_renderPreview: function (file, node) {
var that = this,
options = this.options,
- deferred = $.Deferred();
+ dfd = $.Deferred();
return ((loadImage && loadImage(
file,
function (img) {
node.append(img);
that._forceReflow(node);
that._transition(node).done(function () {
- deferred.resolveWith(node);
+ dfd.resolveWith(node);
});
if (!$.contains(document.body, node[0])) {
// If the element is not part of the DOM,
// transition events are not triggered,
// so we have to resolve manually:
- deferred.resolveWith(node);
+ dfd.resolveWith(node);
}
},
{
@@ -396,7 +396,7 @@
maxHeight: options.previewMaxHeight,
canvas: options.previewAsCanvas
}
- )) || deferred.resolveWith(node)) && deferred;
+ )) || dfd.resolveWith(node)) && dfd;
},
_renderPreviews: function (files, nodes) {
@@ -408,13 +408,13 @@
($.type(options.previewSourceMaxFileSize) !== 'number' ||
file.size < options.previewSourceMaxFileSize)) {
that._processingQueue = that._processingQueue.pipe(function () {
- var deferred = $.Deferred();
+ var dfd = $.Deferred();
that._renderPreview(file, $(element)).done(
function () {
- deferred.resolveWith(that);
+ dfd.resolveWith(that);
}
);
- return deferred.promise();
+ return dfd.promise();
});
}
});
@@ -475,7 +475,7 @@
_transition: function (node) {
var that = this,
- deferred = $.Deferred();
+ dfd = $.Deferred();
if ($.support.transition && node.hasClass('fade')) {
node.bind(
$.support.transition.end,
@@ -484,15 +484,15 @@
// in the container element, e.g. from button elements:
if (e.target === node[0]) {
node.unbind($.support.transition.end);
- deferred.resolveWith(node);
+ dfd.resolveWith(node);
}
}
).toggleClass('in');
} else {
node.toggleClass('in');
- deferred.resolveWith(node);
+ dfd.resolveWith(node);
}
- return deferred;
+ return dfd;
},
_initButtonBarEventHandlers: function () {
@@ -617,9 +617,9 @@
'uploadTemplateId',
'downloadTemplateId'
);
- if (!$.blueimpIP) {
+ if (!$.blueimpFP) {
this._processingQueue = $.Deferred().resolveWith(this).promise();
- this.resize = function () {
+ this.process = function () {
return this._processingQueue;
};
}
View
19 js/main.js
@@ -1,5 +1,5 @@
/*
- * jQuery File Upload Plugin JS Example 6.5.1
+ * jQuery File Upload Plugin JS Example 6.7
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
@@ -34,8 +34,21 @@ $(function () {
url: '//jquery-file-upload.appspot.com/',
maxFileSize: 5000000,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
- resizeMaxWidth: 1920,
- resizeMaxHeight: 1200
+ process: [
+ {
+ action: 'load',
+ fileTypes: /^image\/(gif|jpeg|png)$/,
+ maxFileSize: 20000000 // 20MB
+ },
+ {
+ action: 'resize',
+ maxWidth: 1440,
+ maxHeight: 900
+ },
+ {
+ action: 'save'
+ }
+ ]
});
// Upload server status check for browsers with CORS support:
if ($.support.cors) {
View
6 package.json
@@ -1,6 +1,6 @@
{
"name": "blueimp-file-upload",
- "version": "6.5.5",
+ "version": "6.7.0",
"title": "jQuery File Upload",
"description": "File Upload widget with multiple file selection, drag&drop support, progress bars and preview images for jQuery. Supports cross-domain, chunked and resumable file uploads and client-side image resizing. Works with any server-side platform (PHP, Python, Ruby on Rails, Java, Node.js, Go etc.) that supports standard HTML form file uploads.",
"keywords": [
@@ -50,7 +50,7 @@
"jquery": ">=1.6",
"jquery.ui.widget": ">=1.8",
"blueimp-tmpl": ">=2.1.0",
- "blueimp-load-image": ">=1.1.4",
- "blueimp-canvas-to-blob": ">=1.0.1"
+ "blueimp-load-image": ">=1.1.6",
+ "blueimp-canvas-to-blob": ">=2.0.0"
}
}
View
6 test/index.html
@@ -1,7 +1,7 @@
<!DOCTYPE HTML>
<!--
/*
- * jQuery File Upload Plugin Test 6.5.1
+ * jQuery File Upload Plugin Test 6.7
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
@@ -57,7 +57,7 @@ <h2 id="qunit-userAgent"></h2>
</div>
</div>
</div>
- <!-- The loading indicator is shown during image processing -->
+ <!-- The loading indicator is shown during file processing -->
<div class="fileupload-loading"></div>
<br>
<!-- The table listing the files available for upload/download -->
@@ -131,7 +131,7 @@ <h2 id="qunit-userAgent"></h2>
<script src="http://blueimp.github.com/JavaScript-Canvas-to-Blob/canvas-to-blob.min.js"></script>
<script src="../js/jquery.iframe-transport.js"></script>
<script src="../js/jquery.fileupload.js"></script>
-<script src="../js/jquery.fileupload-ip.js"></script>
+<script src="../js/jquery.fileupload-fp.js"></script>
<script src="../js/jquery.fileupload-ui.js"></script>
<script src="../js/locale.js"></script>
<script src="http://code.jquery.com/qunit/git/qunit.js"></script>

0 comments on commit 12ca7c4

Please sign in to comment.
Something went wrong with that request. Please try again.