wp_calculate_image_sizes( string|int[] $size, string|null $image_src = null, array|null $image_meta = null, int $attachment_id = 0 ): string|false
- Since
- 4.4.0
- Source
wp-includes/media.php:1570
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
$sizestring|int[]- Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order).
$image_srcstring|nulloptional- The URL to the image file. Default null.Default:
null $image_metaarray|nulloptional- The image meta data as returned by 'wp_get_attachment_metadata()'.
Default null.Default:null $attachment_idintoptional- Image attachment ID. Either
$image_metaor$attachment_idis needed when using the image size name as argument for$size. Default 0.Default:0
Return value
string|false- A valid source size value for use in a 'sizes' attribute or false.
Performance profile
How much work a call to wp_calculate_image_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
- Heavy
- Scaling
- Constant
- Instructions
- 11–44
- Plugin surface
- 1 hook
- Called by
- 4
Reaches the database via get_post().
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 52.
Third-party callbacks on 'wp_calculate_image_sizes' 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 - querycontent query
get_post()one call below wp_calculate_image_sizes()
Further down the call graph this can also reach cache, serialize, option 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_sizes() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_array($size) | 11–15 | none |
is_array($size) | 16 | absint() |
!is_array($size) && is_string($size) && !is_array($image_meta) | 19 | wp_get_attachment_metadata() |
!is_array($size) && is_string($size) && is_array($image_meta) | 20–21 | _wp_get_image_size_from_meta() |
!is_array($size) | 24–28 | sprintf(), apply_filters() |
!is_array($size) && is_string($size) && is_array($image_meta) | 25 | wp_get_attachment_metadata(), _wp_get_image_size_from_meta() |
!is_array($size) && is_string($size) && is_array($image_meta) | 26–27 | _wp_get_image_size_from_meta(), absint() |
is_array($size) | 29 | absint(), sprintf(), apply_filters() |
!is_array($size) && is_string($size) && is_array($image_meta) | 31 | wp_get_attachment_metadata(), _wp_get_image_size_from_meta(), absint() |
!is_array($size) && is_string($size) && !is_array($image_meta) | 32 | wp_get_attachment_metadata(), sprintf(), apply_filters() |
!is_array($size) && is_string($size) && is_array($image_meta) | 33–34 | _wp_get_image_size_from_meta(), sprintf(), apply_filters() |
!is_array($size) && is_string($size) && is_array($image_meta) | 38 | wp_get_attachment_metadata(), _wp_get_image_size_from_meta(), sprintf(), apply_filters() |
2 further outcomes, up to 44 instructions
!is_array($size) && is_string($size) && is_array($image_meta) | 39–40 | _wp_get_image_size_from_meta(), absint(), sprintf(), apply_filters() |
!is_array($size) && is_string($size) && is_array($image_meta) | 44 | wp_get_attachment_metadata(), _wp_get_image_size_from_meta(), absint(), sprintf(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 52 instructions, 11–44 executed per call, 7 branches. The work does not change between versions.
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 · 1
One hook fires while wp_calculate_image_sizes() runs, in this order:
- apply_filters( wp_calculate_image_sizes )filterline 1607 (+37 into the body)
Filters the output of 'wp_calculate_image_sizes()'.
Uses · 4
- absint()Converts a value to non-negative integer.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- _wp_get_image_size_from_meta()Gets the image size as array from its meta data.
- apply_filters()Calls the callback functions that have been added to a filter hook.
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_sizes()Retrieves the value for an image attachment's 'sizes' attribute.
- wp_image_add_srcset_and_sizes()Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
Source code
function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) { $width = 0; if ( is_array( $size ) ) { $width = absint( $size[0] ); } elseif ( is_string( $size ) ) { if ( ! $image_meta && $attachment_id ) { $image_meta = wp_get_attachment_metadata( $attachment_id ); } if ( is_array( $image_meta ) ) { $size_array = _wp_get_image_size_from_meta( $size, $image_meta ); if ( $size_array ) { $width = absint( $size_array[0] ); } } } if ( ! $width ) { return false; } // Setup the default 'sizes' attribute. $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width ); /** * Filters the output of 'wp_calculate_image_sizes()'. * * @since 4.4.0 * * @param string $sizes A source size value for use in a 'sizes' attribute. * @param string|int[] $size Requested image size. Can be any registered image size name, or * an array of width and height values in pixels (in that order). * @param string|null $image_src The URL to the image file or null. * @param array|null $image_meta The image meta data as returned by wp_get_attachment_metadata() or null. * @param int $attachment_id Image attachment ID of the original image or 0. */ return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );}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.