Wordpress has three default thumbnail sizes: thumbnail (150x150), Graph (300x300), and large image (1024x1024). The latter two can only be scaled in proportion to the original image, for example, if the aspect ratio of the source image is, the size of the generated image is 300x150 instead of 300x300, which often cannot meet our needs. Therefore, we need to increase the size of the generated thumbnail, mainly used add_imag
Wordpress has three default thumbnail sizes: thumbnail (150x150), Graph (300x300), and large image (1024x1024). The latter two can only be scaled in proportion to the original image, for example, if the aspect ratio of the source image is, the size of the generated image is 300x150 instead of 300x300, which often cannot meet our needs. Therefore, we need to increase the size of the generated thumbnail, the add_image_size function is mainly used.
This function can be used to register a new image size. This means that when you upload a new image, WordPress creates a copy of a special image (featured images) of a specific size.
Note:To enable featured images or thumbnail, the current topic must be included in the functions. php file?Add_theme_support ('post-thumbnails '); for more information, see
? Post Thumbnails.
Syntax:
Parameters:
-
- $ Name
-
(
String)(
Required) Name of the new image size.
-
Default :?
None
- $ Width
-
(
Int) (Optional) width of the thumbnail (in pixels ).
-
Default: 0
- $ Height
-
(
Int) (Optional) Thumbnail height (in pixels )..
-
Default: 0
- $ Crop
-
(
Boolean)(
Optional) Whether to crop the image. False-proportional cropping mode; True-hard cropping mode (not proportional ).
-
Default: false
-
Built-in Image size name
thumb
,?thumbnail
,?medium
,?large
,?post-thumbnail
Name"thumb
"&"thumbnail
"Is an alias, equivalent.
For more explanations, see? Image_downsize ()? This article.
Finally, you can set the option value as needed:
update_option('thumbnail_size_w', 160);update_option('thumbnail_size_h', 160);update_option('thumbnail_crop', 1);
-
Example
Topic 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)}
-
Cropping mode
Scale the image size proportionally (do not delete the image ):
Add_image_size ('homepage-thumb', 220,180); // 220 pixels wide, 180 pixels high, proportional cropping mode
Set the image size by cropping (from both sides, top or bottom ):
Add_image_size ('homepage-thumb', 220,180, true); // 220 pixels wide, 180 pixels high, hard cropping mode
How to use the new image size
Use the following code in the topic template file.
If a blog already has a large number of images and you need to generate new images based on the new size, we recommend that you use the wordpress plug-in.AJAX Thumbnail Rebuild, For http://wordpress.org/plugins/ajax-thumbnail-rebuild/
-