I am currently using the bootstrap-wysiwyg rich text editor. This allows users to drag an image file into the editor, which is great, however I would like to resize this image on the fly. I believe I have found the location in bootstrap-wysiwyg.js where I should attempt to resize the image before it gets displayed in the editor and subsequently posted to a PHP page when the user clicks submit.
From reading other stackoverflow posts, it seems that the preferred way of doing this is using HTML5's canvas functionality. I have jQuery available as well. The only browser the solution must support is either the current version of Chrome or Firefox.
I believe I have found a good entry point for this resize functionality:
function (files) {
editor.focus();
$.each(files, function (idx, fileInfo) {
if (/^image\//.test(fileInfo.type)) {
// ENTRYPOINT: attempt to resize
$.when(readFileIntoDataUrl(fileInfo)).done(function (dataUrl) {
execCommand('insertimage', dataUrl);
editor.trigger('image-inserted');
}).fail(function (e) {
options.fileUploadError("file-reader", e);
});
} else {
options.fileUploadError("unsupported-file-type", fileInfo.type);
}
});
},
This above code is pre-existing in the bootstrap-wysiwg library. I'm thinking that I can somehow take the fileInfo object, turn it into an Image object, do the resizing, generate a dataurl version of the image, and then call:
execCommand('insertimage', dataUrl);
editor.trigger('image-inserted');
I feel like this would eliminate the need for the $.when function which currently encloses those two function calls.
I have tried integrating other examples from StackExchange, this one was especially promising, but I must not understand JS well enough to properly adapt it to my needs.
I realize I can resize the image server-side after it gets POST/uploaded, but this is not desirable as the goal is to automatically resize a very large image which is dragged into the editor window to something which is more manageable to the end user who is using the editor window and doesn't fill the entire editor window in all directions.
Quality of the resulting image isn't all that important, so I'm not concerned with those types of optimizations and such which have been mentioned elsewhere.