Add image size if page template

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:

Read More
// Generate on all uploads
add_theme_support('post-thumbnails');
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.

Related posts

Leave a Reply

4 comments

  1. 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!


  2. Disclaimer:
    – 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.

  3. 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….

    <?php 
    $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.