Advertisement
MizunoBrasil

Redimensiona Imagem

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