Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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

like image 517
hahahaha Avatar asked Aug 03 '16 08:08

hahahaha


1 Answers

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();
            }
        }
    })
},
like image 191
Silver Avatar answered Sep 18 '22 11:09

Silver