目前支持.jpg
/.jpeg
/.bmp
/.png
格式图片
find
命令查找指定目录图片(同时对图片大小和时间进行了筛选)
Shell脚本需要调用OptiPNG和jpegoptim这两个工具,安装OptiPNG、jpegoptim是必须的,这里仅以CentOS为列,其它Linux系统可自行搜索安装。
#安装epel源
yum -y install epel-release
#安装OptiPNG
yum -y install optipng
#安装jpegoptim
yum -y install jpegoptim
#下载批量压缩脚本
wget https://raw.githubusercontent.com/helloxz/shell/master/img_compress.sh
#执行shell脚本
bash img_compress.sh /data/wwwroot/imgurl
其中/data/wwwroot/imgurl
修改为自己的图片目录(需要使用绝对路径),执行的效果如下:
[root@imgurl ~]# bash img_compress.sh /data/wwwroot/imgurl
** Processing: /data/wwwroot/imgurl/imgs/2019/08/a259e6290bacaf3c.png
1137x557 pixels, 3x8 bits/pixel, RGB
Input IDAT size = 112086 bytes
Input file size = 112395 bytes
Trying:
zc = 9 zm = 9 zs = 0 f = 0 IDAT size = 74656
zc = 9 zm = 8 zs = 0 f = 0 IDAT size = 74210
Selecting parameters:
zc = 9 zm = 8 zs = 0 f = 0 IDAT size = 74210
Output IDAT size = 74210 bytes (37876 bytes decrease)
Output file size = 74483 bytes (37912 bytes = 33.73% decrease)
/data/wwwroot/imgurl/imgs/2019/08/58427da011079d66.jpg 2228x4006 24bit N Exif IPTC JFIF [OK] 565511 --> 565481 bytes (0.01%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/bbd6e6b825079540.jpg 1440x1090 24bit N JFIF [OK] 358749 --> 358655 bytes (0.03%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/c2afa79235ce19ec.jpg 1080x1920 24bit N JFIF [OK] 215290 --> 215290 bytes (0.00%), skipped.
/data/wwwroot/imgurl/imgs/2019/08/af0225dd07a1d19a.jpg 1080x2340 24bit N JFIF [OK] 145853 --> 145826 bytes (0.02%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/bf49aa2868dd3892.jpg 1024x1318 24bit P JFIF [OK] 128645 --> 128548 bytes (0.08%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/3d012339766e8830.jpg 9933x7017 24bit N IPTC Exif ICC XMP JFIF [OK] 2724463 --> 2724463 bytes (0.00%), skipped.
/data/wwwroot/imgurl/imgs/2019/08/751528874c3377ef.jpg 1024x1318 24bit P JFIF [OK] 115894 --> 115871 bytes (0.02%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/c4719f67827b47b5.jpg 2208x1242 24bit N Exif IPTC JFIF [OK] 409022 --> 136213 bytes (66.70%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/97e5c418839595f8.jpg 2208x1242 24bit N Exif IPTC JFIF [OK] 409641 --> 143095 bytes (65.07%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/cd1337dbe4a9de63.jpg 2208x1242 24bit N Exif IPTC JFIF [OK] 369246 --> 130287 bytes (64.72%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/643a8733650289fc.jpg 2208x1242 24bit N Exif IPTC JFIF [OK] 469150 --> 182604 bytes (61.08%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/3fa4595aafbdcec4.jpg 2208x1242 24bit N Exif IPTC JFIF [OK] 498075 --> 193409 bytes (61.17%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/6b3a4381757675e0.jpg 776x1173 24bit N JFIF [OK] 294311 --> 212403 bytes (27.83%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/c04b62741e71b4ab.jpg 2208x1242 24bit N Exif IPTC JFIF [OK] 502573 --> 197138 bytes (60.77%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/cc3e5311aa96dcd1.jpg 2560x1440 24bit N JFIF [OK] 787748 --> 162981 bytes (79.31%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/f1c439711b64a9f1.jpg 1229x1410 24bit N JFIF [OK] 308777 --> 136253 bytes (55.87%), optimized.
/data/wwwroot/imgurl/imgs/2019/08/74c80a3b91d20c4e.jpg 1386x1920 24bit N Exif JFIF [OK] 241196 --> 241196 bytes (0.00%), skipped.
/data/wwwroot/imgurl/imgs/2019/08/93f17bbf3f4e00d0.jpg 1056x984 24bit N Exif JFIF [OK] 231805 --> 115288 bytes (50.27%), optimized.
#先将脚本赋予执行权限
chmod +x img_compress.sh
#添加定时任务
crontab -e
#将内容复制到最后并保存(每小时执行一次)
*/60 * * * * /yourpath/img_compress.sh /data/wwwroot/imgurl > /dev/null
#重载crontab
service crond reload
/yourpath/img_compress.sh
为脚本的绝对路径
/data/wwwroot/imgurl
为图片的绝对路径
脚本默认查找60分钟以内曾被修改过的图片文件(再加上定时任务1小时执行一次,这样就不会去压缩重复的图片),并且大于100kb的图片才会进行压缩,您也可以下载脚本后对这些设定自行做出修改(图片的压缩等级也可以适当修改)。此方案适用于图片存放在Linux服务器环境下。
版权免责声明: 本站内容部分来源于网络,请自行鉴定真假。如有侵权,违法,恶意广告,虚假欺骗行为等以上问题联系我们删除。
本文地址:https://18793.cc/index/article/view/11.html