wp_get_registered_image_subsizes()WP 5.3.0

Gets the data (width, height, crop) of the currently registered image sizes.

Use get_intermediate_image_sizes() when you need to get only the names of intermediate sizes, not all the data.

No Hooks.

Returns

Array[]. An associative array of arrays with information about the sub-sizes of images:

Array
	[thumbnail] => Array
			[width] => 150
			[height] => 150
			[crop] => 1

	[medium] => Array
			[width] => 300
			[height] => 300
			[crop] =>
	...

Usage

wp_get_registered_image_subsizes();

Examples

0

#1 Getting all thumbnail sizes

$sizes = wp_get_registered_image_subsizes();    

foreach ( $sizes as $key => $val ) {    

	echo "Name: {$key};" . PHP_EOL;  
	echo "Width: {$val['width']}" . PHP_EOL;     
	echo "Height: {$val['height']}" . PHP_EOL;   

	if ( is_array($val['crop']) ) {  
		echo "Crop: {$val['crop'][0]}x{$val['crop'][1]}." . PHP_EOL;
	} else {     
		echo "Crop: {$val['crop']}." . PHP_EOL;  
	}   

	echo PHP_EOL;    
}    

We will get:

Name: thumbnail;
Width: 150
Height: 150
Crop: 1.

Name: medium;
Width: 300
Height: 300
Crop: .

Name: medium_large;
Width: 768
Height: 0
Crop: .

Name: large;
Width: 1024
Height: 1024
Crop: .
0

#2 Demo: what the function outputs

$sizes = wp_get_registered_image_subsizes(); 

print_r( $sizes );

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

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

	[medium_large] => Array
		(
			[width] => 768
			[height] => 0
			[crop] => 
		)

	[large] => Array
		(
			[width] => 1024
			[height] => 1024
			[crop] => 
		)

	[1536x1536] => Array
		(
			[width] => 1536
			[height] => 1536
			[crop] => 
		)

	[2048x2048] => Array
		(
			[width] => 2048
			[height] => 2048
			[crop] => 
		)

	[post-thumbnail] => Array
		(
			[width] => 1568
			[height] => 9999
			[crop] => 
		)

)
*/

Changelog

Since 5.3.0 Introduced.

wp_get_registered_image_subsizes() code WP 6.9.1

function wp_get_registered_image_subsizes() {
	$additional_sizes = wp_get_additional_image_sizes();
	$all_sizes        = array();

	foreach ( get_intermediate_image_sizes() as $size_name ) {
		$size_data = array(
			'width'  => 0,
			'height' => 0,
			'crop'   => false,
		);

		if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
			// For sizes added by plugins and themes.
			$size_data['width'] = (int) $additional_sizes[ $size_name ]['width'];
		} else {
			// For default sizes set in options.
			$size_data['width'] = (int) get_option( "{$size_name}_size_w" );
		}

		if ( isset( $additional_sizes[ $size_name ]['height'] ) ) {
			$size_data['height'] = (int) $additional_sizes[ $size_name ]['height'];
		} else {
			$size_data['height'] = (int) get_option( "{$size_name}_size_h" );
		}

		if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) {
			// This size isn't set.
			continue;
		}

		if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) {
			$size_data['crop'] = $additional_sizes[ $size_name ]['crop'];
		} else {
			$size_data['crop'] = get_option( "{$size_name}_crop" );
		}

		if ( ! is_array( $size_data['crop'] ) || empty( $size_data['crop'] ) ) {
			$size_data['crop'] = (bool) $size_data['crop'];
		}

		$all_sizes[ $size_name ] = $size_data;
	}

	return $all_sizes;
}