Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have 1000 images. How can i make wordpress to generate thumb only when needed. For example home slider will only use 10 images i dont want the other 1000 images have that thumbnail generated as its a waste of space and resources.

There is a way to fire add_image_size only when needed ?

Thanks

UPDATE As you mention is not really add_image_size what it needs to be fired. What it would be great is to fire the image resize when i use the_post_thumbnail('slider-thumb'); Maybe this slowdown the first view of the image but that view is usually generated by me when i actually review the post so i dont care.

So between my posts, slider, blog thumbnauls, portfolio thumbnails etc i got 1000 images and i want just 10 images to be resized for the slider i see a lot of wasted resources to generate the thumbnail size for the other 990 images.

Hope its clear now, sorry for my english

share|improve this question
1  
How is thumbnails generated from the extra 990 images more of a waste of space and resources than 990 unused images in the first place? Wouldn't it make more sense to only upload the images you're actively using? –  SickHippie May 25 '12 at 22:05
    
Even though more skilled programers are presenting valid arguments against your idea, I find it interesting. I saw some plugins and themes that upload images without generating thumbs (not sure of which right now). But my big doubt about your question is: when are you gonna need it?. What will be the filter? –  brasofilo May 25 '12 at 22:26
    
You got me wrong. I use the 990 images in posts, i just dont use the in the home slider. Some of them i need thumbs for the portfolio, some other for blog thumbs, etc –  chifliiiii May 25 '12 at 22:55

7 Answers 7

up vote 9 down vote accepted

Take a look at Otto's Dynamic Image Resizer plugin

This plugin changes the way WordPress creates images to make it generate the images only when they are actually used somewhere, on the fly. Images created thusly will be saved in the normal upload directories, for later fast sending by the webserver. The result is that space is saved (since images are only created when needed), and uploading images is much faster (since it's not generating the images on upload anymore).

share|improve this answer
    
Note that that plugin has a problem with adding images to old posts. Patches welcome. –  Otto May 26 '12 at 2:18
    
Thats exactly what i was looking for. I will give it a try. So it only works on new posts? –  chifliiiii May 26 '12 at 16:04

Put this in your theme functions file. It will stop Wordpress from creating anything but the 3 default sizes when uploading.

When an image is then requested in a particular size, which is not yet generated, it will be created only that once.

        add_filter('image_downsize', 'ml_media_downsize', 10, 3);
        function ml_media_downsize($out, $id, $size) {
            // If image size exists let WP serve it like normally
            $imagedata = wp_get_attachment_metadata($id);
            if (is_array($imagedata) && isset($imagedata['sizes'][$size]))
                return false;

            // Check that the requested size exists, or abort
            global $_wp_additional_image_sizes;
            if (!isset($_wp_additional_image_sizes[$size]))
                return false;

            // Make the new thumb
            if (!$resized = image_make_intermediate_size(
                get_attached_file($id),
                $_wp_additional_image_sizes[$size]['width'],
                $_wp_additional_image_sizes[$size]['height'],
                $_wp_additional_image_sizes[$size]['crop']
            ))
                return false;

            // Save image meta, or WP can't see that the thumb exists now
            $imagedata['sizes'][$size] = $resized;
            wp_update_attachment_metadata($id, $imagedata);

            // Return the array for displaying the resized image
            $att_url = wp_get_attachment_url($id);
            return array(dirname($att_url) . '/' . $resized['file'], $resized['width'], $resized['height'], true);
        }


        add_filter('intermediate_image_sizes_advanced', 'ml_media_prevent_resize_on_upload');
        function ml_media_prevent_resize_on_upload($sizes) {
            // Removing these defaults might cause problems, so we don't
            return array(
                'thumbnail' => $sizes['thumbnail'],
                'medium' => $sizes['medium'],
                'large' => $sizes['large']
            );
        }
share|improve this answer
    
This filer should be standard in WordPress. Why generate every size for every image? I am adding this code to my custom themes. Thanks –  Michaelkay May 23 at 17:19

Actually, add_image_size() doesn't generate the thumbnail, it just registers an image size as available to WordPress.

Typically, thumbnails are generated when the image is first uploaded. It's an automatic process so you don't have to worry about generating them later. Think of it this way - if it takes 1-2s to generate a thumbnail on a slow server, and you wait until it's requested, you force the requestee to wait an additional 1-2s per image to see the content. It's much easier to do this ahead of time - i.e. when the image is uploaded.

At the same time, if you absolutely must process the thumbnails at a different time, you might want to take a look at Viper's Regenerate Thumbnails plugin. It uses an on-demand action to regenerate all of your image thumbnails ... but you could use similar code to generate thumbnails only when needed.

share|improve this answer

There is a way to fire add_image_size only when needed ?

Not exactly. But you can filter the list of registered sizes right before the thumbnails are generated. The wp_generate_attachment_metadata() function (which calls the function that generates the thumbnails) has a filter called "intermediate_image_sizes_advanced", which lets you manipulate the array of sizes just before the files are generated. You could use this filter whenever you're adding an image of a certain "type", and then remove it immediately after.

I guess your biggest challenge would be to figure out how to differentiate between images that need the extra sizes, and those that don't.

share|improve this answer
    
i would have to add an option or checkbox when i upload media to choose wich thumbs i want to generate for example. Sounds good but i got not clue on how to do that –  chifliiiii May 25 '12 at 23:19

You can use my (not Ottos) "Dynamic Image Resize" 1) plugin.

“Dynamic Image Resize” is a WordPress (MU-)plugin that offers a shortcode and a template tag to resize images “on the flight” without the need of TimThumb, but with WP core functions.

The plugin comes with a template tag and a shortcode as well.

1) Just found out about Ottos plugin. Naming collision wasn't intended.

share|improve this answer

WP Performance Pack plugin offers "improved image handling", which is based on Ottos Dynamic Image Resizer, but includes many improvements, e.g.: First of all it's compatible with the latest WordPress Version (3.9.1), uses WP_Image_Editor, saving of thumbnails can be turned off (but they can be cached and CDN Support is on ist way), Regenerate Thumbails integration (to delete existing thumbnails) and some more.

share|improve this answer

You can try also Aqua Resizer - https://github.com/syamilmj/Aqua-Resizer/

It's just a one file.

You can use it like this:

$img_src = aq_resize( $img_src, $width = null, $height = null, $crop = null, $single = true, $upscale = false );

$img_src = aq_resize( $img_src, 150, 150); // resized
$img_src = aq_resize( $img_src, 150, 150, true); // cropped
$img_src = aq_resize( $img_src, 150, 150, null, null, true); // image with 120x120 for example will be upscaled up to 150x150
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.