Function Reference/add image size
Description
Registers a new image size. This means that WordPress will create a copy of the post thumbnail with the specified dimensions when you upload a new thumbnail.
Usage
<?php add_image_size( $name, $width, $height, $crop ); ?>
Parameters
- $name
- (string) (required) The new image size name.
- Default: None
- $width
- (int) (optional) The post thumbnail width in pixels.
- Default: 0
- $height
- (int) (optional) The post thumbnail height in pixels.
- Default: 0
- $crop
- (boolean) (optional) Crop the image or not. False – Soft proportional crop mode ; True – Hard crop mode.
- Default: false
Reserved Image Size Names
thumb
, thumbnail
, medium
, large
, post-thumbnail
The names “thumb
” & “thumbnail
” are just alias, so exactly the same.
However, if needed, you can always set the options yourself:
update_option('thumbnail_size_w', 160);
update_option('thumbnail_size_h', 160);
update_option('thumbnail_crop', 1);
Examples
A theme’s functions.php file.
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions
}
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
add_image_size( 'homepage-thumb', 220, 180, true ); //(cropped)
}
Crop Mode
Set the image size by resizing the image proportionally (that is, without distorting it):
add_image_size( 'homepage-thumb', 220, 180 ); // 220 pixels wide by 180 pixels tall, soft proportional crop mode
Set the image size by cropping the image (either from the sides, or from the top and bottom):
add_image_size( 'homepage-thumb', 220, 180, true ); // 220 pixels wide by 180 pixels tall, hard crop mode
Using the New Image Sizes
Within a theme’s template files.
<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'category-thumb' ); } ?>