hahahaha
hahahaha

Reputation: 1031

Dropzone check image dimension and file size

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:

Upvotes: 13

Views: 13383

Answers (1)

Silver
Silver

Reputation: 121

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();
            }
        }
    })
},

Upvotes: 12

Related Questions