简体   繁体   English

在文件输入上使用 JavaScript 压缩图像不起作用。 我哪里错了?

[英]Compressing images using JavaScript on File input is not working. Where am I going wrong?

Here is the array that works when I send it to my PHP server.这是我将它发送到我的 PHP 服务器时工作的阵列。 The images are not compressed in this array.图像未在此数组中压缩。

Array ( [name] => Array ( [0] => IMG_3272.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => C:\xampp\tmp\phpE857.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 2267094 ) )

Below is the array that does not work.下面是不起作用的数组。 At this point the images are compressed.此时图像被压缩。

Array ( [name] => Array ( [0] => IMG_3270.jpg ) [type] => Array ( [0] => img/jpg ) [tmp_name] => Array ( [0] => C:\xampp\tmp\php4129.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 12 ) )

I successfully send the email with the image attachments.我成功发送了带有图片附件的 email。 However, when I get the email and try to open it I get the error "It appears we don't support this file format".但是,当我得到 email 并尝试打开它时,我收到错误“看来我们不支持这种文件格式”。

When the images are not compressed the attachments to the emails are viewable.当图像未压缩时,可以查看电子邮件的附件。

Here is the function that is supposed to compress the images and add them to a hidden input in the HTML form.这是 function,它应该压缩图像并将它们添加到 HTML 表单中的隐藏输入中。

  async function handleImageUpload() {
    var fileUpload = document.getElementById("fileUpload");
    
    const options = {
      maxSizeMB: 1,
      maxWidthOrHeight: 1920,
      useWebWorker: true
    }
    const compressedImages = [];
    for(let i = 0; i < fileUpload.files.length; i++){

const imageFile = fileUpload.files[i];

try {
   const compressedFile = await imageCompression(imageFile, options);
   compressedImages.push(compressedFile)

//   await uploadToServer(compressedFile); // write your own logic
} catch (error) {
  console.log(error);
}
}

    var fileUploads = document.getElementById("fileUploads");
    const dataTransfer = new DataTransfer();

    const file = new File([compressedImages[0]['name']], compressedImages[0]['name'], {type: 'img/jpg'})
    dataTransfer.items.add(file);
    fileUploads.files = dataTransfer.files;
    console.log(fileUploads.files)

}

The reason for all of this is due to funky logic happening in the backend.所有这一切的原因是由于后端发生了时髦的逻辑。 For whatever reason when I get an Img array of close 6mb multiple emails get sent.无论出于何种原因,当我收到一个接近 6mb 的 Img 数组时,都会发送多封电子邮件。

Here is the relevant PHP code.这是相关的 PHP 代码。 In a post request route在 post 请求路由中

            $files = $_FILES['uploaded_files'];
            print_r($files);
            die();
``

For anyone who comes across this post.对于任何遇到这篇文章的人。

Note that the ultimate goal is to have multiple files compressed.请注意,最终目标是压缩多个文件。 Right now I can compress just one file.现在我只能压缩一个文件。 I think I will be able to do this to multiple files via a for loop.我想我将能够通过 for 循环对多个文件执行此操作。

front-end javascript--前端javascript——

  async function handleImageUpload() {
    var fileUpload = document.getElementById("fileUpload");
    
    const options = {
      maxSizeMB: 1,
      maxWidthOrHeight: 1920,
      useWebWorker: true
    }
    const compressedImages = [];
    for(let i = 0; i < fileUpload.files.length; i++){

const imageFile = fileUpload.files[i];

try {
console.log(imageFile)
   const compressedFile = await imageCompression(imageFile, options);
   compressedImages.push(compressedFile)
  console.log(compressedImages[0])
} catch (error) {
  console.log(error);
}
}
//This is a hidden input in the html document. This is what will be sent to the server.
    var fileUploads = document.getElementById("fileUploads");
    const dataTransfer = new DataTransfer();

    const file = new File([compressedImages[0]], 'Hello_world.jpg',{type:"image/jpeg"})

    dataTransfer.items.add(file);
    fileUploads.files = dataTransfer.files;
    console.log(fileUploads.files)

}

I used this npm package via CDN https://www.npmjs.com/package/browser-image-compression我通过 CDN https://www.npmjs.com/package/browser-image-compression使用了这个 npm package

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM