image_downsize( int $id, string|int[] $size = 'medium' ): array|false
- Since
- 2.5.0
- Source
wp-includes/media.php:196
Description
The URL might be the original image, or it might be a resized version. This function won't create a new resized copy, it will just return an already resized one if it exists.
A plugin may use the 'image_downsize' filter to hook into and offer image resizing services for images. The hook must return an array with the same elements that are normally returned from the function.
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
$idint- Attachment ID for image.
$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 'medium'.Default:
'medium'
Return value
array|false- Array of image data, or boolean false if no image is available.
$0stringImage source URL.$1intImage width in pixels.$2intImage height in pixels.$3boolWhether the image is a resized image.
Performance profile
How much work a call to image_downsize() 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
- 15–123
- Plugin surface
- 1 hook
- Called by
- 4
Reads or writes the filesystem via file_exists().
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 136.
Third-party callbacks on 'image_downsize' 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 - filesystemfilesystem access
file_exists()called directly - querycontent query
get_post()one call below image_downsize() - optionoption read or write
get_option()one call below image_downsize()
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 · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost image_downsize() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15 | wp_attachment_is_image(), apply_filters() |
empty($value) | 34 | wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename() |
| always | 41–78 | wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename(), image_get_intermediate_size() |
| always | 55–92 | wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename(), image_get_intermediate_size(), image_constrain_size_for_editor() |
$size === "thumbnail" && !empty($meta) && !file_exists() | 64–93 | wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename(), image_get_intermediate_size(), get_attached_file(), wp_basename(), wp_basename(), file_exists() |
$size === "thumbnail" && !empty($meta) && file_exists() | 69–98 | wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename(), image_get_intermediate_size(), get_attached_file(), wp_basename(), wp_basename(), file_exists(), wp_getimagesize() |
$size === "thumbnail" && !empty($meta) && !file_exists() | 78–107 | wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename(), image_get_intermediate_size(), get_attached_file(), wp_basename(), wp_basename(), file_exists(), image_constrain_size_for_editor() |
$size === "thumbnail" && !empty($meta) && file_exists() | 80–109 | wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename(), image_get_intermediate_size(), get_attached_file(), wp_basename(), wp_basename(), file_exists(), wp_getimagesize(), wp_basename() |
$size === "thumbnail" && !empty($meta) && file_exists() | 83–112 | wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename(), image_get_intermediate_size(), get_attached_file(), wp_basename(), wp_basename(), file_exists(), wp_getimagesize(), image_constrain_size_for_editor() |
$size === "thumbnail" && !empty($meta) && file_exists() | 94–123 | wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename(), image_get_intermediate_size(), get_attached_file(), wp_basename(), wp_basename(), file_exists(), wp_getimagesize(), wp_basename(), image_constrain_size_for_editor() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 136 | 15–123 | 14 | |
| 8.5 | 136 | 15–123 | 14 | |
| 8.4 | 136 | 15–123 | 14 | 13 fewer instructions than PHP 8.3 |
| 8.3 | 149 | 15–133 | 14 | |
| 8.2 | 149 | 15–133 | 14 | |
| 8.1 | 149 | 15–133 | 14 | |
| 7.4 | 149 | 15–133 | 14 |
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_downsize() runs, in this order:
- apply_filters( image_downsize )filterline 212 (+16 into the body)
Filters whether to preempt the output of image_downsize().
Uses · 9
- wp_attachment_is_image()Determines whether an attachment is an image.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_get_attachment_url()Retrieves the URL for an attachment.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- wp_basename()i18n-friendly version of basename().
- image_get_intermediate_size()Retrieves the image's intermediate size (resized) path, width, and height.
- get_attached_file()Retrieves attached file path based on attachment ID.
- wp_getimagesize()Allows PHP's getimagesize() to be debuggable when necessary.
- image_constrain_size_for_editor()Scales down the default size of an image.
Used by · 4
- get_image_tag()Gets an img tag for an image attachment, scaling it down if requested.
- image_size_input_fields()Retrieves HTML for the size radio buttons with the specified one checked.
- wp_get_attachment_image_src()Retrieves an image to represent an attachment.
- wp_xmlrpc_server::_prepare_media_item()Prepares media item data for return in an XML-RPC object.
Source code
function image_downsize( $id, $size = 'medium' ) { $is_image = wp_attachment_is_image( $id ); /** * Filters whether to preempt the output of image_downsize(). * * Returning a truthy value from the filter will effectively short-circuit * down-sizing the image, returning that value instead. * * @since 2.5.0 * * @param bool|array $downsize Whether to short-circuit the image downsize. * @param int $id Attachment ID for image. * @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). */ $out = apply_filters( 'image_downsize', false, $id, $size ); if ( $out ) { return $out; } $img_url = wp_get_attachment_url( $id ); $meta = wp_get_attachment_metadata( $id ); $width = 0; $height = 0; $is_intermediate = false; $img_url_basename = wp_basename( $img_url ); /* * If the file isn't an image, attempt to replace its URL with a rendered image from its meta. * Otherwise, a non-image type could be returned. */ if ( ! $is_image ) { if ( ! empty( $meta['sizes']['full'] ) ) { $img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url ); $img_url_basename = $meta['sizes']['full']['file']; $width = $meta['sizes']['full']['width']; $height = $meta['sizes']['full']['height']; } else { return false; } } // Try for a new style intermediate size. $intermediate = image_get_intermediate_size( $id, $size ); if ( $intermediate ) { $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url ); $width = $intermediate['width']; $height = $intermediate['height']; $is_intermediate = true; } elseif ( 'thumbnail' === $size && ! empty( $meta['thumb'] ) && is_string( $meta['thumb'] ) ) { // Fall back to the old thumbnail. $imagefile = get_attached_file( $id ); $thumbfile = str_replace( wp_basename( $imagefile ), wp_basename( $meta['thumb'] ), $imagefile ); if ( file_exists( $thumbfile ) ) { $info = wp_getimagesize( $thumbfile ); if ( $info ) { $img_url = str_replace( $img_url_basename, wp_basename( $thumbfile ), $img_url ); $width = $info[0]; $height = $info[1]; $is_intermediate = true; } } } if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) { // Any other type: use the real image. $width = $meta['width']; $height = $meta['height']; } if ( $img_url ) { // We have the actual image size, but might need to further constrain it if content_width is narrower. list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size ); return array( $img_url, $width, $height, $is_intermediate );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 7.0.4 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.