返回介绍

wp_calculate_image_sizes()

发布于 2017-09-11 11:38:29 字数 3755 浏览 982 评论 0 收藏 0

wp_calculate_image_sizes( array|string $size,  string $image_src = null,  array $image_meta = null,  int $attachment_id )

Creates a ‘sizes’ attribute value for an image.


description


参数

$size

(array|string) (Required) Image size to retrieve. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default 'medium'.

$image_src

(string) (Optional) The URL to the image file.

Default value: null

$image_meta

(array) (Optional) The image meta data as returned by 'wp_get_attachment_metadata()'.

Default value: null

$attachment_id

(int) (Optional) Image attachment ID. Either $image_meta or $attachment_id is needed when using the image size name as argument for $size. Default 0.


返回值

(string|bool) A valid 源代码 size value for use in a 'sizes' attribute or false.


源代码

File: wp-includes/media.php

function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) {
	$width = 0;

	if ( is_array( $size ) ) {
		$width = absint( $size[0] );
	} elseif ( is_string( $size ) ) {
		if ( ! $image_meta && $attachment_id ) {
			$image_meta = wp_get_attachment_metadata( $attachment_id );
		}

		if ( is_array( $image_meta ) ) {
			$size_array = _wp_get_image_size_from_meta( $size, $image_meta );
			if ( $size_array ) {
				$width = absint( $size_array[0] );
			}
		}
	}

	if ( ! $width ) {
		return false;
	}

	// Setup the default 'sizes' attribute.
	$sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width );

	/**
	 * Filters the output of 'wp_calculate_image_sizes()'.
	 *
	 * @since 4.4.0
	 *
	 * @param string       $sizes         A 源代码 size value for use in a 'sizes' attribute.
	 * @param array|string $size          Requested size. Image size or array of width and height values
	 *                                    in pixels (in that order).
	 * @param string|null  $image_src     The URL to the image file or null.
	 * @param array|null   $image_meta    The image meta data as returned by wp_get_attachment_metadata() or null.
	 * @param int          $attachment_id Image attachment ID of the original image or 0.
	 */
	return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );
}

更新日志

Versiondescription
4.4.0Introduced.

相关函数

Uses

  • wp-includes/media.php: wp_calculate_image_sizes
  • wp-includes/media.php: _wp_get_image_size_from_meta()
  • wp-includes/functions.php: absint()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: wp_get_attachment_metadata()

Used By

  • wp-includes/theme.php: get_header_image_tag()
  • wp-includes/media.php: wp_image_add_srcset_and_sizes()
  • wp-includes/media.php: wp_get_attachment_image_sizes()
  • wp-includes/media.php: wp_get_attachment_image()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文