让我们有一些这样的缩略图大小:
add_image_size(\'home-slide-medium\', 1000, 504, true);
add_image_size(\'home-slide-sm\', 500, 252, true);
add_image_size(\'video-poster\', 780, 512);
让我们上传一幅肖像图片,大小如下
1000x3000px
如何避免创建横向缩略图(如1000x504
或500x252
) 这张肖像画?
我尝试过这样的方法,但没有成功:
add_filter( \'image_resize_dimensions\', \'custom_image_resize_dimensions\', 10, 6 );
function custom_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ){
// ie parameters: null, 1000, 3000, 1000, 500, true
// ie payload: array(0, 0, 0, 1248, 1000, 504, 1000, 504))
// if $crop is true...
if($crop ) {
// ...and if src img is portrait, skip it unless is same aspect ratio
if($dest_w > $dest_h) {
return false;
}
// else continue
else {
return $payload;
}
}
else {
return $payload;
}
}
最合适的回答,由SO网友:ChristopherJones 整理而成
你朝着正确的方向前进。还有另一个过滤器可以处理intermediate_image_sizes_advanced
. 您可以在其中添加或删除自定义大小的缩略图。与其在肖像上传上不创建横向自定义尺寸,不如尝试在横向上传上只创建横向自定义尺寸。
add_filter( \'intermediate_image_sizes_advanced\', function( $sizes, $metadata ) {
// Let\'s make sure we have the meta data we need before proceeding
if(!empty($metadata[\'width\']) && !empty($metadata[\'height\'])){
// Now let\'s make sure we have a Landscape being uploaded
if($metadata[\'width\'] > $metadata[\'height\']){
$sizes[\'home-slide-medium\'] = array(
\'width\' => 1000,
\'height\' => 504,
\'crop\' => true
);
$sizes[\'home-slide-sm\'] = array(
\'width\' => 500,
\'height\' => 252,
\'crop\' => tru
e);
$sizes[\'video-poster\'] = array(
\'width\' => 780,
\'height\' => 512,
\'crop\' =>false
);
}
}
return $sizes;
}, 10, 2);
我在我这边测试了几次,但请告诉我你的想法!!