Skip to content

Instantly share code, notes, and snippets.

@akccakcctw
Last active April 11, 2017 04:13
Show Gist options
  • Save akccakcctw/db52fbfbfc965d967a6a933f86ccf7f3 to your computer and use it in GitHub Desktop.
Save akccakcctw/db52fbfbfc965d967a6a933f86ccf7f3 to your computer and use it in GitHub Desktop.
利用 ImageMagick 產生縮圖並輸出為 .html
# version 1.1.0
# Rex Tsou
# 2017/04/11
echo -----------
echo v1.1.0
echo Author: Rex Tsou
echo -----------
echo '-h for more information'
function resize_by_width(){
# ${1} : width(px)
magick ${img} -resize ${1} output/${img%.jpg}-thumb.jpg
}
function resize_cli(){
read -r -p "確定要製作縮圖? [y/N] " response
response=${response,,} # to lower case
if [[ $response =~ ^(yes|y)$ ]]
then
mkdir -p output
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Thumbnails</title>
</head>
<body>
<h1>My Imges</h1>' > output/index.html
read -p "輸出圖片寬度為?(px)" width
find -name "*\ *" -exec bash -c 'mv "$0" "${0// /_}"' {} \; # 檔名空白改底線
for dir in $(find -type d -not -path "./output*");
do
echo "正在製作 ${dir}"
echo "<br><p class=\"dir_title\" style=\"font-size:2em\">${dir}</p>" >> output/index.html
for img in $(find -type f -name "*.jpg" -or -name "*.JPG" -or -name "*.png" -or -name "*.PNG");
do
resize_by_width ${width}
echo "<a href=\"${img}-thumb.jpg\"><img src=\"${img}-thumb.jpg\"></a>" >> output/index.html
done
done
echo "</body>" >> output/index.html
echo "</html>" >> output/index.html
else
exit 1
fi
}
# ==========================
usage="
$(basename "$0") 使用說明:
-h 使用說明
-r 調整尺寸(並產生index.html檔案)
本圖片處理工具,使用 ImageMagick 所提供之命令列介面,使用前須先安裝 imagemagick。
詳細安裝資訊請參考官方網站:https://www.imagemagick.org
"
while getopts ':hr' option; do
case "$option" in
h) echo "$usage"
exit
;;
r) resize_cli
exit
;;
:) printf "missing argument for -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment