image_size with respect to aspect ratios

Right before image subsizes are created, there’s a filter called intermediate_image_sizes_advanced that lets you modify the list of sizes.

Here’s code that you could expand on — right now it just removes the thumbnail as a proof of concept:

function remove_image_sizes_before_generation( $new_sizes, $image_meta, $attachment_id ) {
    $width = $image_meta[0];
    $height = $image_meta[1];

    // ... Calculate aspect ratio and remove the image sizes that aren't relevant
    unset( $new_sizes['thumbnail'] );

    return $new_sizes;
}

add_filter( 'intermediate_image_sizes_advanced', 'remove_image_sizes_before_generation', 10 , 3 );

The nice thing about doing it this way is that the image will then “remember” what sizes it doesn’t have, even if they are registered sizes — for example, it won’t show up in drop-downs where you insert the image.

enter image description here