wp_calculate_image_srcset() - WordPress 函数参考中文文档



发布于 2017-09-11 字数6576 浏览 848 评论 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.




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


The ‘src’ of the image.


The image meta data as returned by ‘wp_get_attachment_metadata()’.


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


Version 描述
4.4.0 Introduced.



  • wp-includes/media.php:
  • wp-includes/functions.php:
  • wp-includes/media.php:
  • wp-includes/media.php:
  • wp-includes/media.php:
  • wp-includes/media.php:
  • wp-includes/formatting.php:
  • wp-includes/formatting.php:
  • wp-includes/load.php:
  • wp-includes/link-template.php:
  • wp-includes/plugin.php:
  • Show 6 more uses
    Hide more uses

Used By

  • wp-includes/theme.php:
  • wp-includes/media.php:
  • wp-includes/media.php:
  • wp-includes/media.php:

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/.


需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。