1

I need to make dropzone to compress images after they all load. I tried to use queuecomplete but I kept getting errors. I don't know where exactly to put that. I put it instead of transformFile but my compressor stopped working.

Can you help please?

Dropzone.options.myDropzone = {
  url: " ",
  autoProcessQueue: true,
  parallelUploads: 10,
  transformFile: function(file, done) { // i tried queuecomplete HERE
    const imageCompressor = new ImageCompressor();
    imageCompressor.compress(file, {
      checkOrientation: true,
      maxWidth: 8192,
      maxHeight: 8192,
      quality: 0.69,
    }).then((result) => {
      done(result)
    }).catch((err) => {
      throw err
    })
  }
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 'i kept getting errors' - what were the errors? – Rory McCrossan Jan 07 '22 at 15:14
  • i try to put ```queuecomplete``` after ```parallelUploads: 10,``` and i kept getting errors that there is unexpected ':' which is the transformFile function – Alev Mustafa Jan 07 '22 at 15:19
  • 1
    Does this answer your question? [Adding queuecomplete to dropzone.js](https://stackoverflow.com/questions/31548480/adding-queuecomplete-to-dropzone-js) – GenericUser Jan 07 '22 at 15:24
  • @GenericUser No :) cuz i already try all of the answers here :) – Alev Mustafa Jan 07 '22 at 15:26
  • 1
    You may want to see if another image compression library may provide what you need. I'm assuming that you are using this library which appears to be untouched for awhile and has low usage: https://www.npmjs.com/package/image-compressor (~450 downloads/week, updated in July/2021) On the other hand this compression library seems to be quite popular and actively maintained: https://www.npmjs.com/package/browser-image-compression (~45,000 downloads/week, updated in December/2021) – GenericUser Jan 07 '22 at 15:35
  • Can you please provide a link to the Image Compressor library you are using. I am unable to find a compressor library matching the config you have shared. If it is as @GenericUser suggested, the setup you using does not appear to be correct as it does not support promises. This would explain the unhelpful error your receiving as promise errors usually are. – factorypolaris Jan 28 '22 at 08:39
  • @factorypolaris https://www.npmjs.com/package/@xkeshi/image-compressor – Alev Mustafa Jan 28 '22 at 11:28
  • Can you please include the library import (dropzone and image-compressor) so people can run it and see the error you are getting? – Hermanboxcar Jan 30 '22 at 05:45
  • I can see there is no transformFile property in dropzone – Negi Rox Jan 30 '22 at 18:51

2 Answers2

2

So here is resize option in Dropzone. So If you mark createImageThumbnails : false then resize function will not work. but if you mark it as true it will work accordingly. The best thing is that its already resizing image and creating thumbnails for you because by default createImageThumbnails : true .

var options = {
  createImageThumbnails: false,
  url: " ",
  autoProcessQueue: true,
  parallelUploads: 10,
  resize: function(file) {
        var info, srcRatio, trgRatio;
        info = {
          srcX: 0,
          srcY: 0,
          srcWidth: file.width,
          srcHeight: file.height
        };
        srcRatio = file.width / file.height;
        info.optWidth = this.options.thumbnailWidth;
        info.optHeight = this.options.thumbnailHeight;
        if ((info.optWidth == null) && (info.optHeight == null)) {
          info.optWidth = info.srcWidth;
          info.optHeight = info.srcHeight;
        } else if (info.optWidth == null) {
          info.optWidth = srcRatio * info.optHeight;
        } else if (info.optHeight == null) {
          info.optHeight = (1 / srcRatio) * info.optWidth;
        }
        trgRatio = info.optWidth / info.optHeight;
        if (file.height < info.optHeight || file.width < info.optWidth) {
          info.trgHeight = info.srcHeight;
          info.trgWidth = info.srcWidth;
        } else {
          if (srcRatio > trgRatio) {
            info.srcHeight = file.height;
            info.srcWidth = info.srcHeight * trgRatio;
          } else {
            info.srcWidth = file.width;
            info.srcHeight = info.srcWidth / trgRatio;
          }
        }
        info.srcX = (file.width - info.srcWidth) / 2;
        info.srcY = (file.height - info.srcHeight) / 2;
        return info;
      }
};

I am adding both codes and you can debug the code. hope it will help you out. You can also add the below option for thumbnails.

      maxThumbnailFilesize: 10,
      thumbnailWidth: 120,
      thumbnailHeight: 120,

var options = {
  createImageThumbnails: false,
  url: " ",
  autoProcessQueue: true,
  parallelUploads: 10,
  resize: function(file) {
        var info, srcRatio, trgRatio;
        info = {
          srcX: 0,
          srcY: 0,
          srcWidth: file.width,
          srcHeight: file.height
        };
        srcRatio = file.width / file.height;
        info.optWidth = this.options.thumbnailWidth;
        info.optHeight = this.options.thumbnailHeight;
        if ((info.optWidth == null) && (info.optHeight == null)) {
          info.optWidth = info.srcWidth;
          info.optHeight = info.srcHeight;
        } else if (info.optWidth == null) {
          info.optWidth = srcRatio * info.optHeight;
        } else if (info.optHeight == null) {
          info.optHeight = (1 / srcRatio) * info.optWidth;
        }
        trgRatio = info.optWidth / info.optHeight;
        if (file.height < info.optHeight || file.width < info.optWidth) {
          info.trgHeight = info.srcHeight;
          info.trgWidth = info.srcWidth;
        } else {
          if (srcRatio > trgRatio) {
            info.srcHeight = file.height;
            info.srcWidth = info.srcHeight * trgRatio;
          } else {
            info.srcWidth = file.width;
            info.srcHeight = info.srcWidth / trgRatio;
          }
        }
        info.srcX = (file.width - info.srcWidth) / 2;
        info.srcY = (file.height - info.srcHeight) / 2;
        return info;
      }
};

$( document ).ready(function() {
        $("#mydropzone").addClass("dropzone");
        $("#mydropzonewithresize").addClass("dropzone");
        var myAwesomeDropzone = new Dropzone("#mydropzone",options);
        options.createImageThumbnails=true;
        var mydropzonewithresize = new Dropzone("#mydropzonewithresize",options);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.js"></script>
<link rel="stylesheet" href="https://rawgit.com/enyo/dropzone/master/dist/dropzone.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<p>
  This is the most minimal example of Dropzone. The upload in this example  doesn't work,
</p>
<div id="mydropzone"></div>
<div id="mydropzonewithresize"></div>
Negi Rox
  • 3,828
  • 1
  • 11
  • 18
1

try

let number_of_files = 0;
//how many files loaded already
let filearr = []
$(function() {
      // Now that the DOM is fully loaded, create the dropzone, and setup the
      // event listeners
      var myDropzone = new Dropzone("#my-dropzone");
      myDropzone.on("success", function(file) {
        number_of_files++
filearr.push(file)
      });
    })
$(function() {
      // Now that the DOM is fully loaded, create the dropzone, and setup the
      // event listeners
      var myDropzone = new Dropzone("#my-dropzone");
      myDropzone.on("success", function(file) {
        number_of_files++
filearr.push(file)
      });
    })
//as many as you wish (or do it in a for loop)
//suppose the number of files you need is x
let x = 10
function flag() {
if(number_of_files == x) {
setTimeout(flag, 100) //checks if complete every 100 milliseconds 
} else {
//compress files here
}

} 

Hermanboxcar
  • 418
  • 1
  • 13