How to get an image intermediate size in WordPress

The image_get_intermediate_size function in WordPress is used to retrieve the URL of an intermediate image size for a given image attachment. This can be useful when you want to display a specific size of an image on your website, such as a thumbnail or a medium-sized image, without having to manually resize and upload the image multiple times.

By using the image_get_intermediate_size function, you can dynamically retrieve the URL of the desired image size based on the original image attachment, saving you time and effort in managing different image sizes for your website.

Parameters Accepted by image_get_intermediate_size Function

  • $post_id (int, required): Attachment ID.
  • $size (string|int[], optional, default: ‘thumbnail’): Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default size is ‘thumbnail’.

Value Returned by image_get_intermediate_size Function

The function returns an array of file relative path, width, and height on success. Additionally, it includes the absolute path and URL if a registered size is passed to the $size parameter. If the function fails, it returns false.

  • file (string): Path of the image relative to the uploads directory.
  • width (int): Width of the image in pixels.
  • height (int): Height of the image in pixels.
  • path (string): Absolute filesystem path of the image.
  • url (string): URL of the image.

Examples

How to get the URL of the medium size image of a WordPress attachment

$image_id = 123;
$medium_image_url = image_get_intermediate_size( $image_id, 'medium' );
echo $medium_image_url;

This code snippet retrieves the URL of the medium size image for a specific attachment in WordPress. It uses the image_get_intermediate_size function to get the URL and then echoes it to display on the webpage.

How to check if a specific size of image exists for a WordPress attachment

$image_id = 123;
$thumbnail_exists = image_get_intermediate_size( $image_id, 'thumbnail' );
if ( $thumbnail_exists ) {
 echo 'Thumbnail size image exists for this attachment.';
} else {
 echo 'Thumbnail size image does not exist for this attachment.';
}

This code snippet checks if the thumbnail size image exists for a specific attachment in WordPress. It uses the image_get_intermediate_size function to get the thumbnail size image URL and then uses an if statement to display a message based on whether the thumbnail exists or not.

How to get the URL of the full size image of a WordPress attachment if a specific size is not found

$image_id = 123;
$custom_image_url = image_get_intermediate_size( $image_id, 'custom_size' );
if ( ! $custom_image_url ) {
 $full_image_url = wp_get_attachment_url( $image_id );
 echo $full_image_url;
} else {
 echo $custom_image_url;
}

This code snippet first tries to retrieve the URL of a custom size image for a specific attachment in WordPress using the image_get_intermediate_size function. If the custom size image is not found, it falls back to getting the URL of the full size image using the wp_get_attachment_url function. It then echoes the URL to display on the webpage.

Conclusion

In conclusion, the image_get_intermediate_size function is a valuable tool for WordPress developers and designers looking to retrieve the URL or dimensions of an intermediate image size. By leveraging this function, users can easily access and manipulate image data within their WordPress themes or plugins, enhancing the overall user experience. With its simple syntax and powerful capabilities, image_get_intermediate_size is a versatile addition to any developer’s toolkit.

Related WordPress Functions