WebP Support and default Mime Types of Sub images
Visit Original Post: Post
WordPress 5.8 introduces support for WebP image format which provides improved lossless and lossy compression for images on the web. WebP images are around 30% smaller on average than their JPEG or PNG equivalents, resulting in sites that are faster and use less bandwidth. WebP is supported in all modern browsers according to caniuse.
When images are uploaded, WordPress generates smaller sub sizes as defined using add_image_size(). By default, WordPress will generate these sub sizes in the same format as the original. Because of the performance benefits of the WebP format, it may be desirable for sub sizes to be generated in WebP instead of the original format.
image_editor_output_format filter hook can be used to change the file format used for image sub sizes. This can be used to switch all sub sizes to WebP, or any other desired format (JPEG, etc.).
The following example shows how to generate all sub sizes for JPG images using WebP:
<?php
function wporg_image_editor_output_format( $formats ) {
$formats['image/jpg'] = 'image/webp';
return $formats;
}
add_filter( 'image_editor_output_format', 'wporg_image_editor_output_format' );
Note: both the GD and ImageMagick libraries support the WebP format in both lossy and lossless. However, only ImageMagick supports animated images.
Setting the output format to WebP will verify if the web server supports it, and if not it will not change the format, i.e. won’t work.