Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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. 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. However, when I get the email and try to open it I get the error "It appears we don't support this file format".

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 .

  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.

Here is the relevant PHP code. In a post request route

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

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

front-end 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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda