Make WordPress Core


Ignore:
Timestamp:
02/02/2021 02:57:22 AM (4 years ago)
Author:
joemcgill
Message:

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.

File:
1 edited

Legend:

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

    r49979 r50134  
    16001600 */
    16011601function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) {
    1602     if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta, $attachment_id ) ) {
    1603         return false;
    1604     }
     1602    $dimensions = false;
    16051603
    16061604    // Is it a full size image?
    16071605    if ( strpos( $image_src, $image_meta['file'] ) !== false ) {
    1608         return array(
     1606        $dimensions = array(
    16091607            (int) $image_meta['width'],
    16101608            (int) $image_meta['height'],
     
    16121610    }
    16131611
    1614     if ( ! empty( $image_meta['sizes'] ) ) {
     1612    if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) {
    16151613        $src_filename = wp_basename( $image_src );
    16161614
    16171615        foreach ( $image_meta['sizes'] as $image_size_data ) {
    16181616            if ( $src_filename === $image_size_data['file'] ) {
    1619                 return array(
     1617                $dimensions = array(
    16201618                    (int) $image_size_data['width'],
    16211619                    (int) $image_size_data['height'],
    16221620                );
     1621
     1622                break;
    16231623            }
    16241624        }
    16251625    }
    16261626
    1627     return false;
     1627    /**
     1628     * Filter the 'wp_image_src_get_dimensions' value.
     1629     *
     1630     * @since 5.7.0
     1631     *
     1632     * @param array|false $dimensions    Array with first element being the width
     1633     *                                   and second element being the height, or
     1634     *                                   false if dimensions could not be determined.
     1635     * @param string      $image_src     The image source file.
     1636     * @param array       $image_meta    The image meta data as returned by
     1637     *                                   'wp_get_attachment_metadata()'.
     1638     * @param int         $attachment_id The image attachment ID. Default 0.
     1639     */
     1640    return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id );
    16281641}
    16291642
Note: See TracChangeset for help on using the changeset viewer.