Skip to content

Commit

Permalink
feat: add createThumbnailFromUrl() function
Browse files Browse the repository at this point in the history
  • Loading branch information
enyo committed Feb 9, 2015
1 parent edea505 commit 5ac21c2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 74 deletions.
60 changes: 34 additions & 26 deletions dist/dropzone-amd-module.js
Expand Up @@ -1066,44 +1066,52 @@
fileReader = new FileReader;
fileReader.onload = (function(_this) {
return function() {
var img;
if (file.type === "image/svg+xml") {
_this.emit("thumbnail", file, fileReader.result);
if (callback != null) {
callback();
}
return;
}
img = document.createElement("img");
img.onload = function() {
var canvas, ctx, resizeInfo, thumbnail, _ref, _ref1, _ref2, _ref3;
file.width = img.width;
file.height = img.height;
resizeInfo = _this.options.resize.call(_this, file);
if (resizeInfo.trgWidth == null) {
resizeInfo.trgWidth = resizeInfo.optWidth;
}
if (resizeInfo.trgHeight == null) {
resizeInfo.trgHeight = resizeInfo.optHeight;
}
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = resizeInfo.trgWidth;
canvas.height = resizeInfo.trgHeight;
drawImageIOSFix(ctx, img, (_ref = resizeInfo.srcX) != null ? _ref : 0, (_ref1 = resizeInfo.srcY) != null ? _ref1 : 0, resizeInfo.srcWidth, resizeInfo.srcHeight, (_ref2 = resizeInfo.trgX) != null ? _ref2 : 0, (_ref3 = resizeInfo.trgY) != null ? _ref3 : 0, resizeInfo.trgWidth, resizeInfo.trgHeight);
thumbnail = canvas.toDataURL("image/png");
_this.emit("thumbnail", file, thumbnail);
if (callback != null) {
return callback();
}
};
img.onerror = callback;
return img.src = fileReader.result;
return _this.createThumbnailFromUrl(file, fileReader.result, callback);
};
})(this);
return fileReader.readAsDataURL(file);
};

Dropzone.prototype.createThumbnailFromUrl = function(file, imageUrl, callback) {
var img;
img = document.createElement("img");
img.onload = (function(_this) {
return function() {
var canvas, ctx, resizeInfo, thumbnail, _ref, _ref1, _ref2, _ref3;
file.width = img.width;
file.height = img.height;
resizeInfo = _this.options.resize.call(_this, file);
if (resizeInfo.trgWidth == null) {
resizeInfo.trgWidth = resizeInfo.optWidth;
}
if (resizeInfo.trgHeight == null) {
resizeInfo.trgHeight = resizeInfo.optHeight;
}
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = resizeInfo.trgWidth;
canvas.height = resizeInfo.trgHeight;
drawImageIOSFix(ctx, img, (_ref = resizeInfo.srcX) != null ? _ref : 0, (_ref1 = resizeInfo.srcY) != null ? _ref1 : 0, resizeInfo.srcWidth, resizeInfo.srcHeight, (_ref2 = resizeInfo.trgX) != null ? _ref2 : 0, (_ref3 = resizeInfo.trgY) != null ? _ref3 : 0, resizeInfo.trgWidth, resizeInfo.trgHeight);
thumbnail = canvas.toDataURL("image/png");
_this.emit("thumbnail", file, thumbnail);
if (callback != null) {
return callback();
}
};
})(this);
if (callback != null) {
img.onerror = callback;
}
return img.src = imageUrl;
};

Dropzone.prototype.processQueue = function() {
var i, parallelUploads, processingLength, queuedFiles;
parallelUploads = this.options.parallelUploads;
Expand Down
60 changes: 34 additions & 26 deletions dist/dropzone.js
Expand Up @@ -1054,44 +1054,52 @@
fileReader = new FileReader;
fileReader.onload = (function(_this) {
return function() {
var img;
if (file.type === "image/svg+xml") {
_this.emit("thumbnail", file, fileReader.result);
if (callback != null) {
callback();
}
return;
}
img = document.createElement("img");
img.onload = function() {
var canvas, ctx, resizeInfo, thumbnail, _ref, _ref1, _ref2, _ref3;
file.width = img.width;
file.height = img.height;
resizeInfo = _this.options.resize.call(_this, file);
if (resizeInfo.trgWidth == null) {
resizeInfo.trgWidth = resizeInfo.optWidth;
}
if (resizeInfo.trgHeight == null) {
resizeInfo.trgHeight = resizeInfo.optHeight;
}
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = resizeInfo.trgWidth;
canvas.height = resizeInfo.trgHeight;
drawImageIOSFix(ctx, img, (_ref = resizeInfo.srcX) != null ? _ref : 0, (_ref1 = resizeInfo.srcY) != null ? _ref1 : 0, resizeInfo.srcWidth, resizeInfo.srcHeight, (_ref2 = resizeInfo.trgX) != null ? _ref2 : 0, (_ref3 = resizeInfo.trgY) != null ? _ref3 : 0, resizeInfo.trgWidth, resizeInfo.trgHeight);
thumbnail = canvas.toDataURL("image/png");
_this.emit("thumbnail", file, thumbnail);
if (callback != null) {
return callback();
}
};
img.onerror = callback;
return img.src = fileReader.result;
return _this.createThumbnailFromUrl(file, fileReader.result, callback);
};
})(this);
return fileReader.readAsDataURL(file);
};

Dropzone.prototype.createThumbnailFromUrl = function(file, imageUrl, callback) {
var img;
img = document.createElement("img");
img.onload = (function(_this) {
return function() {
var canvas, ctx, resizeInfo, thumbnail, _ref, _ref1, _ref2, _ref3;
file.width = img.width;
file.height = img.height;
resizeInfo = _this.options.resize.call(_this, file);
if (resizeInfo.trgWidth == null) {
resizeInfo.trgWidth = resizeInfo.optWidth;
}
if (resizeInfo.trgHeight == null) {
resizeInfo.trgHeight = resizeInfo.optHeight;
}
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = resizeInfo.trgWidth;
canvas.height = resizeInfo.trgHeight;
drawImageIOSFix(ctx, img, (_ref = resizeInfo.srcX) != null ? _ref : 0, (_ref1 = resizeInfo.srcY) != null ? _ref1 : 0, resizeInfo.srcWidth, resizeInfo.srcHeight, (_ref2 = resizeInfo.trgX) != null ? _ref2 : 0, (_ref3 = resizeInfo.trgY) != null ? _ref3 : 0, resizeInfo.trgWidth, resizeInfo.trgHeight);
thumbnail = canvas.toDataURL("image/png");
_this.emit("thumbnail", file, thumbnail);
if (callback != null) {
return callback();
}
};
})(this);
if (callback != null) {
img.onerror = callback;
}
return img.src = imageUrl;
};

Dropzone.prototype.processQueue = function() {
var i, parallelUploads, processingLength, queuedFiles;
parallelUploads = this.options.parallelUploads;
Expand Down
47 changes: 25 additions & 22 deletions src/dropzone.coffee
Expand Up @@ -987,37 +987,40 @@ class Dropzone extends Emitter
callback() if callback?
return

# Not using `new Image` here because of a bug in latest Chrome versions.
# See https://github.com/enyo/dropzone/pull/226
img = document.createElement "img"
@createThumbnailFromUrl file, fileReader.result, callback

img.onload = =>
file.width = img.width
file.height = img.height
fileReader.readAsDataURL file

resizeInfo = @options.resize.call @, file
createThumbnailFromUrl: (file, imageUrl, callback) ->
# Not using `new Image` here because of a bug in latest Chrome versions.
# See https://github.com/enyo/dropzone/pull/226
img = document.createElement "img"

resizeInfo.trgWidth ?= resizeInfo.optWidth
resizeInfo.trgHeight ?= resizeInfo.optHeight
img.onload = =>
file.width = img.width
file.height = img.height

canvas = document.createElement "canvas"
ctx = canvas.getContext "2d"
canvas.width = resizeInfo.trgWidth
canvas.height = resizeInfo.trgHeight
resizeInfo = @options.resize.call @, file

# This is a bugfix for iOS' scaling bug.
drawImageIOSFix ctx, img, resizeInfo.srcX ? 0, resizeInfo.srcY ? 0, resizeInfo.srcWidth, resizeInfo.srcHeight, resizeInfo.trgX ? 0, resizeInfo.trgY ? 0, resizeInfo.trgWidth, resizeInfo.trgHeight
resizeInfo.trgWidth ?= resizeInfo.optWidth
resizeInfo.trgHeight ?= resizeInfo.optHeight

thumbnail = canvas.toDataURL "image/png"
canvas = document.createElement "canvas"
ctx = canvas.getContext "2d"
canvas.width = resizeInfo.trgWidth
canvas.height = resizeInfo.trgHeight

@emit "thumbnail", file, thumbnail
callback() if callback?

img.onerror = callback
# This is a bugfix for iOS' scaling bug.
drawImageIOSFix ctx, img, resizeInfo.srcX ? 0, resizeInfo.srcY ? 0, resizeInfo.srcWidth, resizeInfo.srcHeight, resizeInfo.trgX ? 0, resizeInfo.trgY ? 0, resizeInfo.trgWidth, resizeInfo.trgHeight

img.src = fileReader.result
thumbnail = canvas.toDataURL "image/png"

fileReader.readAsDataURL file
@emit "thumbnail", file, thumbnail
callback() if callback?

img.onerror = callback if callback?

img.src = imageUrl


# Goes through the queue and processes files if there aren't too many already.
Expand Down

0 comments on commit 5ac21c2

Please sign in to comment.