Dropzone check image dimension and file size
Asked Answered
D

1

13

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

Distich answered 3/8, 2016 at 8:4 Comment(0)
Z
12

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();
            }
        }
    })
},
Zohar answered 8/1, 2018 at 9:57 Comment(1)
Interesting, so it seems when I try to get the width when the file is added it fails, it only works after the thumb generation event. Good to know.Whipstitch

© 2022 - 2024 — McMap. All rights reserved.