Description
The $size parameter can be an array with the width and height respectively. If the size matches the ‘sizes’ metadata array for width and height, then it will be used. If there is no direct match, then the nearest image size larger than the specified size will be used. If nothing is found, then the function will break out and return false.
The metadata ‘sizes’ is used for compatible sizes that can be used for the parameter $size value.
The url path will be given, when the $size parameter is a string.
If you are passing an array for the $size, you should consider using add_image_size() so that a cropped version is generated. It’s much more efficient than having to find the closest-sized image and then having the browser scale down the image.
Parameters
- $post_id
-
(int) (Required) Attachment ID.
- $size
-
(array|string) (Optional) Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order).
Default value: 'thumbnail'
Return
(array|false) Array of file relative path, width, and height on success. Additionally includes absolute path and URL if registered size is passed to $size parameter. False on failure.
-
'file'
(string) Image's path relative to uploads directory -
'width'
(int) Width of image -
'height'
(int) Height of image -
'path'
(string) Image's absolute filesystem path. -
'url'
(string) Image's URL.
Source
File: wp-includes/media.php
function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) {
$imagedata = wp_get_attachment_metadata( $post_id );
if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) {
return false;
}
$data = array();
if ( is_array( $size ) ) {
$candidates = array();
if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) {
$imagedata['height'] = $imagedata['sizes']['full']['height'];
$imagedata['width'] = $imagedata['sizes']['full']['width'];
}
foreach ( $imagedata['sizes'] as $_size => $data ) {
if ( intval( $data['width'] ) === intval( $size[0] ) && intval( $data['height'] ) === intval( $size[1] ) ) {
$candidates[ $data['width'] * $data['height'] ] = $data;
break;
}
if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
if ( 0 === $size[0] || 0 === $size[1] ) {
$same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $imagedata['width'], $imagedata['height'] );
} else {
$same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] );
}
if ( $same_ratio ) {
$candidates[ $data['width'] * $data['height'] ] = $data;
}
}
}
if ( ! empty( $candidates ) ) {
if ( 1 < count( $candidates ) ) {
ksort( $candidates );
}
$data = array_shift( $candidates );
} elseif ( ! empty( $imagedata['sizes']['thumbnail'] ) && $imagedata['sizes']['thumbnail']['width'] >= $size[0] && $imagedata['sizes']['thumbnail']['width'] >= $size[1] ) {
$data = $imagedata['sizes']['thumbnail'];
} else {
return false;
}
list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
} elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) {
$data = $imagedata['sizes'][ $size ];
}
if ( empty( $data ) ) {
return false;
}
if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) {
$file_url = wp_get_attachment_url( $post_id );
$data['path'] = path_join( dirname( $imagedata['file'] ), $data['file'] );
$data['url'] = path_join( dirname( $file_url ), $data['file'] );
}
return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size );
}
Changelog
Version | Description |
2.5.0 | Introduced. |