[Resolved] Too many image sizes for blogs

Home Forums Support [Resolved] Too many image sizes for blogs

Home Forums Support Too many image sizes for blogs

  • This topic has 10 replies, 2 voices, and was last updated 6 years ago by Tom.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #534649
    Dean

    Hi Tom.

    I have just discovered the settings to change featured image sizes which can be found here (Customising > Layout > Blog).

    This is causing some issues for us as every time a user enters new image sizes, it appears that every featured image is then cropped to the new sizes and saved in the uploads directory. Even when they are just experimenting with which size to use. Therefore we end up with multiple copies of images with different sizes. Most of which are never used.

    This is bulking up our uploads directory and using up their upload quota more quickly. I understand you are doing this to please the page speed testers though ;-).

    I have two thoughts:

    1) Wouldn’t this be better done with a drop down box where you can just choose an existing size? This way no new image sizes would be added to the uploads directory.

    2) Also, it appears that if only one size is set (width or height) then an image will not be cropped and added to the uploads directory. Could you confirm this is correct and intended?

    Assuming 2 is correct, then as a temporary fix I am thinking to just hide the width box with css so the user can only adjust one and therefore not create any new images. Unless you have a better suggestion of how to prevent creating these new image sizes.

    Thank you πŸ™‚

    #535327
    Tom
    Lead Developer
    Lead Developer

    This is definitely an issue unfortunately. It just involves having to clean up your directory once you find the size you want.

    We tried to prevent too much build up by adding the “Apply” button.

    A dropdown would be nice, but I’m sure we would get topics in here every day from people who need the images to be a specific size.

    If you only select one size, the images should scale to that size, with the empty value becoming proportional.

    #535758
    Dean

    Hi Tom (@generate)

    Can I just confirm that if we only use one size (width or height) then by scale above you mean it won’t add another image to the uploads directory?

    In this case I will either hide just the width box, or both boxes with css so they cannot create new images.

    Thanks for your quick reply as always :-).

    #535832
    Tom
    Lead Developer
    Lead Developer

    The image resize will still create an image to the width or height you’ve specified.

    If you don’t want to create new images, leave those image height/width values empty.

    Then you can do something like this:

    add_action('after_setup_theme', 'tu_add_image_sizes');
    function tu_add_image_sizes() {
        add_image_size( 'large-thumbnail', 600, 600, true );
        add_filter( 'generate_page_header_default_size', 'tu_set_image_size' );
    }
    
    function tu_set_image_size() {
        return 'large-thumbnail';
    }
    #539331
    Dean

    Hi Tom.

    Thanks for providing the example above ;-). Looking at your example, it appears to me that it would adjust the featured image sizes for individual “Pages” (Home, contact, about etc). Is that correct? In our case I think individual pages and posts should remain as Auto Auto.

    However, the blog page listing all the recent posts, and any archive / category / tag pages listing their associated posts should have a smaller default image size.

    Could you tell me if the following would do the trick?

    add_filter( 'generate_blog_image_attributes','tu_variable_image_sizes' );
    function tu_variable_image_sizes( $atts ) {
        
            $atts[ 'width' ] = '';
            $atts[ 'height' ] = 300;
            $atts[ 'crop' ] = false;
        
        // Return our options
        return $atts;
    }

    I imagine the above would grab the wordpress “medium” image size. Make the image height 300 and the width would scale proportionately. Most importantly no new image size would be added to the uploads directory.

    Thank you :-).

    #539431
    Dean

    UPDATE

    So I setup a localhost version and tested this out. The above code I provided didn’t work without a conditional.

    The below now appears to work for me. Doesn’t affect individual posts and pages, but uses the medium WP image size on the blog page, tag, category etc pages. And of course no new images added to the uploads directories :-).

    add_filter( 'generate_blog_image_attributes','tu_variable_image_sizes' );
    function tu_variable_image_sizes( $atts ) {
        
        if ( is_archive() || is_home() ) {
            $atts[ 'width' ] = 300;
            $atts[ 'height' ] = 300;
            $atts[ 'crop' ] = false;
        }
        
        return $atts;
    }
    #540492
    Tom
    Lead Developer
    Lead Developer

    Awesome! Thanks for sharing the code πŸ™‚

    #540545
    Dean

    I couldn’t see a way to just set the medium WP image size. So I specified the width as 300 and height as 300, and without cropping, it seems to just use the medium size.

    Unless you can recommend a better way to set the medium image, then I guess we are all done here to I will mark this as resolved.

    Thanks again Tom.

    #540548
    Tom
    Lead Developer
    Lead Developer

    Technically you should be able to do this, and it will apply to all featured images:

    add_filter( 'generate_page_header_default_size', 'tu_medium_featured_images' );
    function tu_medium_featured_images( $size ) {
        if ( ! is_singular() ) {
            return 'medium';
        }
    
        return $size;
    }

    However, there’s nothing wrong with the method you came up with πŸ™‚

    #540554
    Dean

    Thank you Tom. I prefer your way :-).

    #541129
    Tom
    Lead Developer
    Lead Developer

    No problem! πŸ™‚

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.