wp_image_add_srcset_and_sizes( string $image, array $image_meta, int $attachment_id ): string
- Since
- 4.4.0
- Source
wp-includes/media.php:1748
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
$imagestring- An HTML 'img' element to be filtered.
$image_metaarray- The image meta data as returned by 'wp_get_attachment_metadata()'.
$attachment_idint- Image attachment ID.
Return value
string- Converted 'img' element with 'srcset' and 'sizes' attributes added.
Performance profile
How much work a call to wp_image_add_srcset_and_sizes() 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
- Trivial
- Scaling
- Constant
- Instructions
- 6–103
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 115.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_image_add_srcset_and_sizes()
Further down the call graph this can also reach query, option, cache, serialize 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 · 29 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_image_add_srcset_and_sizes() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($image_meta) | 6 | none |
!empty($image_meta) | 22–23 | preg_match(), explode() |
!empty($image_meta) && preg_match() | 35–36 | preg_match(), explode(), preg_match(), wp_basename() |
!empty($image_meta) && !preg_match() | 53–59 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_image_src_get_dimensions() |
!empty($image_meta) && !preg_match() | 59–68 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_calculate_image_srcset() |
!empty($image_meta) && preg_match() | 59–65 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_image_src_get_dimensions() |
!empty($image_meta) && !preg_match() | 61–71 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset() |
!empty($image_meta) && preg_match() | 65–74 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_calculate_image_srcset() |
!empty($image_meta) && preg_match() | 67–77 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset() |
!empty($image_meta) && !preg_match() && !$image | 69–75 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_calculate_image_srcset(), wp_calculate_image_sizes() |
!empty($image_meta) && !preg_match() && !$image | 71–78 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), wp_calculate_image_sizes() |
!empty($image_meta) && !preg_match() && $image && !is_string($sizes) | 75–80 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_calculate_image_srcset(), esc_attr() |
17 further outcomes, up to 103 instructions
!empty($image_meta) && preg_match() && !$image | 75–81 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_calculate_image_srcset(), wp_calculate_image_sizes() |
!empty($image_meta) && !preg_match() && $image && !is_string($sizes) | 77–83 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), esc_attr() |
!empty($image_meta) && preg_match() && !$image | 77–84 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), wp_calculate_image_sizes() |
!empty($image_meta) && preg_match() && $image && !is_string($sizes) | 81–86 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_calculate_image_srcset(), esc_attr() |
!empty($image_meta) && !preg_match() && $image && is_string($sizes) | 82–87 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_calculate_image_srcset(), esc_attr(), esc_attr() |
!empty($image_meta) && !preg_match() && !$image && !is_string($sizes) | 82–87 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), esc_attr() |
!empty($image_meta) && preg_match() && $image && !is_string($sizes) | 83–89 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), esc_attr() |
!empty($image_meta) && !preg_match() && $image && is_string($sizes) | 84–90 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), esc_attr(), esc_attr() |
!empty($image_meta) && !preg_match() && !$image && !is_string($sizes) | 84–90 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), esc_attr() |
!empty($image_meta) && preg_match() && $image && is_string($sizes) | 88–93 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_calculate_image_srcset(), esc_attr(), esc_attr() |
!empty($image_meta) && preg_match() && !$image && !is_string($sizes) | 88–93 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), esc_attr() |
!empty($image_meta) && !preg_match() && !$image && is_string($sizes) | 89–94 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), esc_attr(), esc_attr() |
!empty($image_meta) && preg_match() && $image && is_string($sizes) | 90–96 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), esc_attr(), esc_attr() |
!empty($image_meta) && preg_match() && !$image && !is_string($sizes) | 90–96 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), esc_attr() |
!empty($image_meta) && !preg_match() && !$image && is_string($sizes) | 91–97 | preg_match(), explode(), preg_match(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), esc_attr(), esc_attr() |
!empty($image_meta) && preg_match() && !$image && is_string($sizes) | 95–100 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), esc_attr(), esc_attr() |
!empty($image_meta) && preg_match() && !$image && is_string($sizes) | 97–103 | preg_match(), explode(), preg_match(), wp_basename(), preg_match(), preg_match(), wp_image_src_get_dimensions(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), esc_attr(), esc_attr() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 115 | 6–103 | 15 | |
| 8.5 | 115 | 6–103 | 15 | |
| 8.4 | 115 | 6–103 | 15 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 127 | 6–115 | 15 | |
| 8.2 | 127 | 6–115 | 15 | |
| 8.1 | 127 | 6–115 | 15 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 131 | 6–119 | 15 |
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.
Uses · 6
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_basename()i18n-friendly version of basename().
- wp_image_src_get_dimensions()Determines an image's width and height dimensions based on the source file.
- wp_calculate_image_srcset()A helper function to calculate the image sources to include in a 'srcset' attribute.
- wp_calculate_image_sizes()Creates a 'sizes' attribute value for an image.
- esc_attr()Escaping for HTML attributes.
Used by · 1
- wp_img_tag_add_srcset_and_sizes_attr()Adds `srcset` and `sizes` attributes to an existing `img` HTML tag.
Source code
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { // Ensure the image meta exists. if ( empty( $image_meta['sizes'] ) ) { return $image; } $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; list( $image_src ) = explode( '?', $image_src ); // Return early if we couldn't get the image source. if ( ! $image_src ) { return $image; } // Bail early if an image has been inserted and later edited. if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && ! str_contains( wp_basename( $image_src ), $img_edit_hash[0] ) ) { return $image; } $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; if ( $width && $height ) { $size_array = array( $width, $height ); } else { $size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id ); if ( ! $size_array ) { return $image; } } $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); if ( $srcset ) { // Check if there is already a 'sizes' attribute. $sizes = strpos( $image, ' sizes=' ); if ( ! $sizes ) { $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); } } if ( $srcset && $sizes ) { // Format the 'srcset' and 'sizes' string and escape attributes. $attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) ); if ( is_string( $sizes ) ) { $attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) ); } // Add the srcset and sizes attributes to the image markup. return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image ); } return $image;}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.7.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.