wppaste
WordPress

image_downsize( int $id, string|int[] $size = 'medium' ): array|false

Since
2.5.0
Source
wp-includes/media.php:196
Scales an image to fit a particular size (such as 'thumb' or 'medium').

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.
  • $0string

    Image source URL.
  • $1int

    Image width in pixels.
  • $2int

    Image height in pixels.
  • $3bool

    Whether 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

Reads or writes the filesystem via file_exists().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–123

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 136.

Plugin surface
1 hook

Third-party callbacks on 'image_downsize' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_exists()called directly
  • querycontent queryget_post()one call below image_downsize()
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
always15wp_attachment_is_image(), apply_filters()
empty($value)34wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename()
always41–78wp_attachment_is_image(), apply_filters(), wp_get_attachment_url(), wp_get_attachment_metadata(), wp_basename(), image_get_intermediate_size()
always55–92wp_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–93wp_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–98wp_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–107wp_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–109wp_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–112wp_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–123wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev13615–12314
8.513615–12314
8.413615–1231413 fewer instructions than PHP 8.3
8.314915–13314
8.214915–13314
8.114915–13314
7.414915–13314

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:

  1. apply_filters( image_downsize )filterline 212 (+16 into the body)

    Filters whether to preempt the output of image_downsize().

Uses · 9

Used by · 4

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.