返回介绍

wp_calculate_image_srcset()

发布于 2017-09-11 11:38:45 字数 6565 浏览 995 评论 0 收藏 0

wp_calculate_image_srcset( array $size_array,  string $image_src,  array $image_meta,  int $attachment_id )

A helper function to calculate the image 源代码s to include in a ‘srcset’ attribute.


description


参数

$size_array

(array) (Required) Array of width and height values in pixels (in that order).

$image_src

(string) (Required) The 'src' of the image.

$image_meta

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

$attachment_id

(int) (Optional) The image attachment ID to pass to the filter. Default 0.


返回值

(string|bool) The 'srcset' attribute value. False on error or when only one 源代码 exists.


源代码

File: wp-includes/media.php

function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
	/**
	 * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
	 *
	 * @since 4.5.0
	 *
	 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
	 * @param array  $size_array    Array of width and height values in pixels (in that order).
	 * @param string $image_src     The 'src' of the image.
	 * @param int    $attachment_id The image attachment ID or 0 if not supplied.
	 */
	$image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );

	if ( empty( $image_meta['sizes'] ) || ! isset( $image_meta['file'] ) || strlen( $image_meta['file'] ) < 4 ) {
		return false;
	}

	$image_sizes = $image_meta['sizes'];

	// Get the width and height of the image.
	$image_width = (int) $size_array[0];
	$image_height = (int) $size_array[1];

	// Bail early if error/no width.
	if ( $image_width < 1 ) {
		return false;
	}

	$image_basename = wp_basename( $image_meta['file'] );

	/*
	 * WordPress flattens animated GIFs into one frame when generating intermediate sizes.
	 * To avoid hiding animation in user content, if src is a full size GIF, a srcset attribute is not generated.
	 * If src is an intermediate size GIF, the full size is excluded from srcset to keep a flattened GIF from becoming animated.
	 */
	if ( ! isset( $image_sizes['thumbnail']['mime-type'] ) || 'image/gif' !== $image_sizes['thumbnail']['mime-type'] ) {
		$image_sizes[] = array(
			'width'  => $image_meta['width'],
			'height' => $image_meta['height'],
			'file'   => $image_basename,
		);
	} elseif ( strpos( $image_src, $image_meta['file'] ) ) {
		return false;
	}

	// Retrieve the uploads sub-directory from the full size image.
	$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );

	if ( $dirname ) {
		$dirname = trailingslashit( $dirname );
	}

	$upload_dir = wp_get_upload_dir();
	$image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname;

	/*
	 * If currently on HTTPS, prefer HTTPS URLs when we know they're supported by the domain
	 * (which is to say, when they share the domain name of the current request).
	 */
	if ( is_ssl() && 'https' !== substr( $image_baseurl, 0, 5 ) && parse_url( $image_baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) {
		$image_baseurl = set_url_scheme( $image_baseurl, 'https' );
	}

	/*
	 * Images that have been edited in WordPress after being uploaded will
	 * contain a unique hash. Look for that hash and use it later to filter
	 * out images that are leftovers from previous versions.
	 */
	$image_edited = preg_match( '/-e[0-9]{13}/', wp_basename( $image_src ), $image_edit_hash );

	/**
	 * Filters the maximum image width to be included in a 'srcset' attribute.
	 *
	 * @since 4.4.0
	 *
	 * @param int   $max_width  The maximum image width to be included in the 'srcset'. Default '1600'.
	 * @param array $size_array Array of width and height values in pixels (in that order).
	 */
	$max_srcset_image_width = apply_filters( 'max_srcset_image_width', 1600, $size_array );

	// Array to hold URL candidates.
	$源代码s = array();

	/**
	 * To make sure the ID matches our image src, we will check to see if any sizes in our attachment
	 * meta match our $image_src. If no matches are found we don't return a srcset to avoid serving
	 * an incorrect image. See

更新日志

Versiondescription
4.4.0Introduced.

相关函数

Uses

  • wp-includes/media.php: wp_image_matches_ratio()
  • wp-includes/functions.php: wp_get_upload_dir()
  • wp-includes/media.php: wp_calculate_image_srcset_meta
  • wp-includes/media.php: _wp_get_attachment_relative_path()
  • wp-includes/media.php: max_srcset_image_width
  • wp-includes/media.php: wp_calculate_image_srcset
  • wp-includes/formatting.php: wp_basename()
  • wp-includes/formatting.php: trailingslashit()
  • wp-includes/load.php: is_ssl()
  • wp-includes/link-template.php: set_url_scheme()
  • wp-includes/plugin.php: apply_filters()
  • Show 6 more uses Hide more uses

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_srcset()
  • wp-includes/media.php: wp_get_attachment_image()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Joe McGill

    Note that only image sizes matching the aspect ratio for the original image will be returned by `wp_calculate_image_srcset()` since the `srcset` attribute is only meant to be used for resolution switching, not changing aspect ratios at different viewport widths (often referred to as the “art direction” use case). See: http://usecases.responsiveimages.org/.

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

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

发布评论

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