Add image size if page template

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(
            array(
                $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!

Leave a Comment