Last active
January 9, 2017 10:38
-
-
Save akccakcctw/80b4e8464b58fa045492218ffb0af6e1 to your computer and use it in GitHub Desktop.
利用 ImageMagick 壓縮圖片
This file contains 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 | |
# version 1.3.2 | |
# Rex Tsou | |
# 2017/01/09 | |
echo -------------------- | |
echo 圖片壓縮工具 v1.3.2 | |
echo Author: Rex Tsou | |
echo -------------------- | |
# ============================================== | |
function resize(){ | |
# ${1} = width(px) | |
# ${2} = height(px) | |
magick ${img} -resize ${1} -gravity center -crop ${1}x${2}+0+0 +repage output/${img%.jpg}.jpg | |
} | |
function compress(){ | |
# ${1} = maxium file size(kb) | |
magick ${img} -strip -interlace Plane -define jpeg:extent=${1}kb ${img} | |
} | |
function resize_cli(){ | |
read -r -p "確定要製作縮圖? [y/N] " response | |
response=${response,,} # tolower | |
if [[ $response =~ ^(yes|y)$ ]] | |
then | |
mkdir -p output | |
read -p "請指定輸出寬度(px) " width | |
read -p "請指定輸出高度(px) " height | |
for img in $(find -type f -name "*.jpg" -printf '%P\n'); | |
do | |
# echo $img >> list.txt | |
resize ${width} ${height} | |
done | |
else | |
exit 1 | |
fi | |
} | |
function compress_cli(){ | |
read -r -p "要壓縮圖片嗎? [y/N] " response | |
response=${response,,} # tolower | |
if [[ $response =~ ^(yes|y)$ ]] | |
then | |
read -p "要壓縮成__kb?(輸入數字即可) " compressed | |
for img in $(find -type f -name "*.jpg" -printf '%P\n' | grep output/); | |
do | |
compress ${compressed} | |
done | |
else | |
exit 1 | |
fi | |
} | |
usage=" | |
$(basename "$0") 使用說明: | |
-h 使用說明 | |
-r 調整尺寸 | |
-c 壓縮圖片 | |
本圖片處理工具,使用 ImageMagick 所提供之命令列介面,使用前須先安裝 imagemagick。 | |
詳細安裝資訊請參考官方網站:https://www.imagemagick.org | |
" | |
while getopts ':hrc' option; do | |
case "$option" in | |
h) echo "$usage" | |
exit | |
;; | |
r) resize_cli | |
exit | |
;; | |
c) compress_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 | |
# ============================================== | |
read -r -p "確定要製作縮圖? [y/N] " response | |
response=${response,,} # tolower | |
if [[ $response =~ ^(yes|y)$ ]] | |
then | |
mkdir -p output | |
read -p "請指定輸出寬度(px) " width | |
read -p "請指定輸出高度(px) " height | |
for img in $(find -type f -name "*.jpg" -printf '%P\n'); | |
do | |
# echo $img >> list.txt | |
resize ${width} ${height} | |
done | |
else | |
exit 1 | |
fi | |
read -r -p "要順便壓縮嗎? [y/N] " response | |
response=${response,,} # tolower | |
if [[ $response =~ ^(yes|y)$ ]] | |
then | |
read -p "要壓縮成__kb?(輸入數字即可) " compressed | |
for img in $(find -type f -name "*.jpg" -printf '%P\n' | grep output/); | |
do | |
compress ${compressed} | |
done | |
else | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment