I'm currently fixing an existing code to check image minimum dimensions before upload. I'm now getting the proper size of image thanks to new Image() but I can't return the result! Here's the code :
_hasError: function (file) {
if (file.error) {
return file.error;
}
if (this.options.minWidth > 0 && this.options.minHeight > 0) {
var checkSizeUpload = function(minWidth, minHeight) {
if (this.width < minWidth || this.height < minHeight) {
return "Image too small";
}
};
var tmpImg = new Image();
tmpImg.src = URL.createObjectURL(file);
tmpImg.onload = checkSizeUpload.bind(tmpImg, this.options.minWidth, this.options.minHeight);
}
...
return null;
}
As you can see, the method is designed to return null or error message. The trouble is, as the onload callback is asynchronous, I can't find a way to return the error message once I know the actual size of the image.
From what I saw, I might have to add another callback but I can't see how this would help since if I return from it, it won't return in the needed scope.
How can I fix this ? Thanks for your help!