前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >解决WordPress图片生成多张缩略图的几种方法

解决WordPress图片生成多张缩略图的几种方法

原创
作者头像
IDC科技
修改2023-05-24 15:07:26
2.7K0
修改2023-05-24 15:07:26
举报
文章被收录于专栏:IDC杂谈

对于网站运营人员来说,网站备份是很重要的。最近我在备份网站数据时,也就几天时间,发现备份的压缩文件增加了好几十M,由于是新的网站,所以这种增长速度是非常快的。于是小编赶紧登陆centos7系统后台看了下,发现主要是增加了图片的存储大小。

细心的看了下,明明是一张清晰的图片,硬生生的被系统生成了很多张不同的缩略图。由于我的是博客网站,所以不需要这么多的缩略图,也占用空间,于是赶紧网上找资料来解决此问题。下面简单说说中方法。

第一种、在WordPress后台进行设置

基本处理这个问题,只要在仪表盘->设置->媒体里把缩略图禁止即可,见图示操作。

第二种、在functions.php文件中进行修改

按照第一种方法设置完之后,可能会发现果然继续上传图片的时候只有一张。然而在上传一张高清图片的时候突然发现又多了一张另外尺寸的图片,有的比原始尺寸还大。而且尺寸不一样,于是在functions.php文件(themolio

主题)看到以下代码:

代码语言:javascript
复制
function themolio_setup() {
 /* Make Themolio available for translation.
 * Translations can be added to the /languages/ directory.
 * If you're building a theme based on Themolio, use a find and replace
 * to change 'themolio' to the name of your theme in all the template files.
 */
 load_theme_textdomain('themolio', get_template_directory().'/languages' );
 add_editor_style();
 add_theme_support('automatic-feed-links');
 add_theme_support('post-thumbnails');
 /**
 * This sets up the image size for the grid based top-down layouts (with no sidebar).
 * If you change the width/height of your content,
 * you will have to modify the width and height mentioned below as well
 */
 add_image_size('themolio-two-col-grid-image-nosidebar',460,300,true);
 add_image_size('themolio-three-col-grid-image-nosidebar',290,200,true);
 add_image_size('themolio-four-col-grid-image-nosidebar',210,150,true);
 /**
 * This sets up the image size for the grid based top-down layouts (with sidebar).
 * If you change the width/height of your content,
 * you will have to modify the width and height mentioned below as well
 */
 add_image_size('themolio-two-col-grid-image-sidebar',356,250,true);
 add_image_size('themolio-three-col-grid-image-sidebar',230,150,true);
 add_image_size('themolio-four-col-grid-image-sidebar',171,110,true);
 /**
 * This sets up the image size for the featured image.
 * If you change the width/height of your content,
 * you will have to modify the width and height mentioned below as well
 */
 add_image_size('themolio-featured-image',800,300,true);
 register_nav_menu('primary', __('Primary Menu', 'themolio'));
 add_theme_support('post-formats', array('link', 'gallery', 'status', 'quote', 'image', 'video'));
 if(themolio_is_wp_version('3.4')) {
 add_theme_support('custom-background');
 } else {
 add_custom_background();
 }
}

其中add_image_size就是增加了尺寸设置的方法,根据需要注释掉即可。其他主题也可以依此类推,只要搜索关键字add_image_size就可以了。

安装上面方法删除了add_image_size,再次上传图片看看,发现各种小的尺寸都没有了,也清爽多了。但是还会多了两种大尺寸图片,比原来尺寸还大,这一般是像素宽超过700PX的图片自动生成medium large尺寸的图片,大概700*300多PX,有的是1024*502等等。。如果遇到这种情况可以继续修改functions.php文件。

把以下代码直接放入functions.php里就可以生效了,注意此代码对之前已经上传完的图无效,之前生成的缩略图需要自行删除。

代码语言:javascript
复制
add_filter( 'intermediate_image_sizes', function( $sizes )
{
return array_filter( $sizes, function( $val )
{
return 'medium_large' !== $val; // Filter out 'medium_large'
} );
} );

第三、去除网页源代码中的srcset和sizes属性

有的时候,虽然不同尺寸的缩略图都没有了,但是在网页源代码里面,同一张图片还会插入多个尺寸大小的图片,也即是srcset和sizes属性。虽然在前段看起来没什么毛病,但是冗余的代码不利于脚步的优化以及蜘蛛的爬取。

这种情况,一般是图片在超过多大的时候会自动生成本地地址srcset和sizes属性。这里需要禁止掉才可以。

同样在Functions.php文件中添加代码即可:

//禁止响应式图片(www.ecscoupon.com)

function disable_srcset( $sources ) {

return false;

}

add_filter( 'wp_calculate_image_srcset', 'disable_srcset' );

添加到当前主题Functions.php文件中,刷新缓存,即可解决问题,这样就不会同时插入不同尺寸大小的同一张图片了。

总之,以上就是解决WordPress程序同一张图片不同大小尺寸缩略图的问题,仅供大家参考。有时候网站如果不需要生产多张缩略图的话,那么大家可以参考上述方法解决。如果仍然不能解决,可以寻求相关人员解决。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档