Skip to main content

Images convert into webp drag and drop

Here is an example of how you can convert a JPG, PNG, GIF, or ICO image to the WEBP format using JavaScript and the canvas element:

// Select the file input element

const fileInput = document.querySelector('#fileInput');


// Select the convert button

const convertButton = document.querySelector('#convertButton');


// Select the download link element

const downloadLink = document.querySelector('#downloadLink');


// Add an event listener to the convert button

convertButton.addEventListener('click', () => {

  // Get the file from the file input element

  const file = fileInput.files[0];


  // Create a new FileReader

  const reader = new FileReader();


  // Add an event listener for when the file has finished loading

  reader.addEventListener('load', () => {

    // Get the data URL of the file

    const dataURL = reader.result;


    // Create an image element

    const image = new Image();


    // Set the src of the image to the data URL

    image.src = dataURL;


    // Add an event listener for when the image has finished loading

    image.addEventListener('load', () => {

      // Get the canvas element

      const canvas = document.querySelector('#canvas');


      // Set the width and height of the canvas to the width and height of the image

      canvas.width = image.width;

      canvas.height = image.height;


      // Get the context of the canvas

      const ctx = canvas.getContext('2d');


      // Draw the image on the canvas

      ctx.drawImage(image, 0, 0);


      // Get the data URL of the canvas

      const convertedDataURL = canvas.toDataURL('image/webp');


      // Set the href of the download link to the converted data URL

      downloadLink.href = convertedDataURL;


      // Enable the download link

      downloadLink.removeAttribute('disabled');

    });

  });


  // Read the file as a data URL

  reader.readAsDataURL(file);

});


This code assumes that you have an HTML page with the following elements:

<input type="file" id="fileInput" /> <button id="convertButton">Convert to WEBP</button> <a id="downloadLink" disabled>Download WEBP</a> <canvas id="canvas"></canvas>


To convert multiple images at once, you can modify the code to loop through an array of files and convert them one by one. You could also use a file drop area or drag and drop functionality to allow the user to select and convert multiple images at once. 


Comments

Popular posts from this blog

C++ Programming Tutorial - How to Install Code::Blocks in Ubuntu Linux - Learn Online

Installing Code::Blocks How do I Install Code::Blocks HOWTO: Installing Code::Blocks 10.05 How do you install Irrlicht on Code::Blocks? How do you install CodeBlock on ubuntu? codeblocks /bin/sh: g++: not found Installing codeblocks on ubuntu Installing Code::Blocks on Ubuntu 10.04?

How To Install Kali Linux in VirtualBox - Learn Online

in this video I am going to sho you How to install Kali Linux in Virtualbox : Step-by-step . This is A detailed guide on installing Kali Linux on VirtualBox. I am going to show How to Install Kali Linux 2016 + Guest Additions on VirtualBox. Install Kali linux 2.0 on Virtual Box Step by Step Install Kali LInux 2 in VirtualBox Installing Kali Linux 2016.1 on VirtualBox

How to install Skype on Ubuntu Linux 13.04/12.10/12.04 x64/x32 bit - Learn Online

Required commands to install skype sudo dpkg --add-architecture i386 sudo apt-get update sudo add-apt-repository "deb $(lsb_release -sc) partner" sudo apt-get update && sudo apt-get install skype Download Skype for Linux, get Skype for Linux How to Install Skype Using Terminal on Ubuntu How to install Skype with 13.04 How do I install Skype Install Skype on Ubuntu 12.04 LTS 64-bit How to install Skype in Ubuntu 13.04 How to get Skype for Ubuntu How to install skype in ubuntu 12.04 Latest Skype Release Added to Ubuntu Software Center How do I add Skype to my version of Ubuntu Skype for Ubuntu (32-bit) How to get Skype video call to work in Ubuntu How to Get Rid of Skype on Ubuntu How to Install Skype Wrapper and Integrate Skype into Ubuntu