Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better image resize handling fixes #769 and fixes #785 #787

Merged
merged 2 commits into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions js/fileinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,20 +222,24 @@
}
previewCache.data[id] = data;
},
unset: function (id, index) {
var chk = previewCache.count(id);
unset: function (obj, index) {
var chk = previewCache.count(obj.id);
if (!chk) {
return;
}
if (chk === 1) {
previewCache.data[id].content = [];
previewCache.data[id].config = [];
previewCache.data[id].tags = [];
previewCache.data[obj.id].content = [];
previewCache.data[obj.id].config = [];
previewCache.data[obj.id].tags = [];
obj.initialPreview = [];
obj.initialPreviewConfig = [];
obj.initialPreviewThumbTags = [];
return;
}
previewCache.data[id].content[index] = null;
previewCache.data[id].config[index] = null;
previewCache.data[id].tags[index] = null;

previewCache.data[obj.id].content[index] = null;
previewCache.data[obj.id].config[index] = null;
previewCache.data[obj.id].tags[index] = null;
},
out: function (id) {
var html = '', data = previewCache.data[id], caption, len = previewCache.count(id, true);
Expand Down Expand Up @@ -1386,7 +1390,8 @@
success: function (data, textStatus, jqXHR) {
var n, cap;
if (isEmpty(data) || isEmpty(data.error)) {
previewCache.unset(self.id, index);
previewCache.init(self);
previewCache.unset(self, index);
n = previewCache.count(self.id);
cap = n > 0 ? self._getMsgSelected(n) : '';
self._raise('filedeleted', [vKey, jqXHR, extraData]);
Expand Down Expand Up @@ -2477,29 +2482,38 @@
return;
}
errFunc = self.isUploadable ? self._showUploadError : self._showError;
var counter = {val: 0};
for (i = 0; i < self.loadedImages.length; i++) {
config = self.loadedImages[i];
$img = config.img;
$thumb = config.thumb;
pid = config.pid;
ind = config.ind;
params = {id: pid, 'index': ind};
if (!self._getResizedImage($img[0], config.typ, pid, ind)) {
if (!self._getResizedImage($img[0], config.typ, pid, ind, counter, self.loadedImages.length)) {
errFunc(self.msgImageResizeError, params, 'fileimageresizeerror');
self._setPreviewError($thumb, ind);
}
}
self._raise('fileimagesresized');
},
_getResizedImage: function (image, type, pid, ind) {
_getResizedImage: function (image, type, pid, ind, counter, num_imgs) {
var self = this, width = image.naturalWidth, height = image.naturalHeight, ratio = 1,
maxWidth = self.maxImageWidth || width, maxHeight = self.maxImageHeight || height,
isValidImage = (width && height), chkWidth, chkHeight,
canvas = self.imageCanvas, context = self.imageCanvasContext;
if (!isValidImage) {
counter.val++;
if (counter.val === num_imgs) {
self._raise('fileimagesresized');
}
return false;
}
if (width === maxWidth && height === maxHeight) {
self._raise('fileimageresized', [pid, ind]);
counter.val++;
if (counter.val === num_imgs) {
self._raise('fileimagesresized');
}
return true;
}
type = type || self.resizeDefaultImageType;
Expand All @@ -2518,12 +2532,20 @@
try {
context.drawImage(image, 0, 0, width, height);
canvas.toBlob(function (blob) {
self._raise('fileimageresized', [pid, ind]);
self.filestack[ind] = blob;
self._raise('fileimageresized', [pid, ind]);
counter.val++;
if (counter.val === num_imgs) {
self._raise('fileimagesresized');
}
}, type, self.resizeQuality);
return true;
}
catch (err) {
counter.val++;
if (counter.val === num_imgs) {
self._raise('fileimagesresized');
}
return false;
}
},
Expand Down
Loading