inotifywait 监控新增图片->转webp格式

需要工具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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容