Title: image_downsize
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#wp--skip-link--target)

Scales an image to fit a particular size (such as ‘thumb’ or ‘medium’).

## 󠀁[Description](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#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’](https://developer.wordpress.org/reference/hooks/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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#parameters)󠁿

 `$id`intrequired

Attachment ID for image.

`$size`string|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](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#return)󠁿

 array|false Array of image data, or boolean false if no image is available.

 * `0` string
 * Image source URL.
 * `1` int
 * Image width in pixels.
 * `2` int
 * Image height in pixels.
 * `3` bool
 * Whether the image is a resized image.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#source)󠁿

    ```php
    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 );
    	}

    	return false;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/media.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/media.php#L204)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/media.php#L204-L287)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#hooks)󠁿

 [apply_filters( ‘image_downsize’, bool|array $downsize, int $id, string|int[] $size )](https://developer.wordpress.org/reference/hooks/image_downsize/)

Filters whether to preempt the output of [image_downsize()](https://developer.wordpress.org/reference/functions/image_downsize/).

## 󠀁[Related](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_getimagesize()](https://developer.wordpress.org/reference/functions/wp_getimagesize/)`wp-includes/media.php` |

Allows PHP’s getimagesize() to be debuggable when necessary.

  | 
| [image_get_intermediate_size()](https://developer.wordpress.org/reference/functions/image_get_intermediate_size/)`wp-includes/media.php` |

Retrieves the image’s intermediate size (resized) path, width, and height.

  | 
| [image_constrain_size_for_editor()](https://developer.wordpress.org/reference/functions/image_constrain_size_for_editor/)`wp-includes/media.php` |

Scales down the default size of an image.

  | 
| [wp_attachment_is_image()](https://developer.wordpress.org/reference/functions/wp_attachment_is_image/)`wp-includes/post.php` |

Determines whether an attachment is an image.

  | 
| [wp_get_attachment_url()](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/)`wp-includes/post.php` |

Retrieves the URL for an attachment.

  | 
| [wp_get_attachment_metadata()](https://developer.wordpress.org/reference/functions/wp_get_attachment_metadata/)`wp-includes/post.php` |

Retrieves attachment metadata for attachment ID.

  | 
| [get_attached_file()](https://developer.wordpress.org/reference/functions/get_attached_file/)`wp-includes/post.php` |

Retrieves attached file path based on attachment ID.

  | 
| [wp_basename()](https://developer.wordpress.org/reference/functions/wp_basename/)`wp-includes/formatting.php` |

i18n-friendly version of basename().

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 4 more](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#)

| Used by | Description | 
| [image_size_input_fields()](https://developer.wordpress.org/reference/functions/image_size_input_fields/)`wp-admin/includes/media.php` |

Retrieves HTML for the size radio buttons with the specified one checked.

  | 
| [get_image_tag()](https://developer.wordpress.org/reference/functions/get_image_tag/)`wp-includes/media.php` |

Gets an img tag for an image attachment, scaling it down if requested.

  | 
| [wp_get_attachment_image_src()](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/)`wp-includes/media.php` |

Retrieves an image to represent an attachment.

  | 
| [wp_xmlrpc_server::_prepare_media_item()](https://developer.wordpress.org/reference/classes/wp_xmlrpc_server/_prepare_media_item/)`wp-includes/class-wp-xmlrpc-server.php` |

Prepares media item data for return in an XML-RPC object.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/image_downsize/?output_format=md#comment-content-1099)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/image_downsize/#comment-1099)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fimage_downsize%2F%23comment-1099)
    Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fimage_downsize%2F%23comment-1099)
 4. **Add a function to select medium attachment images**
 5. Maybe you already know [wp_get_attachment_thumb_url()](https://developer.wordpress.org/reference/functions/wp_get_attachment_thumb_url/);.
    Now I’m showing how to do the same thing to return the url for the medium sized
    attachment.
 6.     ```php
        function wp_get_attachment_medium_url( $id )
        {
            $medium_array = image_downsize( $id, 'medium' );
            $medium_path = $medium_array[0];
    
            return $medium_path;
        }
        ```
    
 7. $id is the ID of the attachment. This can be really useful for plugins like WP-
    Choose-Thumb. With this function you can get the medium sized previews.
 8.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fimage_downsize%2F%3Freplytocom%3D1099%23feedback-editor-1099)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fimage_downsize%2F)
before being able to contribute a note or feedback.