WordPress生成的图像大小过大

时间:2022-03-05 作者:Botond Vajna

我删除了默认图像大小,并使用以下代码添加了自定义图像大小:

// Remove default image sizes
function remove_default_image_sizes( $sizes ) {
  
    /* Default WordPress */
    unset( $sizes[ \'thumbnail\' ]);       // Remove Thumbnail (150 x 150 hard cropped)
    unset( $sizes[ \'medium\' ]);          // Remove Medium resolution (300 x 300 max height 300px)
    unset( $sizes[ \'medium_large\' ]);    // Remove Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
    unset( $sizes[ \'large\' ]);           // Remove Large resolution (1024 x 1024 max height 1024px)

    /* With WooCommerce */
    unset( $sizes[ \'shop_thumbnail\' ]);  // Remove Shop thumbnail (180 x 180 hard cropped)
    unset( $sizes[ \'shop_catalog\' ]);    // Remove Shop catalog (300 x 300 hard cropped)
    unset( $sizes[ \'shop_single\' ]);     // Shop single (600 x 600 hard cropped)

    return $sizes;
    }
  
    add_filter( \'intermediate_image_sizes_advanced\', \'remove_default_image_sizes\' );
//


// post thumbnail sizes
    

    add_action( \'after_setup_theme\', \'vkb_setup_theme\' );
    function vkb_setup_theme() {

        add_theme_support( \'post-thumbnails\' );

        set_post_thumbnail_size( 250, 250, true );

        add_image_size( \'XS\', 128 );
        add_image_size( \'S\', 256 );
        add_image_size( \'M\', 512 );
        add_image_size( \'XL\', 1024 );
    }
//

应该有XS、S、M、XL、Post\\u缩略图和原始大小,即6。

然而,当我上传一个大图像(2898 x 2346)进行测试时,我有9个图像,我还有3个尺寸:-1536x1253,-2048x1671,还有一个命名为-缩放为2560x2088。

为什么我有这些额外的尺寸,我如何才能摆脱它们?非常感谢。

1 个回复
SO网友:Botond Vajna

我在这里找到了答案:https://gist.github.com/iftee/73cbd9f080c5c7a943cff5e806997f90?permalink_comment_id=3314876#gistcomment-3314876

我补充道:

unset( $sizes[\'1536x1536\'] );
unset( $sizes[\'2048x2048\'] );

remove_image_size( \'1536x1536\' );
remove_image_size( \'2048x2048\' );
这会删除两个较大的图像大小,但按比例缩放的图像大小仍然存在

我发现:

When a new image is uploaded, WordPress will detect if it is a “big” image by checking if its height or its width is above a big_image threshold. The default threshold value is 2560px, filterable with the new big_image_size_threshold filter. If an image height or width is above this threshold, it will be scaled down, with the threshold being used as max-height and max-width value. The scaled-down image will be used as the largest available size. In this case, the original image file is stored in the uploads directory and its name is stored in another array key in the image meta array: original_image. To be able to always get the path to an originally uploaded image a new function wp_get_original_image_path() was introduced.

资料来源:https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/

要删除它,请执行以下操作:

add_filter( \'big_image_size_threshold\', \'__return_false\' );

相关推荐

Images with overlay

我有一些图片在一个容器中,我想添加一个覆盖和图标。这不是现成的,但我找到了一些有用的代码:HTML:<div class=\"main\"> <span class=\"featured\"><img src=\"http://joshrodg.com/IMG_001-258x258.jpg\" title=\"\" alt=\"\"></span> </div> CSS:.featured {