欢迎来到站长教程网!

PHP教程

当前位置: 主页 > 网络编程 > PHP教程

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

更新时间:2025-10-19 04:51:41|栏目:PHP教程|点击:21 次

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

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

 

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

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

 

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

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

主题)看到以下代码:

 

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里就可以生效了,注意此代码对之前已经上传完的图无效,之前生成的缩略图需要自行删除。

 

/**
 * 禁止生成768*516图片
 */
add_filter( 'intermediate_image_sizes', function( $sizes )
{
return array_filter( $sizes, function( $val )
{
return 'medium_large' !== $val; // Filter out 'medium_large'
} );
} );

上一篇:PHP中实现一个简易三角形的方法

栏    目:PHP教程

下一篇:PHP 7安装使用体验之性能大提升,兼容性强,扩展支

本文标题:PHP中实现九九乘法口诀表

本文地址:http://tp2.com/index.php?m=home&c=View&a=index&aid=172

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:88888888 | 邮箱:888petersds@gmail.com

Copyright © 2002-202X 某某站长教程网 版权所有 非商用版本粤ICP备xxxxxxxx号