codebuds/webp-conversion-bundle
The WebP Conversion Bundle is a Symfony bundle designed to facilitate the automatic conversion of JPEG, BMP, PNG, and GIF images to WebP format. By converting images to WebP, you can significantly reduce file sizes and improve website rendering speed. This bundle is compatible with PHP 8.1 and 8.2, as well as Symfony 6.1, 6.2, and 6.3
The WebPConversionBundle allows you to configure various settings to customize the behavior of the image conversion process. To do this, follow these steps:
Install the bundle: If you haven't already installed the WebPConversionBundle, add it to your Symfony project by running the following command:
composer require codebuds/webp-conversion-bundle
Override default configuration: To customize the bundle's configuration, create a new file named webp_conversion.yaml inside the config/packages directory of your Symfony project (if it doesn't exist, create it).
Configure the parameters: In the webp_conversion.yaml file, you can set the desired configuration options. For example, to change the default quality and upload path, use the following syntax:
# config/packages/webp_conversion.yaml
web_p_conversion:
quality: 90
upload_path: '/custom/upload/directory'
This bundle provides a command, codebuds:webp:convert, that simplifies the process of converting images to WebP format. The command accepts a directory as an argument, and all images (JPEG, PNG, GIF, and BMP) within that directory will be converted to WebP. The command offers several optional parameters to customize the conversion:
php bin/console codebuds:webp:convert --create --quality=90 --suffix=_q90 public/images
If the public/images directory contains an image named test.jpg, after executing the command, it will also contain test_q90.webp.
The bundle includes a Twig extension that generates WebP images and returns the path to them. This makes it easy to create elements to optimize website rendering speed.
<!-- Traditional approach -->
<img src="/public/images/test.jpg">
<!-- Modern approach using WebP -->
<picture>
<source srcset="{{ '/images/test.jpg' | cb_webp }}" type="image/webp">
<source srcset="/images/test.jpg" type="image/jpeg">
<img src="/images/test.jpg">
</picture>
The Twig extension also supports VichUploaderBundle assets and LiipImagineBundle filters:
<picture>
<source srcset="{{ vich_uploader_asset(asset, 'imageFile') | cb_webp | set_webp_extension | imagine_filter(filter) }}"
type="image/webp">
<source srcset="{{ vich_uploader_asset(asset, 'imageFile') | imagine_filter(filter) }}">
<img src="{{ vich_uploader_asset(asset, 'imageFile') | imagine_filter(filter) }}">
</picture>
How can I help you explore Laravel packages today?