image_get_intermediate_size()
Gets an array of image-attachment data of the specified size: thumbnail, medium etc., if it exists.
If you use an array for $size, it is recommended to register the required size using add_image_size() - this is sometimes more efficient than finding the nearest larger size and then scaling it down to the required size in the browser.
As a result, the image URL will be returned only if the $size parameter is specified as a string.
Hooks from the function
Returns
Array|false. false if the size does not exist and an array of image data if the size exists:
If a string is passed in $size and an exact matching size is found: Array ( [file] => setting2-120x14.png [width] => 120 [height] => 14 [mime-type] => image/png [path] => 2014/07/setting2-120x14.png [url] => http://wp-kama.ru/wp-content/uploads/2014/07/setting2-120x14.png ) When an exact size is not found, but a nearest larger image is found: Array ( [file] => setting2-120x14.png [width] => 90 [height] => 10 )
Usage
$image = image_get_intermediate_size( $post_id, $size );
- $post_id(int) (required)
- ID of the image attachment
- $size(string/array)
Image size.
Here you specify a registered image size in WordPress; out of the box the following sizes are available:
thumb thumbnail medium large post-thumbnail
Custom images are registered using add_image_size().
You can pass an array to the parameter, for example
[200, 300]- width and height in pixels. If such a size matches one of the already generated image sizes, the corresponding thumbnail will be used. If there is no exact match, the nearest suitable larger size will be returned. If there is no suitable larger size, the function will returnfalse.Default: 'thumbnail'
Examples
#1 Usage Example
Let's get the data of the attachment image 4831, medium size:
$intermediate = image_get_intermediate_size( 4831, 'medium' ); print_r( $intermediate ); /* He will: Array ( [file] => setting2-120x14.png [width] => 120 [height] => 14 [mime-type] => image/png [path] => 2014/07/setting2-120x14.png [url] => http://wp-kama.ru/wp-content/uploads/2014/07/setting2-120x14.png ) */
#2 Specify an array in the $size parameter
Let's get the image data of attachment 4831, the specified size (i.e., the size may not be registered):
$intermediate = image_get_intermediate_size( 4831, [90,90] ); /* will return: Array ( [file] => setting2-120x14.png [width] => 90 [height] => 10 ) */
This code will pick it up the most suitable size from existing ones, but not generate a new one.
#3 Display a picture of the right size, if there is one
In this example, we output a picture if there is one and "no picture" if there isn't. Unlike image_downsize() which returns the URL in any case:
function get_image_link_if_exists( $size ){
global $post;
// get the id of the post thumbnail
$thumbnail_id = get_post_thumbnail_id( $post->ID );
// output the picture if there is one
$image = image_get_intermediate_size( $thumbnail_id, $size );
if( $image ){
echo '<img src="'. $image['url'] .'" alt="" width="'. $image['width'] .'" height="'. $image['height'] .'" />';
}
else {
echo 'No picture';
}
}
get_image_link_if_exists('my_size');
Changelog
| Since 2.5.0 | Introduced. |