add_image_size()
云策文档标注
概述
add_image_size() 函数用于在 WordPress 中注册一个新的图像尺寸,允许开发者自定义图片裁剪和缩放行为。它通过参数控制宽度、高度和裁剪模式,并支持在主题或插件中灵活应用。
关键要点
- 函数参数:$name(必需,图像尺寸标识符),$width(可选,宽度像素,默认0),$height(可选,高度像素,默认0),$crop(可选,裁剪行为,默认false表示缩放,true表示居中裁剪,数组可指定裁剪位置)。
- 保留图像尺寸名称:WordPress 内置保留名称如 'thumb'、'thumbnail'、'medium'、'medium_large'、'large'、'post-thumbnail',其中 'thumb' 和 'thumbnail' 是别名。
- 裁剪模式示例:使用 false 进行比例缩放,true 进行硬裁剪,数组指定裁剪位置(如 array('left', 'top'))。
- 使用自定义图像尺寸:可通过 the_post_thumbnail() 在特色图片中应用,使用 image_size_names_choose 钩子在管理后台显示,或通过 wp_get_attachment_image() 在模板中输出。
- 注意事项:注册新尺寸后需重新生成缩略图(如使用 Regenerate Thumbnails 插件或 WP-CLI 命令);add_theme_support('post-thumbnails') 必须在 add_image_size() 之前调用;避免使用保留名称;尺寸参数应明确设置,避免默认值0导致问题。
代码示例
// 在主题的 functions.php 中注册自定义图像尺寸
add_action( 'after_setup_theme', 'wpdocs_theme_setup' );
function wpdocs_theme_setup() {
add_image_size( 'category-thumb', 300 ); // 宽度300像素,高度无限制
add_image_size( 'homepage-thumb', 220, 180, true ); // 硬裁剪模式
add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) ); // 指定裁剪位置
}注意事项
- 如果上传图像的尺寸与注册尺寸相同且 crop 为 true,可能不会生成新图像;删除已注册尺寸后,之前上传的图像文件可能不会自动删除。
- 使用 update_option() 可以修改内置尺寸(如 'thumbnail_size_w'),但需谨慎调用以避免频繁数据库更新。
- WordPress 5.3 新增了 '1536x1536' 和 '2048x2048' 图像尺寸。
原文内容
Registers a new image size.
Parameters
$namestringrequired-
Image size identifier.
$widthintoptional-
Image width in pixels. Default 0.
$heightintoptional-
Image height in pixels. Default 0.
$cropbool|arrayoptional-
Image cropping behavior. If false, the image will be scaled (default).
If true, image will be cropped to the specified dimensions using center positions.
If an array, the image will be cropped using the array to specify the crop location:0stringThe x crop position. Accepts'left','center', or'right'.1stringThe y crop position. Accepts'top','center', or'bottom'.
Default:
false
Source
function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
global $_wp_additional_image_sizes;
$_wp_additional_image_sizes[ $name ] = array(
'width' => absint( $width ),
'height' => absint( $height ),
'crop' => $crop,
);
}
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |
Skip to note 14 content
Codex
In a theme’s
functions.phpfile. Always use the “after_setup_theme” action hook.add_action( 'after_setup_theme', 'wpdocs_theme_setup' ); function wpdocs_theme_setup() { add_image_size( 'category-thumb', 300 ); // 300 pixels wide (and unlimited height) add_image_size( 'homepage-thumb', 220, 180, true ); // (cropped) }Skip to note 15 content
Ian Dunn
In addition to the Regenerate Thumbnails plugins, there’s also a WP-CLI command:
wp media regenerate. See https://wp-cli.org/commands/media/regenerate/ orwp help regenerate mediafor details.Skip to note 16 content
Jensdiep
After you have add new sizes WordPress has to regenerate the images.
Best plugin to do that: https://wordpress.org/plugins/regenerate-thumbnails/
Skip to note 17 content
rtpHarry
The section “Reserved Image Size Names” hints at it but doesn’t explicitly point out that you can also customise the medium and large sizes via code by using these option names:
medium_size_h: The medium size height.medium_size_w: The medium size width.large_size_h: The large size height.large_size_w: The large size width.So for example, to set the large height to 700px you would use this code:
update_option( 'large_size_h', 700 );Skip to note 18 content
pixeline
Note that along
thumbnail,medium,large, there is also a size built into wordpress that you can use :fullto get the image at its original dimensions.Skip to note 19 content
Anthony Hortin
Related
remove_image_size()
has_post_thumbnail()
the_post_thumbnail()
get_post_thumbnail_id()
get_the_post_thumbnail()
set_post_thumbnail_size()
Skip to note 20 content
Ivijan-Stefan Stipic
NOTE THIS:
If
add_image_size()still doesn’t work even after regenerating thumbnails, make sure thatadd_theme_support( 'post-thumbnails' )is loaded beforeadd_image_size()Example:
function wpse_setup_theme() { add_theme_support( 'post-thumbnails' ); add_image_size( 'team-thumb', 60, 60, true ); } add_action( 'after_setup_theme', 'wpse_setup_theme' );Skip to note 21 content
hongpong
If you upload an image whose dimensions match the
add_image_size()when crop is set totrue, in the$metaobject accessed by thewp_generate_attachment_metadatafilter, that matching image size will not be available. Also, image sizes that have larger dimensions than an uploaded photo will not be available either.(Thus if you are using a technique to create something like a monochrome derivative image, you won’t be able to get it to work if the uploaded image is exactly the same size as the image size you’re using for your black and white version).
Skip to note 22 content
Marc Bernard
Default settings for image size (see /wp-admin/includes/schema.php):
<br />
'thumbnail_size_w' => 150,<br />
'thumbnail_size_h' => 150,<br />
'medium_size_w' => 300,<br />
'medium_size_h' => 300,<br />
'medium_large_size_w' => 768,<br />
'medium_large_size_h' => 0,<br />
'large_size_w' => 1024,<br />
'large_size_h' => 1024,<br />
You can change these option values in WordPress > Admin > Settings > Media (/wp-admin/options-media.php).
Use
get_intermediate_image_sizes()to get the current settings for predefined as well as custom image sizes.Skip to note 23 content
slingshotdesign
Note that if you would like your custom image sizes to be available in the admin, for example when choosing an image size for the Media & Text block, use the filter: image_size_names_choose
Example:
add_filter( 'image_size_names_choose', 'my_custom_sizes' ); function my_custom_sizes( $sizes ) { return array_merge( $sizes, array( 'square_medium' => __( 'Square Medium' ), 'square_large' => __( 'Square Large' ), ) ); }Skip to note 24 content
Lovro Hrust
Since WordPress 5.3, two additional image sizes are added:
1536×1536
2048×2048
These are also their names.
For additional information, check wp-includes/media.php
Skip to note 25 content
dejudicibus
Let us suppose that a master theme set in functions.php a size, let us say ‘a_size’. and that the functions.php in a child theme uses again this function to add the same size but with different value. It is not clear from this page if the old size is changed or the call is ignored or forbidden.
Skip to note 26 content
Diana Thompson
The cropping behavior cited as 2) in Description, https://developer.wordpress.org/reference/functions/add_image_size/#description, is *not* currently supported. Please see the ticket at https://core.trac.wordpress.org/ticket/40370 for more information.