javascript - 如何使用 DropzoneJS 压缩图像?

标签 javascript image dropzone.js dropzone

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

最佳答案

我已经找到解决办法了。您将需要图像压缩库,可以在这里下载: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
     })
  }
};

关于javascript - 如何使用 DropzoneJS 压缩图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51688638/

相关文章:

javascript - slideDown 不工作或像 .show 一样工作

android - 相机胶卷图库链接?!土坯空气

jquery - HTML 文件输入到 Dropzone

javascript - 如何将之前加载的文件添加到 DropZone.js

javascript - 使用 vis.js 使节点可点击

以相同速度运行的 Javascript 游戏循环?

javascript - 一个事件的多个功能不起作用

c# - C# 中的快速子图像比较

Android - 如何设置壁纸图片

javascript - Dropzone MVC 5 在上传时添加额外参数