0

If I upload an image in Wordpress, it usually generates more images sizes than what I have indicated in my settings -> media (small 150, medium 300, large 1024).

For example, if I upload a 4K image, it generates up to 7 more copies + the original (150, 300, 768, 1024, 1536, 20148, scaled, original).

I'm using a child theme based on Underscores, but it seems that no file functions tell Wordpress to generate the other copies.

Where do they come from?

4

WordPress generates more than 3 thumbnails or image sub sizes because there are actually more than 3 standard/core image sizes in WordPress. Moreover, themes and plugins can also register custom image sizes. :)

The 6+1 standard/core image sizes

  • Default (4): Thumbnail, Medium, Large and Medium Large (medium_large), but only the first three that you can configure by default at wp-admin → Settings → Media.

  • Default additional/custom sizes (2): 1536x1536 and 2048x2048 which are added internally/automatically through _wp_add_additional_image_sizes() which uses add_image_size().

  • Scaled (with -scaled in the image file name): Available for high-res and non-PNG images, where WordPress makes a copy of the original (BIG) image and scales it down to the threshold size which defaults to 2560 pixels.

For example, if I upload a 4K image, it generates up to 7 more copies + the original (150, 300, 768, 1024, 1536, 20148, scaled, original).

So in that case (but note that I believe you made a typo with that "20148" and it should be 2048), the 7 copies of the original image, are most likely:

  • 150 — Thumbnail
  • 300 — Medium
  • 768 — Medium Large
  • 1024 — Large
  • 1536 — Core custom image size (1536x1536)
  • 2048 — Core custom image size (2048x2048)
  • Scaled — Core Scaled

And if you want to be sure, you can use get_intermediate_image_sizes() to check all the registered image sizes on the site. You can also use wp_get_additional_image_sizes() to get just the additional/custom sizes (including both the 1536x1536 and 2048x2048 added by WordPress).

In fact, you can use wp_get_attachment_metadata() (see examples at the very bottom) to check the sizes available for an image attachment.

Non-core/Other custom image sizes

Themes and plugins can use the same add_image_size() to register custom image sizes, so if you noticed extra sizes than the core ones (see above), then the extra sizes may have been added by the active theme or plugins on the site. E.g.

So for such custom image sizes, you can search for add_image_size or set_post_thumbnail_size in the theme or plugin files.

How to disable certain image sizes from being generated

  • For the 6 standard/core sizes and all other sizes added via add_image_size(), e.g. medium_large and 2048x2048, you can use the intermediate_image_sizes_advanced hook:

    add_filter( 'intermediate_image_sizes_advanced', function ( $sizes ) {
        // Disable the 2048x2048 size.
        unset( $sizes['2048x2048'] );
    
        // Or maybe allow specific sizes only:
        /*
        $allowed = [ 'thumbnail', 'medium', 'large' ];
        foreach ( $sizes as $name => $size ) {
            if ( ! in_array( $name, $allowed ) ) {
                unset( $sizes[ $name ] );
            }
        }
        */
    
        return $sizes;
    } );
    
  • For the core Scaled size, you'd want to use the big_image_size_threshold hook:

    // Disable the threshold.
    add_filter( 'big_image_size_threshold', '__return_false' );
    
    // Or maybe increase it:
    /*
    add_filter( 'big_image_size_threshold', function () {
        return 3032;
    } );
    */
    

Either way, make sure you know what you're doing and do it properly.

Core Scaled and wp_get_attachment_metadata()

( I mentioned this in the previous revisions, so I thought I should include it here. )

For image attachments with the core Scaled image, the main width, height and file items as you can see below, will point to the scaled image and not the original one. However, the original file's name is available in original_image.

// Query an attachment with a core Scaled image.
var_dump( wp_get_attachment_metadata( 123 ) );
/* Sample output:
array(6) {
  ["width"]=>
  int(2560)
  ["height"]=>
  int(1440)
  ["file"]=>
  string(25) "2020/07/hi-res-scaled.jpg"
  ["sizes"]=>
  array(7) {
    ["medium"]=>
    array(4) {
      ["file"]=>
      string(18) "hi-res-300x169.jpg"
      ["width"]=>
      int(300)
      ["height"]=>
      int(169)
      ["mime-type"]=>
      string(10) "image/jpeg"
    }
    ["large"]=>
    ...
    ["thumbnail"]=>
    ...
    ["medium_large"]=>
    ...
    ["1536x1536"]=>
    ...
    ["2048x2048"]=>
    ...
  }
  ["image_meta"]=>
  array(12) {
    ...
  }
  ["original_image"]=>
  string(10) "hi-res.jpg"
}
*/

// Query an attachment without a core Scaled image.
var_dump( wp_get_attachment_metadata( 456 ) );
/* Sample output: (no original_image)
array(5) {
  ["width"]=>
  int(1024)
  ["height"]=>
  int(768)
  ["file"]=>
  string(21) "2020/07/foo-image.jpg"
  ["sizes"]=>
  array(3) {
    ["medium"]=>
    array(4) {
      ["file"]=>
      string(21) "foo-image-300x225.jpg"
      ["width"]=>
      int(300)
      ["height"]=>
      int(225)
      ["mime-type"]=>
      string(10) "image/jpeg"
    }
    ["thumbnail"]=>
    ...
    ["medium_large"]=>
    ...
  }
  ["image_meta"]=>
  array(12) {
    ...
  }
}
*/
| improve this answer | |
  • I just deactivated all the plugins, and I have even momentarily deleted the content of the two files functions (parent and child) ... and it keep generating more copies of the 3 standards.... – Antonino Lattene Jul 10 at 10:13
  • Even changing to another theme (twentynineteen) it keeps generating 7 copies (+1 which is setted in the twentynineteen functions file). – Antonino Lattene Jul 10 at 10:18
  • I hope the revised answer helps? – Sally CJ Jul 10 at 13:17
  • Thanks for the reply, very helpful. I still don't know how/where to use get_intermediate_image_sizes (), but I'll try to find out. Anyway, all these sizes are introduced by the core of wordpress (no theme, no plugins), I only doubt for the "scaled" version. What is the best way to disable all these standard measures and tell wordpress to use only the ones I want? – Antonino Lattene Jul 10 at 16:01
  • There's no "best" way, but what I suggested in the answer should work fine. PS: Sorry for the many revisions.. and if the answer answered your question, I'd appreciate it if you can accept the answer. :) – Sally CJ Jul 11 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.