I'm using the Dropzonejs plugin. I want to check the image dimension (width and height) and also the file size when a file is uploaded. I managed to check the dimension and file size but when I combined both of them, it didn't work well.
var maxImageWidth = 2500,
maxImageHeight = 2500;
Dropzone.options.formUserEdit = {
maxFilesize: 2,
acceptedFiles: 'image/*',
success : function(file, Response){
var obj = JSON.parse(Response);
$('#form-user-edit').append('<input type="hidden" name="userprofile" data-ori="'+file.name+'" value="'+obj.filename+'" />');
$('.error-msg').remove();
},
init: function() {
this.on("thumbnail", function(file) {
if (file.width > maxImageWidth || file.height > maxImageHeight) {
file.rejectDimensions();
else {
file.acceptDimensions();
}
})
},
accept: function(file, done) {
file.rejectDimensions = function() {
done("Please make sure the image width and height are not larger than 2500px.");
};
file.acceptDimensions = done;
}
}
if:
upload big dimension image: it accepts the function
upload small dimension image but more than 2MB: it will return file.acceptDimensions is not a function
or will not go to success
In case someone still need the answer.
init: function() {
this.on("thumbnail", function(file) {
if (file.width > maxImageWidth || file.height > maxImageHeight) {
file.rejectDimensions();
else {
if(file.size < 1024*1024*2/*2MB*/)
{
file.acceptDimensions();
}
}
})
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With