First time
building 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 systems
VPS, if you are using
WordPress hosting, then this method cannot be used.
Specific Method:Here we use the tools OptiPNG and jpegoptim along with a script to achieve automatic image dimension compression. 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 systems. If you are using Debian, Ubuntu, or other systems, please search for the corresponding command yourself. 2. Use a shell script to achieve batch compression.
#下载批量压缩脚本
wget https://raw.githubusercontent.com/helloxz/shell/master/img_compress.sh
#执行shell脚本
bash img_compress.sh /home/wwwroot/blog.naibabiji.com/wp-content/uploads
In the above command, replace `/home/wwwroot/blog.naibabiji.com` with your own website directory path. The following `wp-content/uploads` corresponds to the WordPress uploads folder, where all our website's image attachments are located. The script is set by default to target files larger than 100k and modified within the last hour to avoid re-compressing images. You can edit the parameters in the script to set a longer time or remove the time restriction. The script was shared by
Xiao ZShare
Methods for Compressing Images in WordPress
The above method achieves automatic image compression by installing software on the server combined with a script (for full automation, it also needs to be paired with a scheduled task). So, if you are using WordPress hosting, how can you achieve automatic image compression? The methods are as follows:
- 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.
Comments are closed
The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.