-
-
Notifications
You must be signed in to change notification settings - Fork 315
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
Feature request: Allow the possibility to modify image blob/fileList prior to upload #378
Comments
Hi, @MRB60 |
Ok thanks - not sure how to do that since I need to modify blob contents/resolution size and this function does not return new contents - changing function arguments I presume is not the way. I also believe is is called too late in the process for that looking at the image plugin code. |
Currently, it seems difficult to adjust the file size before uploading. |
Yes, it may work. File size checks are done prior to call and info prepared but not sure if this is a problem if the actual file blob (and w/h) is changed. I can make a try if |
Ok, I will modify the code as below: image.js#L395 if (typeof this.functions.onImageUploadBefore === 'function') {
files = this.functions.onImageUploadBefore(files, info, this);
if (!files) return;
} |
Code might become backward incompatible in case someone today returns true (would destroy files variable) so better make a warning in changelog. Or make an additional test on true before assigning "files". |
The 2.30.0 version has been updated. |
Currently it runs async. |
You are correct. |
Promise is not supported in IE. |
In my solution, I will not support IE but blob generation is supported in IE (with own vendor prefix). |
What does uploadhandler return? |
"uploadhandler" is upload method of plugin. |
I did not use async/await to implement canvas to blob. I used a simple callback and thus I do not completely follow your suggestion on how to continue execution and make the upload of the new files. |
If editor.uploadBefore = function (files, info, core, uploadHandler) {
setTimeout(function() {
// upload files
uploadHandler(files /* or [new File(...)] */);
// error
uploadHandler('Error message');
// Just finish with don't upload
uploadHandler();
},1000)
} |
Example: function ResizeImage (files, uploadHandler) {
const uploadFile = files[0];
const img = document.createElement('img');
const canvas = document.createElement('canvas');
const reader = new FileReader();
reader.onload = function (e) {
img.src = e.target.result
img.onload = function () {
let ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
const MAX_WIDTH = 200;
const MAX_HEIGHT = 100;
let width = img.width;
let height = img.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
canvas.width = width;
canvas.height = height;
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
canvas.toBlob(function (blob) {
uploadHandler([new File([blob], uploadFile.name)])
}, uploadFile.type, 1);
}
}
reader.readAsDataURL(uploadFile);
}
editor.onImageUploadBefore = function (files, info, core, uploadHandler) {
console.log('files--', files);
console.log('info--', info);
ResizeImage(files, uploadHandler)
// return undefined
} |
The 2.30.1 version has been updated. |
Tried your example but am getting uploadHandler not a function error |
my working example:
and ResizeImage:
Worked with Suneditor v2.31.0 |
ok, thanks |
One more thing. How do I implement this in react, the uploadHandler is not recognised as a function. |
It would be useful to be able to perform file size reduction of images prior to upload. If a callback could be defined which is called at the very beginning of image dialog, the fileList/blob could be modified by this callback function and returned to the image dialog plugin for further processing and upload. How the callback could look like I can post an example here if/when this is implemented.
The text was updated successfully, but these errors were encountered: