WordPress adding scaled images that don’t exist (1536×1536 and 2048×2048)

The question:

I’m building a theme in _S and using native Gutenberg blocks.

I have the following code via my functions.php file to set my image sizes:

add_theme_support( 'post-thumbnails' );
add_image_size( 'carousel', 1366, 550, true );
add_image_size( 'hero', 1366, 400, true );
add_image_size( 'large-square', 392, 340, true ); 
add_image_size( 'medium-square', 279, 314, true );
add_image_size( 'small-square', 215, 170, true );
add_image_size( 'diagram', 650 ); 
add_image_size( 'full-width', 884 ); 
add_image_size( 'half', 430 ); 
add_image_size( 'third', 279 ); 
add_image_size( 'quarter', 203 );

I also have this code to remove the default image sizes:

function remove_default_image_sizes( $sizes) {
    unset( $sizes['thumbnail']);
    unset( $sizes['medium']);
    unset( $sizes['medium_large']);
    return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'remove_default_image_sizes');

When adding new images or regenerating my thumbnails via WP CLI, there are a number of generated image sizes that appear and do not recognise:

image-scaled-2048x600.jpg
image-scaled-1536x450.jpg
image-scaled-1024x300.jpg

I initially thought it could be down to the recent WordPress 5.3 update that introduced a new way to manage large images.

I tried adding this to functions.php:

add_filter( 'big_image_size_threshold', '__return_false' );

But I still get larger images that are scaled, like so:

image-scaled-2048x600.jpg

Where could WordPress be getting these image sizes from? I’m running a single plugin for the build (ACF).

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I found the culprit!

WordPress 5.3 introduced additional image sizes which can be found via /wp-includes/media.php.

Updating my function, like so, removed the extra sizes:

function remove_default_image_sizes( $sizes) {
    unset( $sizes['large']); // Added to remove 1024
    unset( $sizes['thumbnail']);
    unset( $sizes['medium']);
    unset( $sizes['medium_large']);
    unset( $sizes['1536x1536']);
    unset( $sizes['2048x2048']);
    return $sizes;
}
add_filter('intermediate_image_sizes_advanced', 'remove_default_image_sizes');

Method 2

You could also remove those image sizes completely using remove_image_size (see: https://developer.wordpress.org/reference/functions/remove_image_size/)

Example (to be placed in your functions.php file):

remove_image_size('1536x1536');
remove_image_size('2048x2048');

This function, however, won’t work for default WP image sizes (e.g. ‘thumbnail’, ‘medium’, ‘large’, etc.). There’s a work-around though. Simply set the sizes to 0:

update_option( 'thumbnail_size_h', 0 );
update_option( 'thumbnail_size_w', 0 );
update_option( 'medium_size_h', 0 );
update_option( 'medium_size_w', 0 );
update_option( 'medium_large_size_w', 0 );
update_option( 'medium_large_size_h', 0 );
update_option( 'large_size_h', 0 );
update_option( 'large_size_w', 0 );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment