THpubs
THpubs

Reputation: 8172

In Dropzone.js how to set the minimum image resolution that we can upload? Accept is not working

Let's say I need to restrict my users from uploading images below 3MP (Nearly 2048px wide). How can I do this in Dropzone.js? Tried to do it with 'accept' but it's not working :

$(function() {
var mediaDropzone;
mediaDropzone = new Dropzone("#media-dropzone", {
  paramName: "file",
        autoProcessQueue: false,
        parallelUploads: 500,
        maxFilesize: <%= ENV["max_image_size"] %>,
        acceptedFiles: '<%= ENV["accepted_files"] %>',
        accept: function(file, done) {
          if (file.width < 2048) {
            done("Naha, you don't.");
          }
          else { done(); }
        }
});

Upvotes: 3

Views: 10279

Answers (2)

xrcwrn
xrcwrn

Reputation: 5327

insted to apply in accept: you can use thumbnail like below

You can check width and height for image like

 this.on("thumbnail", function (file) {
     if (file.height < 350 && file.width < 2048) {
        alert("Image should be 350 x 2048 ");
         }
  });

Upvotes: 0

aries23
aries23

Reputation: 326

I have a similar need and am using this solution:

    var maxImageWidth = 1000, maxImageHeight = 1000;

    Dropzone.options.dropzonePhotographs = {
        paramName: "file", // The name that will be used to transfer the file
        maxFilesize: 2, // MB
        dictDefaultMessage: "Drag files or click here",
        acceptedFiles: ".jpeg,.jpg,.png,.gif",
        init: function () {
            this.on("success", function(file, responseText) {
                file.previewTemplate.setAttribute('id',responseText[0].id);
            });
            this.on("thumbnail", function(file) {
                if (file.width > maxImageWidth || file.height > maxImageHeight) {
                    file.rejectDimensions()
                }
                else {
                    file.acceptDimensions();
                }
            });
        },
        accept: function(file, done) {
            file.acceptDimensions = done;
            file.rejectDimensions = function() { done("Image width or height too big."); };
        }
    };

You can find the original solution here: https://github.com/enyo/dropzone/issues/708

Upvotes: 8

Related Questions