0

我目前正在尝试使用 DropzoneJS 压缩图像。我想通过质量、删除元数据等来压缩图像,而不仅仅是调整大小。我该怎么做?

4

1 回答 1

2

我找到了解决方案。您将需要图像压缩库,可以在这里下载:https ://xkeshi.github.io/image-compressor/

// "myDropzone" is the camelized version of the HTML element's ID
Dropzone.options.myDropzone = {
  transformFile: function(file, done) {
     const imageCompressor = new ImageCompressor();

     imageCompressor.compress(file, {
     // I assume the output image won't have the meta data anymore
     checkOrientation: true,
     // Limit output image width & height
     // For controllable file size & avoid blank output image
     // https://github.com/xkeshi/image-compressor#maxwidth
     maxWidth: 8192,
     maxHeight: 8192,
     // 0.8 is the default and already good enough
     // https://github.com/xkeshi/image-compressor#quality
     quality: 0.6,
     // Convert ALL PNG images to JPEG
     convertSize: 0,
     })
     .then((result) => {
       // Handle the compressed image file.
       done(result)
     })
     .catch((err) => {
       // Handle the error
       throw err
     })
  }
};
于 2018-08-06T00:34:51.907 回答