get_intermediate_image_sizes()
云策文档标注
概述
get_intermediate_image_sizes() 函数用于获取 WordPress 中所有可用的中间图像尺寸名称。它返回一个包含默认尺寸和自定义尺寸的数组,并可通过过滤器进行修改。
关键要点
- 函数返回一个字符串数组,包含图像尺寸名称,如 'thumbnail'、'medium'、'medium_large'、'large' 和自定义尺寸。
- 默认尺寸包括 'thumbnail'、'medium'、'medium_large' 和 'large',可通过 wp_get_additional_image_sizes() 获取自定义尺寸。
- 使用 apply_filters('intermediate_image_sizes', $default_sizes) 钩子可以过滤返回的尺寸列表。
- 自 WordPress 3.0.0 版本引入此函数。
代码示例
var_dump( get_intermediate_image_sizes() );
array(4) {
[0]=>
string(9) "thumbnail"
[1]=>
string(6) "medium"
[2]=>
string(12) "medium_large"
[3]=>
string(5) "large"
[4]=>
string(10) "custom-size"
}注意事项
- 自 WordPress 4.4 版本起,新增了 'medium_large' 尺寸,在相关代码中应包含此尺寸以避免错误。
- 自 WordPress 5.3.0 版本起,推荐使用 wp_get_registered_image_subsizes() 函数来获取包含宽度和高度的注册图像尺寸信息。
原文内容
Gets the available intermediate image size names.
Source
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 );
}
Hooks
- apply_filters( ‘intermediate_image_sizes’, string[] $default_sizes )
-
Filters the list of intermediate image sizes.
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 5 content
Codex
List available image sizes with width and height following
/** * Get information about available image sizes */ function get_image_sizes( $size = '' ) { $wp_additional_image_sizes = wp_get_additional_image_sizes(); $sizes = array(); $get_intermediate_image_sizes = get_intermediate_image_sizes(); // Create the full array with sizes and crop info foreach( $get_intermediate_image_sizes as $_size ) { if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) { $sizes[ $_size ]['width'] = get_option( $_size . '_size_w' ); $sizes[ $_size ]['height'] = get_option( $_size . '_size_h' ); $sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' ); } elseif ( isset( $wp_additional_image_sizes[ $_size ] ) ) { $sizes[ $_size ] = array( 'width' => $wp_additional_image_sizes[ $_size ]['width'], 'height' => $wp_additional_image_sizes[ $_size ]['height'], 'crop' => $wp_additional_image_sizes[ $_size ]['crop'] ); } } // Get only 1 size if found if ( $size ) { if( isset( $sizes[ $size ] ) ) { return $sizes[ $size ]; } else { return false; } } return $sizes; }Some examples of use of this function:
var_dump( get_image_sizes() ); /* array(4) { ["thumbnail"]=> array(3) { ["width"]=> string(3) "150" ["height"]=> string(3) "150" ["crop"]=> bool(true) } ["medium"]=> array(3) { ["width"]=> string(3) "300" ["height"]=> string(3) "300" ["crop"]=> bool(false) } ["large"]=> array(3) { ["width"]=> string(4) "1024" ["height"]=> string(4) "1024" ["crop"]=> bool(false) } ["juliobox-size"]=> array(3) { ["width"]=> int(211) ["height"]=> int(279) ["crop"]=> bool(false) } } */ var_dump( get_image_sizes( 'large' ) ); /* array(3) { ["width"]=> int(1024) ["height"]=> int(1024) ["crop"]=> bool(false) } */ var_dump( get_image_sizes( 'foo-bar' ) ); /* bool(false) */Skip to note 6 content
markcallen
In
the get_image_sizes()example above – note that since WordPress 4.4 there is a new size – ‘medium_large’.Include that in the array on line 13 to prevent errors.
Skip to note 7 content
Muhammad Ayoub
Get all possible registered image size with their names
$wp_additional_image_sizes = wp_get_additional_image_sizes(); $sizes = array(); $get_intermediate_image_sizes = get_intermediate_image_sizes(); // Create the full array with sizes and crop info foreach ( $get_intermediate_image_sizes as $_size ) { if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) { $sizes[ $_size ]['width'] = get_option( $_size . '_size_w' ); $sizes[ $_size ]['height'] = get_option( $_size . '_size_h' ); $sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' ); } elseif ( isset( $wp_additional_image_sizes[ $_size ] ) ) { $sizes[ $_size ] = array( 'width' => $wp_additional_image_sizes[ $_size ]['width'], 'height' => $wp_additional_image_sizes[ $_size ]['height'], 'crop' => $wp_additional_image_sizes[ $_size ]['crop'] ); } } foreach ( $sizes as $key => $image_size ) { echo '<li>' . $key . ' ' . $image_size['width'] . ' x ' . $image_size['height'] . ' ' . $image_size['crop'] . '</li>'; }Skip to note 8 content
Ramon Ahnert
Since version 5.3.0, you can use wp_get_registered_image_subsizes() function to list registered image sizes with width and height.