From 539d04a6515f4703b69fe947b5f64d3ccf3557cd Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Fri, 8 Apr 2022 10:40:53 -0700 Subject: [PATCH 1/2] Introduce webp_uploads_prefer_smaller_image_file filter to opt in to preferring the smaller image file regardless of MIME type. --- modules/images/webp-uploads/load.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 3c8f915a90..2e89a4eb0a 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -599,6 +599,23 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id */ $target_mimes = apply_filters( 'webp_uploads_content_image_mimes', array( 'image/webp', 'image/jpeg' ), $attachment_id, $context ); + /** + * Filters whether the smaller image should be used regardless of which MIME type is preferred overall. + * + * This is disabled by default only because it is not part of the current WordPress core feature proposal, as it + * requires storing the file sizes, which the plugin implementation does but the core implementation does not. + * + * By enabling this, the plugin will compare the image file sizes and prefer the smaller file regardless of MIME + * type. + * + * @since n.e.x.t + * + * @param bool $prefer_smaller_image_file Whether to prefer the smaller image file. + */ + if ( apply_filters( 'webp_uploads_prefer_smaller_image_file', false ) ) { + $target_mimes = webp_uploads_get_mime_types_by_filesize( $target_mimes, $attachment_id ); + } + $target_mime = null; foreach ( $target_mimes as $mime ) { if ( isset( $metadata['sources'][ $mime ] ) ) { @@ -933,4 +950,3 @@ function( $a, $b ) { // Create an array of available mime types ordered by smallest filesize. return array_keys( $sources ); } -add_filter( 'webp_uploads_content_image_mimes', 'webp_uploads_get_mime_types_by_filesize', 10, 2 ); From da75dc85742ce638af5808490e49ad15f5413cd6 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 13 Apr 2022 12:05:49 -0700 Subject: [PATCH 2/2] Fix slightly inaccurate comment. --- modules/images/webp-uploads/load.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/images/webp-uploads/load.php b/modules/images/webp-uploads/load.php index 2e89a4eb0a..43428da6bb 100644 --- a/modules/images/webp-uploads/load.php +++ b/modules/images/webp-uploads/load.php @@ -602,8 +602,7 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id /** * Filters whether the smaller image should be used regardless of which MIME type is preferred overall. * - * This is disabled by default only because it is not part of the current WordPress core feature proposal, as it - * requires storing the file sizes, which the plugin implementation does but the core implementation does not. + * This is disabled by default only because it is not part of the current WordPress core feature proposal. * * By enabling this, the plugin will compare the image file sizes and prefer the smaller file regardless of MIME * type.