wppaste
WordPress

wp_calculate_image_srcset( int[] $size_array, string $image_src, array $image_meta, int $attachment_id = 0 ): string|false

Since
4.4.0
Source
wp-includes/media.php:1363
A helper function to calculate the image sources to include in a 'srcset' attribute.

Compatibility

WordPress
since 4.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$size_arrayint[]
An array of width and height values.
  • $0int

    The width in pixels.
  • $1int

    The height in pixels.
$image_srcstring
The 'src' of the image.
$image_metaarray
The image meta data as returned by 'wp_get_attachment_metadata()'.
$attachment_idintoptional
The image attachment ID. Default 0.Default: 0

Return value

string|false
The 'srcset' attribute value. False on error or when only one source exists.

Performance profile

How much work a call to wp_calculate_image_srcset() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
15–138

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 204.

Plugin surface
3 hooks

Third-party callbacks on 'wp_calculate_image_srcset_meta', 'max_srcset_image_width', 'wp_calculate_image_srcset' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 14 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_calculate_image_srcset() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always15–28apply_filters()
!empty($image_meta) && isset($image_meta) && !$image_width && isset($value) && $image_src44apply_filters(), wp_basename()
!empty($image_meta) && isset($image_meta) && !$image_width91–104apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), wp_get_upload_dir(), trailingslashit(), is_ssl(), wp_basename(), preg_match(), apply_filters(), apply_filters()
!empty($image_meta) && isset($image_meta) && !$image_width95–108apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), trailingslashit(), wp_get_upload_dir(), trailingslashit(), is_ssl(), wp_basename(), preg_match(), apply_filters(), apply_filters()
!empty($image_meta) && isset($image_meta) && !$image_width && is_array($sources)103–112apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), wp_get_upload_dir(), trailingslashit(), is_ssl(), wp_basename(), preg_match(), apply_filters(), apply_filters(), rtrim()
!empty($image_meta) && isset($image_meta) && !$image_width && is_ssl() && !$image_baseurl && $domain !== false106–121apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), wp_get_upload_dir(), trailingslashit(), is_ssl(), parse_url(), wp_basename(), preg_match(), apply_filters(), apply_filters()
!empty($image_meta) && isset($image_meta) && !$image_width && is_array($sources)107–116apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), trailingslashit(), wp_get_upload_dir(), trailingslashit(), is_ssl(), wp_basename(), preg_match(), apply_filters(), apply_filters(), rtrim()
!empty($image_meta) && isset($image_meta) && !$image_width && is_ssl() && !$image_baseurl && $domain !== false110–125apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), trailingslashit(), wp_get_upload_dir(), trailingslashit(), is_ssl(), parse_url(), wp_basename(), preg_match(), apply_filters(), apply_filters()
!empty($image_meta) && isset($image_meta) && !$image_width && is_ssl() && !$image_baseurl && $domain === false111–126apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), wp_get_upload_dir(), trailingslashit(), is_ssl(), parse_url(), set_url_scheme(), wp_basename(), preg_match(), apply_filters(), apply_filters()
!empty($image_meta) && isset($image_meta) && !$image_width && is_ssl() && !$image_baseurl && $domain === false115–130apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), trailingslashit(), wp_get_upload_dir(), trailingslashit(), is_ssl(), parse_url(), set_url_scheme(), wp_basename(), preg_match(), apply_filters(), apply_filters()
!empty($image_meta) && isset($image_meta) && !$image_width && is_ssl() && !$image_baseurl && $domain !== false && is_array($sources)118–129apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), wp_get_upload_dir(), trailingslashit(), is_ssl(), parse_url(), wp_basename(), preg_match(), apply_filters(), apply_filters(), rtrim()
!empty($image_meta) && isset($image_meta) && !$image_width && is_ssl() && !$image_baseurl && $domain !== false && is_array($sources)122–133apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), trailingslashit(), wp_get_upload_dir(), trailingslashit(), is_ssl(), parse_url(), wp_basename(), preg_match(), apply_filters(), apply_filters(), rtrim()
2 further outcomes, up to 138 instructions
!empty($image_meta) && isset($image_meta) && !$image_width && is_ssl() && !$image_baseurl && $domain === false && is_array($sources)123–134apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), wp_get_upload_dir(), trailingslashit(), is_ssl(), parse_url(), set_url_scheme(), wp_basename(), preg_match(), apply_filters(), apply_filters(), rtrim()
!empty($image_meta) && isset($image_meta) && !$image_width && is_ssl() && !$image_baseurl && $domain === false && is_array($sources)127–138apply_filters(), wp_basename(), _wp_get_attachment_relative_path(), trailingslashit(), wp_get_upload_dir(), trailingslashit(), is_ssl(), parse_url(), set_url_scheme(), wp_basename(), preg_match(), apply_filters(), apply_filters(), rtrim()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev20415–13830
8.520415–13830
8.420415–1383015 fewer instructions than PHP 8.3
8.321915–14130
8.221915–14130
8.121915–141301 fewer instruction than PHP 7.4
7.422015–14130

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while wp_calculate_image_srcset() runs, in this order:

  1. apply_filters( wp_calculate_image_srcset_meta )filterline 1379 (+16 into the body)

    Pre-filters the image meta to be able to fix inconsistencies in the stored data.

  2. apply_filters( max_srcset_image_width )filterline 1464 (+101 into the body)

    Filters the maximum image width to be included in a 'srcset' attribute.

  3. apply_filters( wp_calculate_image_srcset )filterline 1551 (+188 into the body)

    Filters an image's 'srcset' sources.

Uses · 10

Used by · 4

Source code

function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {	/**	 * Pre-filters 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 int[]  $size_array    {	 *     An array of requested width and height values.	 *	 *     @type int $0 The width in pixels.	 *     @type int $1 The height in pixels.	 * }	 * @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 ( str_contains( $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() && ! str_starts_with( $image_baseurl, 'https' ) ) {		/*		 * Since the `Host:` header might contain a port, it should		 * be compared against the image URL using the same port.		 */		$parsed = parse_url( $image_baseurl );		$domain = $parsed['host'] ?? ''; 		if ( isset( $parsed['port'] ) ) {			$domain .= ':' . $parsed['port'];		} 		if ( $_SERVER['HTTP_HOST'] === $domain ) {			$image_baseurl = set_url_scheme( $image_baseurl, 'https' );		}	}

Changelog

Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.