Make WordPress Core

Changeset 57294


Ignore:
Timestamp:
01/16/2024 05:01:30 PM (4 months ago)
Author:
flixos90
Message:

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.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r57263 r57294  
    21182118
    21192119        if ( $size_array ) {
     2120            // If the width is enforced through style (e.g. in an inline image), calculate the dimension attributes.
     2121            $style_width = preg_match( '/style="width:\s*(\d+)px;"/', $image, $match_width ) ? (int) $match_width[1] : 0;
     2122            if ( $style_width ) {
     2123                $size_array[1] = (int) round( $size_array[1] * $style_width / $size_array[0] );
     2124                $size_array[0] = $style_width;
     2125            }
     2126
    21202127            $hw = trim( image_hwstring( $size_array[0], $size_array[1] ) );
    21212128            return str_replace( '<img', "<img {$hw}", $image );
Note: See TracChangeset for help on using the changeset viewer.