Skip to content

Commit faa8156

Browse files
authored
Merge pull request #287 from WordPress/enhancement/smaller-image-file-optin
Introduce `webp_uploads_prefer_smaller_image_file` filter allowing to opt in to preferring the smaller image file
2 parents 9402949 + da75dc8 commit faa8156

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

modules/images/webp-uploads/load.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,22 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
599599
*/
600600
$target_mimes = apply_filters( 'webp_uploads_content_image_mimes', array( 'image/webp', 'image/jpeg' ), $attachment_id, $context );
601601

602+
/**
603+
* Filters whether the smaller image should be used regardless of which MIME type is preferred overall.
604+
*
605+
* This is disabled by default only because it is not part of the current WordPress core feature proposal.
606+
*
607+
* By enabling this, the plugin will compare the image file sizes and prefer the smaller file regardless of MIME
608+
* type.
609+
*
610+
* @since n.e.x.t
611+
*
612+
* @param bool $prefer_smaller_image_file Whether to prefer the smaller image file.
613+
*/
614+
if ( apply_filters( 'webp_uploads_prefer_smaller_image_file', false ) ) {
615+
$target_mimes = webp_uploads_get_mime_types_by_filesize( $target_mimes, $attachment_id );
616+
}
617+
602618
$target_mime = null;
603619
foreach ( $target_mimes as $mime ) {
604620
if ( isset( $metadata['sources'][ $mime ] ) ) {
@@ -933,4 +949,3 @@ function( $a, $b ) {
933949
// Create an array of available mime types ordered by smallest filesize.
934950
return array_keys( $sources );
935951
}
936-
add_filter( 'webp_uploads_content_image_mimes', 'webp_uploads_get_mime_types_by_filesize', 10, 2 );

0 commit comments

Comments
 (0)