Media: Add filter to wp_image_src_get_dimensions.

This adds a new filter, `wp_image_src_get_dimensions` to the `wp_image_src_get_dimensions()` function to correct the dimensions returned for a file whenever WordPress isn't able to correctly get the dimensions from attachment metadata.

Fixes #51865.

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


git-svn-id: http://core.svn.wordpress.org/trunk@49813 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Joe McGill 2021-02-02 02:59:05 +00:00
parent 683e767517
commit 5ca780edb1
2 changed files with 21 additions and 8 deletions

View File

@ -1599,32 +1599,45 @@ function wp_image_file_matches_image_meta( $image_location, $image_meta, $attach
* or false if dimensions cannot be determined. * or false if dimensions cannot be determined.
*/ */
function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) { function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) {
if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta, $attachment_id ) ) { $dimensions = false;
return false;
}
// Is it a full size image? // Is it a full size image?
if ( strpos( $image_src, $image_meta['file'] ) !== false ) { if ( strpos( $image_src, $image_meta['file'] ) !== false ) {
return array( $dimensions = array(
(int) $image_meta['width'], (int) $image_meta['width'],
(int) $image_meta['height'], (int) $image_meta['height'],
); );
} }
if ( ! empty( $image_meta['sizes'] ) ) { if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) {
$src_filename = wp_basename( $image_src ); $src_filename = wp_basename( $image_src );
foreach ( $image_meta['sizes'] as $image_size_data ) { foreach ( $image_meta['sizes'] as $image_size_data ) {
if ( $src_filename === $image_size_data['file'] ) { if ( $src_filename === $image_size_data['file'] ) {
return array( $dimensions = array(
(int) $image_size_data['width'], (int) $image_size_data['width'],
(int) $image_size_data['height'], (int) $image_size_data['height'],
); );
break;
} }
} }
} }
return false; /**
* Filter the 'wp_image_src_get_dimensions' value.
*
* @since 5.7.0
*
* @param array|false $dimensions Array with first element being the width
* and second element being the height, or
* false if dimensions could not be determined.
* @param string $image_src The image source file.
* @param array $image_meta The image meta data as returned by
* 'wp_get_attachment_metadata()'.
* @param int $attachment_id The image attachment ID. Default 0.
*/
return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id );
} }
/** /**

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.7-alpha-50133'; $wp_version = '5.7-alpha-50134';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.