I'm building a members website with WordPress Multisite. Is it possible to restrict how many images are being generated depending on the selected template?

I have tried the following lines of code to generate certain images on the gallery template:

// Generate on all uploads
set_post_thumbnail_size( 1440, 350, true ); 
add_image_size( 'standard_box', 450, 215, true );
add_image_size( 'default_image', 691, 9999 );

// Generate on gallery template only
if ( is_page_template('page-gallery.php') ) {
add_image_size( 'gallery', 900, 9999 );
add_image_size( 'gallery_thumb', 450, 450, true );

This hasn't worked. I've done some research and can't seem to find anything on the subject. If you could point me in the right direction, I'd really appreciate it.

  • 1
    There is also Otto's Dynamic Image Resizer which lets you define as many image sizes as you want, but only generates an image at a particular size when it is needed. SO for your example the gallery thumbnails would only be generated for the images that appear on the page-gallery.php template. – helgatheviking Mar 30 '14 at 10:35
  • 2
    Just want to mention other two great options: @kaiser's Dynamic Image Resize and the Photon service in JetPack. – birgire Mar 30 '14 at 14:22

This has always been a bugbear for me - the lack of on-demand image sizing, and the subsequent number of files you can end up with if you have lots of sizes!

I can see the logic behind your efforts - the trouble is, add_image_size only truly comes into play at point-of-upload. As such, is_page_template(..) will always be false.

A quick google dug up Aqua Resizer, a script designed to tackle this issue. Rather than use add_image_size, you use aq_resize directly in your theme, and if a size for the image doesn't exist, it's created and cached on-the-fly.

In fact I've used a similar, albeit different, technique on several sites with many image sizes. You still save the overhead of WordPress generating every size for every image uploaded - they're generated on-the-fly (& cached) as and when they're requested. Where it differs, is that you can simply use all of WP's standard image functions and template tags as you would normally!

Also, as @Waqas mentioned, using Aqua Resizer will leave orphaned files when you delete an image from your media library. With my technique, all files will be deleted, since they're saved to the database & recognised by WordPress.

 * Resize internally-registered image sizes on-demand.
 * @link    http://wordpress.stackexchange.com/q/139624/1685
 * @param   mixed   $null
 * @param   int     $id
 * @param   mixed   $size
 * @return  mixed
function wpse_139624_image_downsize( $null, $id, $size ) {
    static $sizes = array(
        'post-thumbnail' => array(
            'height' => 350,
            'width'  => 1440,
            'crop'   => true,

        'standard_box' => array(
            'height' => 215,
            'width'  => 450,
            'crop'   => true,

        'default_image' => array(
            'height' => 9999,
            'width'  => 691,
            'crop'   => false,

        'gallery' => array(
            'height' => 900,
            'width'  => 9999,
            'crop'   => false,

        'gallery_thumb' => array(
            'height' => 450,
            'width'  => 450,
            'crop'   => true,

    if ( ! is_string( $size ) || ! isset( $sizes[ $size ] ) )
        return $null;
    if ( ! is_array( $data = wp_get_attachment_metadata( $id ) ) )
        return $null;
    if ( ! empty( $data['sizes'][ $size ] ) )
        return $null;
    if ( $data['height'] <= $sizes[ $size ]['height'] && $data['width'] <= $sizes[ $size ]['width'] )
        return $null;
    if ( ! $file = get_attached_file( $id ) )
        return $null;

    $editor = wp_get_image_editor( $file );

    if ( ! is_wp_error( $editor ) ) {
        $data['sizes'] += $editor->multi_resize(
                $size => $sizes[ $size ],

        wp_update_attachment_metadata( $id, $data );

    return $null;

add_filter( 'image_downsize', 'wpse_139624_image_downsize', 10, 3 );

And in practice:

wp_get_attachment_image( $id, 'gallery' ); // Resized if not already
wp_get_attachment_image_src( $id, 'standard_box' ); // Resized if not already
the_post_thumbnail(); // You get the idea!
// And so forth!

I'm intending to turn this into a plugin that will automatically convert all add_image_size calls into on-demand resizing, so watch this space!

  • Thank you for taking the time to leave such a great answer. I'll certainly give this a go. It's important I get this right before opening my server up to multiple users. I'll give you a shout if I have any further questions. – Sam Mar 29 '14 at 17:56
  • 2
    I gave this a go with the_post_thumbnail(); and it works perfectly. One problem: When I delete the image, it leaves behind the one which has been re-sized by your script. Any ideas? – Sam Apr 10 '14 at 17:55

- This isn't actually an answer.
- It is intended to help you with your additional research on the topic.
- Furthermore it is reflecting a - at least felt - lately more frequent occurrence of similar questions regarding similar problems.

Additional information regarding this topic on Wordpress Development:

Note: The list isn't ordered and comprehensive by any means.


If you want to create thumbs on the fly you can use Aqua image resizer, But there is a drawback of this mini script. The created thumbs will not delete upon deleting image from library. But its not a big deal. If required you can do so via SHH commands

  • 1
    Good point about files not being deleted. My solution won't suffer from this! (see updated answer). – TheDeadMedic Mar 29 '14 at 17:06

It is not a direct answer for your problem. But I will help you to make image according to your need.

When you use add_image_size it does not resize the existing images. it only works for the new images that will be uploaded after adding your add_image_size function.

So your code will not generate new images for is_page_template function.

But you can use a simple php class to solve your problem.... it is a famous php class that is being used for a lot of premium theme out there for WordPress. It is called Aqua-Resizer.

You can find more details here https://github.com/syamilmj/Aqua-Resizer Wiki: https://github.com/syamilmj/Aqua-Resizer/wiki

Problem that may arise:

This function works this way....

<?php aq_resize( $url, $width, $height, $crop, $single ) ?>

So when it can not crop ( for small image that your height or width defined ) it does not show anything. You can overcome this situation by checking whether your are getting a null value after passing the url within this function very easily like mine here....

$thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'full' ); if($thumb){ $url = aq_resize( $thumb, 400, 200, true, true ); if(!$url){ $url = WPM_DIR.'/assets/images/demo/wrong_image_size.jpg'; } }else{ $url = WPM_DIR.'/assets/images/demo/400x200_no_feature_image.jpg'; } 

In this way you can ensure that the specific image will be generated for specific page template and this way your website will be much more cleaner.

P.S: this php class usages WordPress core cropping system so there is not security issues.

Your Answer

