Media: Consider inline image CSS width to backfill `width` and `height` attributes.

Prior to this changeset, WordPress core would use the original image size, which in the particular case of inline images would be severely off, as they are usually very small. This could lead to incorrect application of `fetchpriority="high"` and other performance optimizations.

Props westonruter, flixos90, joemcgill, mukesh27.
Fixes #59352.

Built from https://develop.svn.wordpress.org/trunk@57294


git-svn-id: http://core.svn.wordpress.org/trunk@56800 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2024-01-16 17:03:13 +00:00
parent 365a82ffeb
commit 14263ea394
2 changed files with 8 additions and 1 deletions

View File

@ -2117,6 +2117,13 @@ function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id
$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id );
if ( $size_array ) {
// If the width is enforced through style (e.g. in an inline image), calculate the dimension attributes.
$style_width = preg_match( '/style="width:\s*(\d+)px;"/', $image, $match_width ) ? (int) $match_width[1] : 0;
if ( $style_width ) {
$size_array[1] = (int) round( $size_array[1] * $style_width / $size_array[0] );
$size_array[0] = $style_width;
}
$hw = trim( image_hwstring( $size_array[0], $size_array[1] ) );
return str_replace( '<img', "<img {$hw}", $image );
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57290';
$wp_version = '6.5-alpha-57294';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.