Search code examples
javascriptphphtmlcsswebp

Compressing and Resizing Images for the Web


So I'm currently building a website using a php backend and polymer frontend. The client wants to be able to have a news feature for their own events. For this I want to convert all the images to webp and create a few different sizes so I can serve them quickly to different browsers (Mobile, Tablet, Desktop etc). However I haven't been able to find a good way of doing this in PHP or JS. Squoosh is great for static assets but not user generated content. Any help appreciated thanks!


Solution

  • PHP has functions for manipulating webp images. Try this.

    <?php
    $im = imagecreatefromstring(file_get_contents('path/to/image.jpg')); // Create image identifier
    imagewebp($im, 'path/to/image.webp'); // Generate webp image and save to location
    imagedestroy($im); // Free up image identifier
    ?>