How to PNG or JPEG convert into Web format easily ?

Convert PNG or JPEG images to Web format programmatically 

you can use various programming languages and libraries. Here are examples using popular languages Serve images in next-gen formats



JavaScript with the browser's built-in canvas:


<body>
  <script>
    // Create a canvas element
    var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');

    // Function to convert and download an image
    function convertImageToWebP(imageSrc) {
      // Load the input image
      var image = new Image();
      image.onload = function() {
        // Set the canvas dimensions to match the image
        canvas.width = image.width;
        canvas.height = image.height;

        // Draw the image on the canvas
        context.drawImage(image, 0, 0);

        // Convert the canvas to WebP format
        canvas.toBlob(function(blob) {
          // Create a download link
          var link = document.createElement('a');
          link.href = URL.createObjectURL(blob);
          link.download = 'output.webp';

          // Simulate a click on the link to trigger the download
          link.click();
        }, 'image/webp');
      };

      // Specify the image source
      image.src = imageSrc;
    }

    // Convert and download each image
    convertImageToWebP('input.png');
    convertImageToWebP('input.jpg');
    convertImageToWebP('input.jpeg');
  </script>
</body>

PHP with GD library:


// Load the input image
$image = imagecreatefromjpeg('input.jpg');

// Convert and save the image in WebP format
imagewebp($image, 'output.webp', 80); // 80 is the quality parameter (0-100)

// Free up memory
imagedestroy($image);


Python with Pillow library:


from PIL import Image

# Open the input image
image = Image.open('input.png')

# Convert and save the image in WebP format
image.save('output.webp', 'webp')

Tools or Plugin: "WebP Express" free plugin will help you.

Online Source

Comments

Popular posts from this blog

Office Tool_SPSS v23 + Serial key

How to Fix FATAL error Failed to parse input Document ?

How to Reduce Lazy Load Resources

Popular posts from this blog

Office Tool_SPSS v23 + Serial key

How to Fix FATAL error Failed to parse input Document ?

How to Reduce Lazy Load Resources