How can i alter the amount of different images sizes being stored?

Additional image sizes for uploaded images are created by wp_generate_attachment_metadata() after the file has been uploaded. An array of available sizes is populated using get_intermediate_image_sizes() (thumbnail, medium, large + custom sizes) which is later used to create the images.

Both of the above functions have filters for overriding the sizes available to WordPress during this task, intermediate_image_sizes_advanced and intermediate_image_sizes respectively. By hooking on the any of those filters you should be able to remove the sizes you don’t need. I would suggest using intermediate_image_sizes as it is executed earlier and you’ll only have to filter an array of strings (image size identifiers).

add_filter('intermediate_image_sizes', 'wpse_101197_image_sizes');
function wpse_101197_image_sizes($sizes) {

    if (($key = array_search('size_1', $sizes)) !== false) {
        unset($sizes[$key]);
    }

    return $sizes;
}

Not however that it is key to only apply this filter when you are handling uploads from within your plugin, naturally, as you will otherwise end up removing images sizes from WordPress globally. Thus, don’t forget to remove the filter once you’re done with the upload. Also keep in mind that WordPress kind of expects each image to be available in every image size – if a specific size is not found for an image, you will end up with the largest one.

Leave a Comment