wp_image_src_get_dimensions

The timeline below displays how wordpress function wp_image_src_get_dimensions has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Determines an image's width and height dimensions based on the source file.
 *
 * @since 5.5.0
 *
 * @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 Optional. The image attachment ID. Default 0.
 * @return array|false Array with first element being the width and second element being the height,
 *                     or false if dimensions cannot be determined.
 */
function wp_image_src_get_dimensions($image_src, $image_meta, $attachment_id = 0)
{
    $dimensions = false;
    // Is it a full size image?
    if (isset($image_meta['file']) && str_contains($image_src, wp_basename($image_meta['file']))) {
        $dimensions = array((int) $image_meta['width'], (int) $image_meta['height']);
    }
    if (!$dimensions && !empty($image_meta['sizes'])) {
        $src_filename = wp_basename($image_src);
        foreach ($image_meta['sizes'] as $image_size_data) {
            if ($src_filename === $image_size_data['file']) {
                $dimensions = array((int) $image_size_data['width'], (int) $image_size_data['height']);
                break;
            }
        }
    }
    /**
     * Filters 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);
}

WordPress Version: 5.7

/**
 * Determines an image's width and height dimensions based on the source file.
 *
 * @since 5.5.0
 *
 * @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 Optional. The image attachment ID. Default 0.
 * @return array|false Array with first element being the width and second element being the height,
 *                     or false if dimensions cannot be determined.
 */
function wp_image_src_get_dimensions($image_src, $image_meta, $attachment_id = 0)
{
    $dimensions = false;
    // Is it a full size image?
    if (isset($image_meta['file']) && strpos($image_src, wp_basename($image_meta['file'])) !== false) {
        $dimensions = array((int) $image_meta['width'], (int) $image_meta['height']);
    }
    if (!$dimensions && !empty($image_meta['sizes'])) {
        $src_filename = wp_basename($image_src);
        foreach ($image_meta['sizes'] as $image_size_data) {
            if ($src_filename === $image_size_data['file']) {
                $dimensions = array((int) $image_size_data['width'], (int) $image_size_data['height']);
                break;
            }
        }
    }
    /**
     * Filters 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);
}

WordPress Version: 5.5

/**
 * Determines an image's width and height dimensions based on the source file.
 *
 * @since 5.5.0
 *
 * @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 Optional. The image attachment ID. Default 0.
 * @return array|false Array with first element being the width and second element being the height,
 *                     or false if dimensions cannot be determined.
 */
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)) {
        return false;
    }
    // Is it a full size image?
    if (strpos($image_src, $image_meta['file']) !== false) {
        return array((int) $image_meta['width'], (int) $image_meta['height']);
    }
    if (!empty($image_meta['sizes'])) {
        $src_filename = wp_basename($image_src);
        foreach ($image_meta['sizes'] as $image_size_data) {
            if ($src_filename === $image_size_data['file']) {
                return array((int) $image_size_data['width'], (int) $image_size_data['height']);
            }
        }
    }
    return false;
}