需要工具cwebp & inotifywait
# cwebp安装
yum-y install libwebp-tools
# inotifywait 安装
yum-y install inotify-tool
vim /usr/local/scripts/img2webp.sh
#!/bin/sh
# -----------------------------------------------------------------------------------------------
# Filename: img2webp.sh
# Version: 0.0.1
# Date: 2019/11/30 2:05:10
# Author: Zhang Zhao
# Description: Monitor newly added png and jpg images in a directory and convert to webp format.
# -----------------------------------------------------------------------------------------------
IMG_DIR=/var/www/html/media
/usr/bin/inotifywait -mrq -e create,modify,attrib $IMG_DIR | while read path action file;
do
OLDFILE="$path$file"
NEWFILE="$OLDFILE.webp"
if [[ $(file -b $OLDFILE) =~ ^('PNG '|'JPEG ') ]]; then
cwebp $OLDFILE -o $NEWFILE
chown apache:apache $NEWFILE
elif [[ $(file -b $OLDFILE) =~ ^('GIF ') ]]; then
gif2webp $OLDFILE -o $NEWFILE
chown apache:apache $NEWFILE
fi
done
chmod +x /usr/local/scripts/img2webp.sh
nohup /root/scripts/img2webp.sh &
echo "/bin/sh /usr/local/scripts/img2webp.sh &" >> /etc/rc.local