The image_get_intermediate_size() WordPress PHP function retrieves the image’s intermediate size (resized) path, width, and height.
Usage
image_get_intermediate_size($post_id, $size);
Custom example:
Input:
$image_info = image_get_intermediate_size(123, 'medium');
Output:
Array containing ‘file’, ‘width’, ‘height’, and ‘path’ of the resized image.
Parameters
$post_id (int)
– Required. The attachment ID of the image.$size (string|array)
– Optional. The image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default is ‘thumbnail’.
More information
See WordPress Developer Resources: image_get_intermediate_size()
Examples
Get image info for a specific size
This example retrieves information about the medium-sized image for the given attachment ID.
$image_info = image_get_intermediate_size(123, 'medium'); print_r($image_info);
Get image URL for a specific size
This example retrieves the URL of the medium-sized image for the given attachment ID.
$image_info = image_get_intermediate_size(123, 'medium'); $image_url = wp_get_attachment_url($image_info['file']); echo $image_url;
Get image info for a custom size
This example retrieves information about a custom-sized image for the given attachment ID.
$image_info = image_get_intermediate_size(123, array(300, 200)); print_r($image_info);
Check if image exists for a specific size
This example checks if an image exists for a specific size before displaying it.
$image_info = image_get_intermediate_size(123, 'large'); if ($image_info) { $image_url = wp_get_attachment_url($image_info['file']); echo '<img src="' . $image_url . '" alt="My Image">'; } else { echo 'Image not available in the requested size.'; }
Get width and height of an image
This example retrieves the width and height of an image for a specific size.
$image_info = image_get_intermediate_size(123, 'medium'); $width = $image_info['width']; $height = $image_info['height']; echo 'Width: ' . $width . ', Height: ' . $height;