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
Take a look at Otto’s Dynamic Image Resizer plugin
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.
Unfortunately @Patrick’s answer breaks the srcset functions introduced in WP 4.4. Fortunately, we just need to add two additional functions!
First, we need to temporarily re-introduce all of the registered thumbnail sizes to the image metadata so they can be considered:
Then we need to run through the matches and generate any missing thumbnails:
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.
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.
You can use my (not Ottos) “Dynamic Image Resize” 1) plugin.
The plugin comes with a template tag and a shortcode as well.
1) Just found out about Ottos plugin. Naming collision wasn’t intended.
You can try this plugin:
https://wordpress.org/plugins/optimize-images-resizing
It resizes images based on your registered image size, but only when needed.
Also can cleanup existing image sizes so they can regenerate.
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.
Here yet another approach: Its hooks into the 404 HTTP error handling. That is, when thumbnail is not available, find the original image and create the thumbnail. Note that this does not really solve your problem, as it does not preven thumbnail generation during upload.
Note also that this plugin might be used by malicious users to create any number of thumbnails and thus to exhaust your disk space.
Note: This plugin can easily be installed using Pluginception.
On other places all made sizes will work but only on the media upload if you want the specific size to upload then below is the code for it. Here is the link for more description – https://developer.wordpress.org/reference/hooks/wp_handle_upload_prefilter/
You can try also Aqua Resizer – https://github.com/syamilmj/Aqua-Resizer/
It’s just a one file.
You can use it like this: