From a38ecee8c9554dfd3b6152fc0b1950cd69838fc4 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Thu, 5 Sep 2019 22:26:50 +0000 Subject: [PATCH] Media: Add two new intermediate image sizes, 1536px and 2048px. They are meant to enhance the way WordPress displays images on the front-end on larger, high-density devices. They make it possible to generate more suitable `srcset` and `sizes` attributes, and not use the original, often non-optimized image. Also change the default `max_srcset_image_width` value to match the new max size. Props pierlo, azaozz. See #43524. git-svn-id: https://develop.svn.wordpress.org/trunk@46066 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/default-filters.php | 1 + src/wp-includes/media.php | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index f88398d0ee..fc0f4408f7 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -519,6 +519,7 @@ add_filter( 'the_content', 'do_shortcode', 11 ); // AFTER wpautop() // Media add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' ); add_action( 'customize_controls_enqueue_scripts', 'wp_plupload_default_settings' ); +add_action( 'plugins_loaded', '_wp_add_additional_image_sizes', 0 ); // Nav menu add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 ); diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index b9601c3f60..6f104a20c7 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1200,10 +1200,10 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac * * @since 4.4.0 * - * @param int $max_width The maximum image width to be included in the 'srcset'. Default '1600'. + * @param int $max_width The maximum image width to be included in the 'srcset'. Default '2048'. * @param array $size_array Array of width and height values in pixels (in that order). */ - $max_srcset_image_width = apply_filters( 'max_srcset_image_width', 1600, $size_array ); + $max_srcset_image_width = apply_filters( 'max_srcset_image_width', 2048, $size_array ); // Array to hold URL candidates. $sources = array(); @@ -4329,3 +4329,23 @@ function wp_media_personal_data_exporter( $email_address, $page = 1 ) { 'done' => $done, ); } + +/** + * Add additional default image sub-sizes. + * + * These sizes are meant to enhance the way WordPress displays images on the front-end on larger, + * high-density devices. They make it possible to generate more suitable `srcset` and `sizes` attributes + * when the users upload large images. + * + * The sizes can be changed or removed by themes and plugins but that is not recommended. + * The size "names" reflect the image dimensions, so changing the sizes would be quite misleading. + * + * @since 5.3.0 + * @access private + */ +function _wp_add_additional_image_sizes() { + // 2x medium_large size + add_image_size( '1536x1536', 1536, 1536 ); + // 2x large size + add_image_size( '2048x2048', 2048, 2048 ); +}