Responsive Images: limit the size of images included in srcset attributes. Introduce max_srcset_image_width filter to adjust the limit.

Props joemcgill.
Fixes #34341.

git-svn-id: https://develop.svn.wordpress.org/trunk@35358 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz
2015-10-22 19:30:12 +00:00
parent 7adf6e9125
commit 5aa7a8ce78

View File

@@ -928,6 +928,15 @@ function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium'
*/
$img_edited = preg_match( '/-e[0-9]{13}/', $img_url, $img_edit_hash );
/**
* Filter the maximum width included in a srcset attribute.
*
* @since 4.4.0
*
* @param array|string $size Size of image, either array or string.
*/
$max_srcset_width = apply_filters( 'max_srcset_image_width', 1600, $size );
/*
* Set up arrays to hold url candidates and matched image sources so
* we can avoid duplicates without looping through the full sources array
@@ -945,6 +954,11 @@ function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium'
continue;
}
// Filter out images that are wider than $max_srcset_width.
if ( $max_srcset_width && $img['width'] > $max_srcset_width ) {
$contiue;
}
$candidate_url = path_join( dirname( $img_url ), $img['file'] );
// Calculate the new image ratio.