Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce webp_uploads_prefer_smaller_image_file filter allowing to opt in to preferring the smaller image file #287

Merged
merged 3 commits into from
Apr 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,22 @@ 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.
*
* 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 );
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
}

$target_mime = null;
foreach ( $target_mimes as $mime ) {
if ( isset( $metadata['sources'][ $mime ] ) ) {
Expand Down Expand Up @@ -933,4 +949,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 );