wp_image_src_get_dimensions( string $image_src, array $image_meta, int $attachment_id = 0 ): array|false
- Since
- 5.5.0
- Source
wp-includes/media.php:1715
Compatibility
- WordPress
- since 5.5.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
$image_srcstring- The image source file.
$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
array|false- Array with first element being the width and second element being the height, or false if dimensions cannot be determined.
Performance profile
How much work a call to wp_image_src_get_dimensions() 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–49
- Plugin surface
- 1 hook
- Called by
- 2
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 49.
Third-party callbacks on 'wp_image_src_get_dimensions' run inside this call, and their cost is not bounded by anything here.
2 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
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_image_src_get_dimensions() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($image_meta) | 15–17 | apply_filters() |
| always | 22–35 | wp_basename(), apply_filters() |
isset($image_meta) && !empty($image_meta) | 31–49 | wp_basename(), wp_basename(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 49 | 15–49 | 7 | |
| 8.5 | 49 | 15–49 | 7 | |
| 8.4 | 49 | 15–49 | 7 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 52 | 15–52 | 7 | |
| 8.2 | 52 | 15–52 | 7 | |
| 8.1 | 52 | 15–52 | 7 | |
| 7.4 | 52 | 15–52 | 7 |
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_image_src_get_dimensions() runs, in this order:
- apply_filters( wp_image_src_get_dimensions )filterline 1757 (+42 into the body)
Filters the 'wp_image_src_get_dimensions' value.
Uses · 3
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_basename()i18n-friendly version of basename().
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 2
- wp_image_add_srcset_and_sizes()Adds 'srcset' and 'sizes' attributes to an existing 'img' element.
- wp_img_tag_add_width_and_height_attr()Adds `width` and `height` attributes to an `img` HTML tag.
Source code
function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) { $dimensions = false; // Is it a full size image? if ( isset( $image_meta['file'] ) && str_contains( $image_src, wp_basename( $image_meta['file'] ) ) ) { $dimensions = array( (int) $image_meta['width'], (int) $image_meta['height'], ); } if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) { $src_filename = wp_basename( $image_src ); foreach ( $image_meta['sizes'] as $image_size_data ) { if ( $src_filename === $image_size_data['file'] ) { $dimensions = array( (int) $image_size_data['width'], (int) $image_size_data['height'], ); break; } } } /** * Filters the 'wp_image_src_get_dimensions' value. * * @since 5.7.0 * * @param array|false $dimensions Array with first element being the width * and second element being the height, or * false if dimensions could not be determined. * @param string $image_src The image source file. * @param array $image_meta The image meta data as returned by * 'wp_get_attachment_metadata()'. * @param int $attachment_id The image attachment ID. Default 0. */ return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id );}Changelog
Introduced in 5.5.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.