Generate Thumbnails only for Featured Images

I would like to know if there is a simple way (custom code or plugin) to create thumbnail sizes only for images that I intend to use as featured images (index.php, archive.php, etc.) , but not for the images used in the posts (single.php). My main goal is reduce server space usage by not creating thumbnails that my theme will never use.

My thumbnails would actually only have two sizes, 720px wide and 328px wide, and the 720px wide featured images (homepage only) would also have a 328px wide one (for archive.php and sidebar.php)

Currently, the only programmatic way I know is to generate thumbnails for every image upload, which is undesirable, since most of my uploads will be post images and I would have to deleted a lot of images from the server manually.

I would prefer custom code over plugins, but a plugin would be acceptable. I know there are some image resizing plugins out there, but they haven’t been update in a longtime (TimThumb, Dynamic Image Resizer).

I have also found a similar question here on WordPress SE, but the accepted answer doesn’t really solve my problem.


EDIT: I need to delete or prevent thumbnails for images inside the post, not for featured images, i.e.:

(1) Featured image: additional thumbnails auto-generated by WP are OK.

(2) Images used inside the posts: Upload the original image and do not generate any additional sizes. I will crop, resize and optimize the image before uploading it and one size will fit my needs.

5

This function will generate an image by temporarily registering an image size, generating the image (if necessary) and the removing the size so new images will not be created in that size.

function lazy_image_size($image_id, $width, $height, $crop) {
    // Temporarily create an image size
    $size_id = 'lazy_' . $width . 'x' .$height . '_' . ((string) $crop);
    add_image_size($size_id, $width, $height, $crop);

    // Get the attachment data
    $meta = wp_get_attachment_metadata($image_id);

    // If the size does not exist
    if(!isset($meta['sizes'][$size_id])) {
        require_once(ABSPATH . 'wp-admin/includes/image.php');

        $file = get_attached_file($image_id);
        $new_meta = wp_generate_attachment_metadata($image_id, $file);

        // Merge the sizes so we don't lose already generated sizes
        $new_meta['sizes'] = array_merge($meta['sizes'], $new_meta['sizes']);

        // Update the meta data
        wp_update_attachment_metadata($image_id, $new_meta);
    }

    // Fetch the sized image
    $sized = wp_get_attachment_image_src($image_id, $size_id);

    // Remove the image size so new images won't be created in this size automatically
    remove_image_size($size_id);
    return $sized;
}

It’s “lazy” because images are not generated until they are needed.

In your use-case, you would call that lazy_image_size with the post thumbnail ID to get an image of the desired size. An image would be generated on the first call. Subsequent calls would fetch the existing image.

Gist: https://gist.github.com/mtinsley/be503d90724be73cdda4

Leave a Comment