From 14263ea3945cbbbd2bbed1d20565255d638ce46a Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 16 Jan 2024 17:03:13 +0000 Subject: [PATCH] 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 --- wp-includes/media.php | 7 +++++++ wp-includes/version.php | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index 9cf301e95c..38ec2213b7 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -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( '