你是对的,你预测你将任由上传者摆布,他们在尺寸和长宽比方面可以很有创造力!
我的方法是在前端UI中非常详细地说明哪些图像是可以接受的-“请上传标准比例的图像,例如1200x900或800x600。请注意,您的图像将被裁剪为该比例,因此无法……”(等)
如果他们忽略了这一点,那么他们的图像仍然会出现在我的函数文件中,我在其中取消了标准WP图像大小的设置,并设置了自己的大小。它们中有很多是硬裁剪的,所以它是上传的,我恐怕要小心了(我在最后也留下了一些额外的功能):
//Remove all the defaults
function remove_default_image_sizes( $sizes ) {
//Default WordPress
unset( $sizes[ \'thumbnail\' ] );
unset( $sizes[ \'medium\' ] );
unset( $sizes[ \'medium_large\' ] );
unset( $sizes[ \'large\' ] );
//With WooCommerce you can remove these too
//unset( $sizes[ \'shop_thumbnail\' ]); // Shop thumbnail (180 x 180 hard cropped)
//unset( $sizes[ \'shop_catalog\' ]); // Shop catalog (300 x 300 hard cropped)
//unset( $sizes[ \'shop_single\' ]); // Shop single (600 x 600 hard cropped)
return $sizes;
}
add_filter( \'intermediate_image_sizes_advanced\',\'remove_default_image_sizes\' );
//Add our new sizes, xome hard cropped...
add_image_size( \'custom-large\', 1200, 900, false);
add_image_size( \'custom-medium\', 800, 600, false);
add_image_size( \'custom-thumb\', 260, 195, false);
add_image_size( \'custom-blog-cropped\', 1200, 500, true );
add_image_size( \'custom-avatar-cropped\', 110, 110, true );
//Bonus functions
// Disable WordPress responsive srcset images
add_filter( \'max_srcset_image_width\', create_function( \'\', \'return 1;\' ) );
//Remove the hardcoded width and height WP loves setting
function remove_img_attr( $html ) {
return preg_replace( \'/(width|height)="\\d+"\\s/\', "", $html );
}
add_filter( \'post_thumbnail_html\', \'remove_img_attr\' );