
First timebuilding your own websiteFriends may not know that images uploaded to the website need to have their dimensions compressed, otherwise they will occupy a lot of hard drive space over time. Naiba recently encountered several WordPress e-commerce websites where image attachments filled up the disk. So, here's how to batch compress image dimensions on a Linux VPS.
Prerequisite:
This method is only for Linux systemsVPS, if you are usingWordPress hosting, then this method cannot be used.
Specific Method:
Here we use OptiPNG and jpegoptim tools along with scripts to automatically compress image sizes.
1. Install OptiPNG and jpegoptim
#安装epel源 yum -y install epel-release #安装OptiPNG yum -y install optipng #安装jpegoptim yum -y install jpegoptim
The above command is for CentOS system. If you are using Debian, Ubuntu, or other systems, please search for the corresponding command yourself.
2. Use shell script to batch compress
#下载批量压缩脚本 wget https://raw.githubusercontent.com/helloxz/shell/master/img_compress.sh #执行shell脚本 bash img_compress.sh /home/wwwroot/blog.naibabiji.comhttps://edgeone.naibabiji.com/wp-content/uploads
In the above command, replace /home/wwwroot/blog.naibabiji.com with your own website directory path.
The wp-content/uploads folder corresponds to WordPress's upload folder, where our website's image attachments are stored.
The script defaults to processing files larger than 100k and modified within 1 hour to avoid repeated compression. You can edit the parameters in the script to set a longer time or remove the time limit.
The script is provided byXiao ZShare
Methods for Compressing Images in WordPress
The above method achieves automatic image compression by installing software on the server and using a script (fully automatic requires a scheduled task). So, if you are using a WordPress host, how do you achieve automatic image compression?
Here are several methods:
- Install a batch image compression plugin. Well-known ones includeEWWW Image Optimizer、Smush、ShortPixel Image Optimizeretc. Naibabiji usesCompress JPEG & PNG images
- Manually compress images locally before uploading them yourself;
- Enable WordPress's built-in image compression feature. Refer tothe article on optimizing WordPress image upload speed;
- Use an image hosting or CDN service, which typically comes with automatic image compression features. Refer toWebsite images loading slowlythis article.