Last active
December 21, 2019 02:21
-
-
Save mattintosh4/8cdbb49ed2dce7e4a924488746cc02a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# vim:fenc=utf-8 ff=unix ft=sh et sts=4 sw=4 ts=4: | |
set -e | |
set -u | |
#set -x | |
PATH=/usr/local/bin:/usr/bin:/bin | |
: ${QUALITY:=90} | |
log(){ | |
echo "$(date -Is) ${*}" | tee /dev/fd/2 | logger -t "${0##*/}" | |
} | |
convert(){( i=${1:?} s=${2:?} o=${3:?} | |
convert "${i}" -quality 100 -filter Lanczos2 -resize ${s}x${s} ${o}:- | |
)} | |
cwebp(){( p=${1:?} | |
test -d "${p%/*}" \ | |
|| mkdir -p "${p%/*}" | |
cwebp -quiet -q ${QUALITY} -o "${p}" -- - | |
)} | |
jpegoptim(){( p=${1:?} | |
test -d "${p%/*}" \ | |
|| mkdir -p "${p%/*}" | |
jpegoptim --quiet --stdin --stdout --strip-all -m${QUALITY} >"${p}" | |
)} | |
log "画像最適化スクリプト起動" | |
! pgrep -f "${0##*/}" >/dev/null || { log "多重起動を検出しました。処理を終了します"; exit 1; } | |
type convert >/dev/null 2>&1 || { log "$_ not found."; exit 1; } | |
type cwebp >/dev/null 2>&1 || { log "$_ not found."; exit 1; } | |
type jpegoptim >/dev/null 2>&1 || { log "$_ not found."; exit 1; } | |
cd "${1:?}" | |
srcdir=$(pwd) | |
cd .. | |
dstdir_jpeg_s=save_image_jpeg_s | |
dstdir_jpeg_m=save_image_jpeg_m | |
dstdir_webp_s=save_image_webp_s | |
dstdir_webp_m=save_image_webp_m | |
set -- "${srcdir}"/* | |
log "チェック対象 ${#} ファイル from \"${srcdir}\"" | |
log "最適化開始" | |
for f | |
do | |
test -f "${f}" || continue | |
srcname=${f##*/} | |
case ${srcname##*.} in | |
jpg|jpeg|png|\ | |
JPG|JPEG|PNG) | |
: | |
;; | |
*) | |
continue | |
;; | |
esac | |
dstpath_jpeg_s=${dstdir_jpeg_s}/${srcname}_s.jpg | |
dstpath_jpeg_m=${dstdir_jpeg_m}/${srcname}_m.jpg | |
dstpath_webp_s=${dstdir_webp_s}/${srcname}_s.webp | |
dstpath_webp_m=${dstdir_webp_m}/${srcname}_m.webp | |
test : \ | |
-a -s "${dstpath_jpeg_s}" \ | |
-a "${dstpath_jpeg_s}" -nt "${f}" \ | |
-a -s "${dstpath_jpeg_m}" \ | |
-a "${dstpath_jpeg_m}" -nt "${f}" \ | |
-a -s "${dstpath_webp_s}" \ | |
-a "${dstpath_webp_s}" -nt "${f}" \ | |
-a -s "${dstpath_webp_m}" \ | |
-a "${dstpath_webp_m}" -nt "${f}" \ | |
&& continue || : | |
# JPEG | |
log "${f} -> ${dstpath_jpeg_s}" | |
convert "${f}" 240 jpg | jpegoptim "${dstpath_jpeg_s}" & | |
log "${f} -> ${dstpath_jpeg_m}" | |
convert "${f}" 800 jpg | jpegoptim "${dstpath_jpeg_m}" & | |
wait | |
# WebP | |
log "${f} -> ${dstpath_webp_s}" | |
convert "${f}" 240 png | cwebp "${dstpath_webp_s}" & | |
log "${f} -> ${dstpath_webp_m}" | |
convert "${f}" 800 png | cwebp "${dstpath_webp_m}" & | |
wait | |
sleep 0.1 | |
done; unset f | |
log "最適化終了" | |
log "$(printf '%s\n%s' "du -sh *" "$(du -sh * | column -t)")" | |
log "$(printf '%s\n%s' "df -h" "$(df -h)" )" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment