add_filter(
'image_resize_dimensions',
function ( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
if ( ! $crop || $dest_w <= 0 || $dest_h <= 0 ) {
return $payload;
}
$dest_ratio = $dest_w / $dest_h;
$orig_ratio = $orig_w / $orig_h;
if ( $orig_ratio > $dest_ratio ) {
$src_h = $orig_h;
$src_w = (int) round( $dest_ratio * $src_h );
$src_x = (int) floor( ( $orig_w - $src_w ) / 2 );
$src_y = 0;
} else {
$src_w = $orig_w;
$src_h = (int) round( $src_w / $dest_ratio );
$src_x = (int) floor( ( $orig_w - $src_w ) / 2 );
$src_y = 0;
}
$dst_x = 0;
$dst_y = 0;
return array( $dst_x, $dst_y, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h );
},
10,
6
);