image_get_intermediate_size( int $post_id, string|int[] $size = 'thumbnail' ): array|false
- Since
- 2.5.0
- Source
wp-includes/media.php:780
Description
The $size parameter can be an array with the width and height respectively.
If the size matches the 'sizes' metadata array for width and height, then it will be used. If there is no direct match, then the nearest image size larger than the specified size will be used. If nothing is found, then the function will break out and return false.
The metadata 'sizes' is used for compatible sizes that can be used for the parameter $size value.
The url path will be given, when the $size parameter is a string.
If you are passing an array for the $size, you should consider using add_image_size() so that a cropped version is generated. It's much more efficient than having to find the closest-sized image and then having the browser scale down the image.
Compatibility
- WordPress
- since 2.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
$post_idint- Attachment ID.
$sizestring|int[]optional- Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'thumbnail'.Default:
'thumbnail'
Return value
array|false- Array of file relative path, width, and height on success. Additionally includes absolute path and URL if registered size is passed to
$sizeparameter. False on failure.$filestringFilename of image.$widthintWidth of image in pixels.$heightintHeight of image in pixels.$pathstringPath of image relative to uploads directory.$urlstringURL of image.
Performance profile
How much work a call to image_get_intermediate_size() 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
- Scales with input
- Instructions
- 8–129
- Plugin surface
- 1 hook
- Called by
- 3
Reaches the database via get_post().
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 201.
Third-party callbacks on 'image_get_intermediate_size' run inside this call, and their cost is not bounded by anything here.
3 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 image_get_intermediate_size() - optionoption read or write
get_option()one call below image_get_intermediate_size()
Further down the call graph this can also reach 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 · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost image_get_intermediate_size() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–71 | wp_get_attachment_metadata() |
is_array($imagedata) && !empty($imagedata) && !is_array($size) && !empty($data) | 28–35 | wp_get_attachment_metadata(), apply_filters() |
is_array($imagedata) && !empty($imagedata) && is_array($size) && !empty($candidates) && empty($data) | 50–83 | wp_get_attachment_metadata(), array_shift(), image_constrain_size_for_editor() |
is_array($imagedata) && !empty($imagedata) && is_array($size) && !empty($candidates) && empty($data) | 53–86 | wp_get_attachment_metadata(), ksort(), array_shift(), image_constrain_size_for_editor() |
is_array($imagedata) && !empty($imagedata) && !is_array($size) | 55–58 | wp_get_attachment_metadata(), wp_get_attachment_url(), path_join(), path_join(), apply_filters() |
is_array($imagedata) && !empty($imagedata) && is_array($size) && !empty($candidates) && !empty($data) | 58–95 | wp_get_attachment_metadata(), array_shift(), image_constrain_size_for_editor(), apply_filters() |
is_array($imagedata) && !empty($imagedata) && is_array($size) && empty($candidates) && !empty($value) && empty($data) | 61–94 | wp_get_attachment_metadata(), image_constrain_size_for_editor() |
is_array($imagedata) && !empty($imagedata) && is_array($size) && !empty($candidates) && !empty($data) | 61–98 | wp_get_attachment_metadata(), ksort(), array_shift(), image_constrain_size_for_editor(), apply_filters() |
is_array($imagedata) && !empty($imagedata) && is_array($size) && empty($candidates) && !empty($value) && !empty($data) | 69–106 | wp_get_attachment_metadata(), image_constrain_size_for_editor(), apply_filters() |
is_array($imagedata) && !empty($imagedata) && is_array($size) && !empty($candidates) | 85–118 | wp_get_attachment_metadata(), array_shift(), image_constrain_size_for_editor(), wp_get_attachment_url(), path_join(), path_join(), apply_filters() |
is_array($imagedata) && !empty($imagedata) && is_array($size) && !empty($candidates) | 88–121 | wp_get_attachment_metadata(), ksort(), array_shift(), image_constrain_size_for_editor(), wp_get_attachment_url(), path_join(), path_join(), apply_filters() |
is_array($imagedata) && !empty($imagedata) && is_array($size) && empty($candidates) && !empty($value) | 96–129 | wp_get_attachment_metadata(), image_constrain_size_for_editor(), wp_get_attachment_url(), path_join(), path_join(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 201 | 8–129 | 25 | |
| 8.5 | 201 | 8–129 | 25 | |
| 8.4 | 201 | 8–129 | 25 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 205 | 8–133 | 25 | |
| 8.2 | 205 | 8–133 | 25 | |
| 8.1 | 205 | 8–133 | 25 | |
| 7.4 | 205 | 8–133 | 25 |
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 image_get_intermediate_size() runs, in this order:
- apply_filters( image_get_intermediate_size )filterline 873 (+93 into the body)
Filters the output of image_get_intermediate_size()
Uses · 6
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- wp_image_matches_ratio()Helper function to test if aspect ratios for two images match.
- image_constrain_size_for_editor()Scales down the default size of an image.
- wp_get_attachment_url()Retrieves the URL for an attachment.
- path_join()Joins two filesystem paths together.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 3
- _load_image_to_edit_path()Retrieves the path or URL of an attachment's attached file.
- image_downsize()Scales an image to fit a particular size (such as 'thumb' or 'medium').
- wp_image_editor()Loads the WP image-editing interface.
Source code
function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { $imagedata = wp_get_attachment_metadata( $post_id ); if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) { return false; } $data = array(); // Find the best match when '$size' is an array. if ( is_array( $size ) ) { $candidates = array(); if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) { $imagedata['height'] = $imagedata['sizes']['full']['height']; $imagedata['width'] = $imagedata['sizes']['full']['width']; } foreach ( $imagedata['sizes'] as $_size => $data ) { // If there's an exact match to an existing image size, short circuit. if ( (int) $data['width'] === (int) $size[0] && (int) $data['height'] === (int) $size[1] ) { $candidates[ $data['width'] * $data['height'] ] = $data; break; } // If it's not an exact match, consider larger sizes with the same aspect ratio. if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) { // If '0' is passed to either size, we test ratios against the original file. if ( 0 === $size[0] || 0 === $size[1] ) { $same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $imagedata['width'], $imagedata['height'] ); } else { $same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] ); } if ( $same_ratio ) { $candidates[ $data['width'] * $data['height'] ] = $data; } } } if ( ! empty( $candidates ) ) { // Sort the array by size if we have more than one candidate. if ( 1 < count( $candidates ) ) { ksort( $candidates ); } $data = array_shift( $candidates ); } elseif ( ! empty( $imagedata['sizes']['thumbnail'] ) && $size[0] <= $imagedata['sizes']['thumbnail']['width'] && $size[1] <= $imagedata['sizes']['thumbnail']['width'] ) { /* * When the size requested is smaller than the thumbnail dimensions, we * fall back to the thumbnail size to maintain backward compatibility with * pre-4.6 versions of WordPress. */ $data = $imagedata['sizes']['thumbnail']; } else { return false; } // Constrain the width and height attributes to the requested values. list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); } elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) { $data = $imagedata['sizes'][ $size ]; } // If we still don't have a match at this point, return false. if ( empty( $data ) ) { return false; } // Include the full filesystem path of the intermediate file. if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) { $file_url = wp_get_attachment_url( $post_id ); $data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] ); $data['url'] = path_join( dirname( $file_url ), $data['file'] ); }Changelog
Introduced in 2.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.