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:1343
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.
$0intThe width in pixels.$1intThe 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
- Scaling
- Scales with input
- Instructions
- 15–139
- Plugin surface
- 3 hooks
- Called by
- 4
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 206.
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.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15–28 | apply_filters() |
!empty($image_meta) && isset($image_meta) && !$image_width && isset($value) && $image_src | 44 | apply_filters(), wp_basename() |
!empty($image_meta) && isset($image_meta) && !$image_width | 91–104 | apply_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_width | 95–108 | apply_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–112 | apply_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 !== false | 107–122 | apply_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–116 | apply_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 !== false | 111–126 | apply_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 === false | 112–127 | apply_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 === false | 116–131 | apply_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) | 119–130 | apply_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) | 123–134 | apply_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 139 instructions
!empty($image_meta) && isset($image_meta) && !$image_width && is_ssl() && !$image_baseurl && $domain === false && is_array($sources) | 124–135 | apply_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) | 128–139 | apply_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 206 | 15–139 | 30 | |
| 8.5 | 206 | 15–139 | 30 | |
| 8.4 | 206 | 15–139 | 30 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 221 | 15–142 | 30 | |
| 8.2 | 221 | 15–142 | 30 | |
| 8.1 | 221 | 15–142 | 30 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 223 | 15–143 | 30 |
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:
- apply_filters( wp_calculate_image_srcset_meta )filterline 1359 (+16 into the body)
Pre-filters the image meta to be able to fix inconsistencies in the stored data.
- apply_filters( max_srcset_image_width )filterline 1444 (+101 into the body)
Filters the maximum image width to be included in a 'srcset' attribute.
- apply_filters( wp_calculate_image_srcset )filterline 1531 (+188 into the body)
Filters an image's 'srcset' sources.
Uses · 10
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_basename()i18n-friendly version of basename().
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- _wp_get_attachment_relative_path()Gets the attachment path relative to the upload directory.
- trailingslashit()Appends a trailing slash.
- wp_get_upload_dir()Retrieves uploads directory information.
- is_ssl()Determines if SSL is used.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- set_url_scheme()Sets the scheme for a URL.
- wp_image_matches_ratio()Helper function to test if aspect ratios for two images match.
Used by · 4
- get_header_image_tag()Creates image tag markup for a custom header image.
- wp_get_attachment_image()Gets an HTML img element representing an image attachment.
- wp_get_attachment_image_srcset()Retrieves the value for an image attachment's 'srcset' attribute.
- wp_image_add_srcset_and_sizes()Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
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 = isset( $parsed['host'] ) ? $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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.