函数文档

wp_image_src_get_dimensions()

💡 云策文档标注

概述

wp_image_src_get_dimensions() 函数用于基于图像源文件确定其宽度和高度尺寸。它通过检查图像元数据来匹配源文件,并返回尺寸数组或 false。

关键要点

  • 函数接受三个参数:$image_src(图像源文件,字符串,必需)、$image_meta(图像元数据数组,必需)、$attachment_id(附件 ID,整数,可选,默认 0)。
  • 返回值为数组或 false:数组的第一个元素是宽度,第二个元素是高度;如果无法确定尺寸,则返回 false。
  • 函数逻辑:首先检查是否为全尺寸图像,通过比较 $image_src 和 $image_meta['file'] 的 basename;如果不是,则遍历 $image_meta['sizes'] 来匹配缩略图尺寸。
  • 包含一个过滤器钩子 apply_filters('wp_image_src_get_dimensions', ...),允许开发者修改返回值。
  • 相关函数包括 wp_basename()、apply_filters(),并被 wp_img_tag_add_width_and_height_attr() 和 wp_image_add_srcset_and_sizes() 使用。
  • 自 WordPress 5.5.0 版本引入。

代码示例

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;
            }
        }
    }

    return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id );
}

注意事项

  • 确保 $image_meta 参数是通过 wp_get_attachment_metadata() 获取的正确元数据数组,否则函数可能无法正确工作。
  • 函数依赖于图像文件名匹配,因此源文件路径应准确,避免因路径问题导致尺寸获取失败。
  • 过滤器钩子允许自定义尺寸逻辑,开发者可以添加回调来修改或扩展功能。

📄 原文内容

Determines an image’s width and height dimensions based on the source file.

Parameters

$image_srcstringrequired
The image source file.
$image_metaarrayrequired
The image meta data as returned by ‘wp_get_attachment_metadata() ‘.
$attachment_idintoptional
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.

Source

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 );
}

Hooks

apply_filters( ‘wp_image_src_get_dimensions’, array|false $dimensions, string $image_src, array $image_meta, int $attachment_id )

Filters the ‘wp_image_src_get_dimensions’ value.

Changelog

Version Description
5.5.0 Introduced.