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

347
Visualizações
Javascript - How to reduce the size when converting an image file to Base64 string data?

This code converts the image file to Base64 string data. But I want to reduce the size of the base64 string because the size of the base64 string data I converted is larger than the image file. Is that possible?

<input id="inputFileToLoad" type="file" accept="image/*" onchange="encodeImageFileAsURL();"/>
<div id="imgTest"></div>

<script type='text/javascript'>
  function encodeImageFileAsURL(){
    var filesSelected = document.getElementById("inputFileToLoad").files;
    var fileSize = filesSelected[0].size;
    if(fileSize < 10000000){
      if (filesSelected.length > 0){
        var fileToLoad = filesSelected[0];
        var fileReader = new FileReader();

        fileReader.onload = function (fileLoadedEvent){
          var srcData = fileLoadedEvent.target.result; // <--- data: base64
          var newImage = document.createElement('img');
          newImage.src = srcData;

          document.getElementById("imgTest").innerHTML = newImage.outerHTML;
          alert("Converted Base64 version is " + document.getElementById("imgTest").innerHTML);
        }
        fileReader.readAsDataURL(fileToLoad);
      }

    } else {
      alert("File exceeded"); 
      }
  }
</script>        
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

64 is 2^6(6 bits). This means that every char(8 bits) contains only 6 bits of information. So in order to convey the same amount of information base64 requires 33.3% more bits compared to binary.

The only thing you can do is reduce the size of the original image before you apply base64, this will reduce the size of the base64 string by the same ratio.

about 4 years ago · Juan Pablo Isaza 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