Advertisement
MizunoBrasil

Automação: Redimensiona imagem e depois deixa baixar - Outra opção

Feb 9th, 2023
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <script>
  5.       function uploadImage(input) {
  6.         var reader = new FileReader();
  7.         reader.onload = function (event) {
  8.           var image = new Image();
  9.           image.onload = function () {
  10.             var select = document.getElementById("dimensions");
  11.             var dimensions = select.options[select.selectedIndex].value.split("x");
  12.             var width = parseInt(dimensions[0]);
  13.             var height = parseInt(dimensions[1]);
  14.             var canvas = document.createElement("canvas");
  15.             canvas.width = width;
  16.             canvas.height = height;
  17.             var ctx = canvas.getContext("2d");
  18.             ctx.drawImage(image, 0, 0, width, height);
  19.             document.getElementById("image").src = canvas.toDataURL();
  20.             document.getElementById("downloadButton").style.display = "block";
  21.             document.getElementById("image").style.display = "block";
  22.           };
  23.           image.src = event.target.result;
  24.         };
  25.         reader.readAsDataURL(input.files[0]);
  26.       }
  27.  
  28.       function downloadImage() {
  29.         var image = document.getElementById("image");
  30.         var link = document.createElement("a");
  31.         link.download = "resized-image.png";
  32.         link.href = image.src;
  33.         link.click();
  34.       }
  35.     </script>
  36.   </head>
  37.   <body>
  38.   <br><br>
  39.    Escolha primeiro a dimensão que deseja a sua imagem e depois escolha o arquivo:
  40.   <br><br>
  41.     <select id="dimensions">
  42.       <option value="640x360">640x360</option>
  43.       <option value="848x480">848x480</option>
  44.       <option value="1280x720">1280x720</option>
  45.       <option value="1920x1080">1920x1080</option>
  46.     </select>
  47.     <br><br>
  48.     <input type="file" onchange="uploadImage(this)">
  49.     <br><br>
  50.     <img id="image" src="" style="display: none">
  51.     <br><br>
  52.     <button id="downloadButton" onclick="downloadImage()" style="display: none">Baixar imagem</button>
  53.   </body>
  54. </html>
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement