get_intermediate_image_sizes()WP 3.0.0

Gets the available intermediate image sizes.

1 time — 0.000018 sec (very fast) | 50000 times — 0.14 sec (very fast) | PHP 7.0.5, WP 4.5.2
Hooks from the function

Return

String[]. An array of image size names.

Usage

get_intermediate_image_sizes();

Examples

0

#1 Get the names of all sizes

$sizes = get_intermediate_image_sizes();
print_r( $sizes );

/* We get:
Array
(
	[0] => thumbnail
	[1] => medium
	[2] => medium_large
	[3] => large
	[4] => shop_thumbnail
	[5] => shop_catalog
	[6] => shop_single
	[7] => post-thumbnail
)
*/

But in this case we don't see the data of each of the sizes, what width and height is set for it, whether the image is cropped or not...

Also, if for example in the media files for the size "Large" set zeros (0) for height and width, then technically this size is disabled - no thumbnail is created for it anymore, but this function will show it.

To solve all these problems, use wp_get_registered_image_subsizes(). see the following example.

0

#2 Get the data of all registered image sizes

From WP 5.3 there is a core function that provides exactly this functionality: wp_get_registered_image_subsizes().

WordPress doesn't have such a built-in function, but it's not hard to create it using this function:

/**
 * Gets information about all registered image sizes.
 * 
 * @global $_wp_additional_image_sizes
 * @uses   get_intermediate_image_sizes()
 * 
 * @param bool $unset_disabled Remove dimensions with 0 height and width from the list?
 * @return array Data of all sizes.
 */
function get_image_sizes( $unset_disabled = true ) {

	$sizes = array();
	$wais = wp_get_additional_image_sizes();

	foreach ( get_intermediate_image_sizes() as $_size ) {

		if ( in_array( $_size, [ 'thumbnail', 'medium', 'medium_large', 'large' ] ) ) {

			$sizes[ $_size ] = array(
				'width'  => get_option( "{$_size}_size_w" ),
				'height' => get_option( "{$_size}_size_h" ),
				'crop'   => (bool) get_option( "{$_size}_crop" ),
			);
		}
		elseif ( isset( $wais[$_size] ) ) {

			$sizes[ $_size ] = array(
				'width'  => $wais[ $_size ]['width'],
				'height' => $wais[ $_size ]['height'],
				'crop'   => $wais[ $_size ]['crop'],
			);
		}

		// size registered, but has 0 width and height
		if( 
			$unset_disabled && ( $sizes[ $_size ]['width'] == 0 ) 
			&& 
			( $sizes[ $_size ]['height'] == 0 ) 
		){
			unset( $sizes[ $_size ] );
		}

	}

	return $sizes;
}

By calling such a function we get:

print_r( get_image_sizes() );

/*
Array
(
	[thumbnail] => Array
		(
			[width] => 80
			[height] => 80
			[crop] => 1
		)

	[medium] => Array
		(
			[width] => 120
			[height] => 120
			[crop] => 
		)

)
*/
0

#3 Get the data of a separate size

Now, with a function at hand that gets all the image sizes and their data, you can easily extend the functionality by writing functions to get individual data of a specified size.

/**
 * Gets data of a certain image size.
 *
 * @uses   get_image_sizes()
 * @param string $size Size name
 * @return bool|array $size Image size data or false if there is no such size.
 */
function get_image_size( $size ) {
	$sizes = get_image_sizes(0);

	return $sizes[ $size ] ?? false;
}

/**
 * Gets the width of a specific image size.
 *
 * @uses   get_image_size()
 * @param string $size Size name
 * @return bool|int Width or false if no size.
 */
function get_image_size_width( $size ) {

	if ( ! $size = get_image_size( $size ) )
		return false;

	return $size['width'] ?? false;
}

/**
 * Gets the height of a specific image size.
 *
 * @uses   get_image_size()
 * @param string $size Size name
 * @return bool|int Height or false if there is no size.
 */
function get_image_size_height( $size ) {

	if ( ! $size = get_image_size( $size ) )
		return false;

	return $size['height'] ?? false;
}

And now a demonstration of the functions created:

$medium_size = get_image_size('medium');

/* will return:
Array
(
	[width] => 120
	[height] => 120
	[crop] => 
)
*/

$medium_size_width = get_image_size_width('medium'); //> 120

$medium_size_height = get_image_size_height('medium'); //> 120

Changelog

Since 3.0.0 Introduced.

get_intermediate_image_sizes() code WP 6.5.3

function get_intermediate_image_sizes() {
	$default_sizes    = array( 'thumbnail', 'medium', 'medium_large', 'large' );
	$additional_sizes = wp_get_additional_image_sizes();

	if ( ! empty( $additional_sizes ) ) {
		$default_sizes = array_merge( $default_sizes, array_keys( $additional_sizes ) );
	}

	/**
	 * Filters the list of intermediate image sizes.
	 *
	 * @since 2.5.0
	 *
	 * @param string[] $default_sizes An array of intermediate image size names. Defaults
	 *                                are 'thumbnail', 'medium', 'medium_large', 'large'.
	 */
	return apply_filters( 'intermediate_image_sizes', $default_sizes );
}