当前位置: 首页 > 知识库问答 >
问题:

通过调整dropzone压缩图像。js

冯德佑
2023-03-14

我是JS新手,最近几天我一直在压缩客户端的图像。我想做的是,用户在dropzone上放置一堆图像(可以超过10个),它们应该使用JIC进行压缩,一旦用户点击一个按钮上传所有压缩文件,就会上传到服务器上。

到目前为止,我的代码只能在删除一个图像时压缩和上传,但当我删除多个图像时,所有图像都保持未压缩状态,只有一个图像。我不确定我做错了什么。我试着从这篇文章中找到解决方案,但没能实现我的目标。我使用的代码如下:

Dropzone.autoDiscover=false;
var myDropZone=new Dropzone("#dropzonePreview",{
      url:"/dragdrop",
      autoProcessQueue:false,
      acceptedFiles: 'image/*',
      parallelUploads: 10,

      init:function(){
        this.on('addedfile', function(file){

            _this = this;
            ////console.log("Added File");
            $('#userphoto').css('color', "transparent");
            EXIF.getData(file, function(){ // async call
              var lat=EXIF.getTag(this,"GPSLatitude");
              var lon=EXIF.getTag(this,"GPSLongitude");
              geocoder.geocode( { 'latLng': temp }, function(results, status) { // another async call });
              }
            });

            myReader2 = new FileReader(); // Reading image for compression purpose
            myReader2.onload = function(event) {
                console.log(file.status);
                // var i = new Image();

                var i = document.getElementById("source_image");
                i.src = event.target.result;

                i.onload = function() {
                  var source_image = document.getElementById('source_image');

                  var quality = 70;

                  comp = jic.compress(source_image, 70, "jpg"); // Link to function can be found at the end of code.

                  var editedFile = base64ToFile(comp.src, file); // same function used in mentioned stackoverflow post.

                    // Replace original with resized

                    var origFileIndex = myDropZone.files.indexOf(file);
                    myDropZone.files[origFileIndex] = editedFile;

                    editedFile.status = Dropzone.ADDED;
                    myDropZone.enqueueFile(editedFile);

                    delete source_image;
                };
            };
            myReader2.readAsDataURL(file);
        });

        this.on("sending",function(file,xhr,formData){
          //appending some data to formData
        });

        this.on("complete", function(file){
            // processing like removing objects of file from drop zone
        });
      }
    });

$('#upload').click(function(evt){ // Button that triggers uploading file 
      myDropZone.processQueue();
}

链接到函数。非常感谢你的帮助。非常感谢。

共有1个答案

冷翼
2023-03-14

我找到了这个问题的解决方案。这是我的工作。

请查收

function base64ToFile(dataURI, origFile) {
  var byteString, mimestring;

  if(dataURI.split(',')[0].indexOf('base64') !== -1 ) {
    byteString = atob(dataURI.split(',')[1]);
  } else {
    byteString = decodeURI(dataURI.split(',')[1]);
  }

  mimestring = dataURI.split(',')[0].split(':')[1].split(';')[0];

  var content = new Array();
  for (var i = 0; i < byteString.length; i++) {
    content[i] = byteString.charCodeAt(i);
  }

  var newFile = new File(
    [new Uint8Array(content)], origFile.name, {type: mimestring}
  );


  // Copy props set by the dropzone in the original file

  var origProps = [ 
    "upload", "status", "previewElement", "previewTemplate", "accepted" 
  ];

  $.each(origProps, function(i, p) {
    newFile[p] = origFile[p];
  });

  return newFile;
}
Dropzone.autoDiscover = false;
jQuery(document).ready(function($) {

var myDropZone=new Dropzone("#dropzonePreview",{
     url:"/dragdrop",
     autoProcessQueue:false,
     acceptedFiles: 'image/*',
     parallelUploads: 10,

      init:function(){

        this.on("sending",function(file,xhr,formData){

        });
        this.on("complete", function(file){

        });
      }

     });
myDropZone.on("addedfile", function(file) {

        var reader = new FileReader();

        reader.addEventListener("load", function(event) {

          var origImg = new Image();
          origImg.src = event.target.result;

          origImg.addEventListener("load", function(event) { 
            comp = jic.compress(origImg, 30, "jpg");  
            var resizedFile = base64ToFile(comp.src, file);
            var origFileIndex = myDropZone.files.indexOf(file);
            myDropZone.files[origFileIndex] = resizedFile;
            myDropZone.enqueueFile(resizedFile);
          });
        });
        reader.readAsDataURL(file);
      });
$('#upload').click(function(e){ // Button that triggers uploading file 
    e.preventDefault();
    myDropZone.processQueue();  
});

});
 类似资料:
  • 我正在尝试调整缩略图中的图像大小,以固定框的大小。我试过这个: 这是原始图像的外观:http://files.parsetfss.com/12917a88-ac80-4e5e-a009-fc634161b79c/tfss-6c59b59f-8f57-4610-966e-31bbc203707b-samsung-galaxy-note-4-7290-002.jpg 我也试过: 但是没有任何变化,没有

  • 我想集成Dropzone。js具有客户端图像大小调整功能。我知道有一个函数可以调整缩略图的大小,但我想在上传之前创建一个函数来调整主图像的大小。有人能帮我吗?

  • 问题内容: 寻找一种将图像压缩为pdf格式并输出pdf进行存档的方法。我无法在创建图像之前对其进行压缩,因为这会影响打印质量。 每个pdf的大小约为8MB,其中大部分由2张图片组成。图片为png格式,并在生成过程中转换为pdf(使用了第三方生成器) 有没有一种方法可以在不使用第3方工具的情况下用Java压缩它们。我尝试使用pdfbox,itext和第3方exe(neevia),第3方工具是迄今为止

  • 我一直在计算图像的未压缩和压缩文件大小。这对我来说总是导致压缩图像比我预期的未压缩图像小。如果图像包含大量不同的颜色,则存储调色板会占用大量空间,还需要更多位来存储每个代码。然而,我的问题是,压缩方法是否可能导致比未压缩的RGB图像更大的文件。对于这种压缩方法仍然有用的、总共包含k种不同颜色的最小正方形RGB图像,其大小(像素)是多少?因此,我们想要找到,对于给定的k值,找到最小整数n,对于该整数

  • 我用C#在屏幕外生成一堆SSRS报告,然后把每个报告的图像粘贴到一个PowerPoint演示文稿中,有效地将所有报告整理成一个包。虽然这样做很好,但对于一个26页的演示文稿,它的文件大小只有23mb。(我明白,使用高dpi在一定程度上导致了这个巨大的数字,但事实证明,这是创建所需清晰度的图像所必需的)。 但是,当我将此演示文稿手动保存在PowerPoint中时,文件大小会减小到大约13mb。(这是

  • 问题内容: 我正在通过wifi或移动网络通过网络发送图像,以将其存储在服务器中并再次检索。我已经这样做了,但是由于相机拍摄的图像太大,这使我的应用程序变慢,只是要指出我正在打开图库并从那里拍摄照片,而不是直接从应用程序拍摄照片。我注意到,从相机和图库中获取的来自whatsapp的图像已被压缩到大约50%。100kb。 目前,我的代码获取一个文件,并将其转换为字节,然后发送。这是获取文件并将其转换为