Last active
March 8, 2019 06:42
-
-
Save hite/2b5c413176e85597e32de39e90ab0fc6 to your computer and use it in GitHub Desktop.
compress images in some directory using tinypng service 使用 tinypng 来压缩图片
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 | |
# author: [email protected] | |
log_info() { | |
printf "[\e[36m%s\e[0m] [\e[32mINFO\e[0m] $*\n\r" "$(date +'%H:%M:%S')" | |
} | |
log_warn() { | |
printf "[\e[36m%s\e[0m] [\e[33mWARNING\e[0m] $*\n\r" "$(date +'%H:%M:%S')" | |
} | |
log_error() { | |
printf "[\e[36m%s\e[0m] [\e[91mERROR\e[0m] $*\n\r" "$(date +'%H:%M:%S')" | |
} | |
function help() | |
{ | |
log_error "输入目标路径或者图片地址, sh ./compressImages.sh fileNameOrDirectory" | |
exit 1 | |
} | |
if [ $# -eq 0 ] | |
then | |
help | |
fi | |
api_key="1Sf6MzPVoDFUqvhRqeuQ8GkwNKODPoJx" | |
API_LIMIT=500 | |
process_image() { | |
if [[ $1 != *.png ]]; then | |
log_info "非图片文件,跳过:"$1 | |
return | |
fi | |
t=$(ls -l "$1" | awk '{print $5}') | |
orig_size=$((t / 1024)) | |
if [ "$orig_size" -gt 1024 ]; then | |
orig_size_display="$(printf "%0.2f\n" "$(awk "BEGIN {print ($orig_size)/1024}")") MB" | |
else | |
orig_size_display="$orig_size KB" | |
fi | |
log_info "Compressing \"$1\".... (${orig_size_display}) \n" | |
curl --progress-bar --user api:"$api_key" --data-binary @"$1" --output api_response.txt -i https://api.tinify.com/shrink | |
if [ -f api_response.txt ]; then | |
status_code=$(head <api_response.txt -1 | awk '{print $2}') | |
else | |
status_code="-1" | |
fi | |
if [ "$status_code" != 201 ]; then | |
log_warn "Something went wrong! Error code: $status_code Retrying....\n" | |
# if [ -f api_response.txt ]; then | |
# rm api_response.txt 2>/dev/null | |
# fi | |
continue | |
fi | |
download_url=`grep <api_response.txt location | awk '{print $2}'` | |
compression_count="$(grep <api_response.txt compression-count | awk '{print $2}')" | |
compression_count=${compression_count%$'\r'} | |
log_info "Total API Requests: $compression_count/$API_LIMIT" | |
if [ "$compression_count" -gt "$((API_LIMIT - 1))" ]; then | |
log_error "API Limit Reached! Exiting....\n" | |
rm api_response.txt | |
exit 1 | |
fi | |
# # 覆盖原来的文件 | |
curl ${download_url%$'\r'} --progress-bar --user api:"$api_key" --header "Content-Type: application/json" --data '{ "preserve": ["location", "creation"] }' --output "$1" | |
new_size="$(ls -l "$1" | awk '{print $5}')" | |
if [ "$new_size" = "" ]; then | |
new_size=1 | |
fi | |
new_size="$((new_size / 1024))" | |
if [ "$new_size" -gt 1024 ]; then | |
new_size_display="$(printf "%0.2f\n" "$(awk "BEGIN {print ($new_size)/1024}")") MB" | |
else | |
new_size_display="$new_size KB" | |
fi | |
log_info "Done compressing \"$1\" (${new_size_display})\n" | |
rm api_response.txt | |
echo "" | |
} | |
function read_dir(){ | |
ls $1 | while read FILE; do | |
file="$1/$FILE" | |
if [ -d "$file" ] #注意此处之间一定要加上空格,否则会报错 | |
then | |
read_dir "$file" | |
else | |
# echo $file #在此处处理文件即可 | |
process_image "$file" | |
fi | |
done | |
} | |
# 读取命令行参数 | |
for f in "$@" | |
do | |
if [ -d "$f" ] | |
then | |
read_dir "$f" | |
else | |
# echo $file #在此处处理文件即可 | |
process_image "$f" | |
fi | |
done | |
afplay /System/Library/Sounds/Submarine.aiff | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
验证压缩是否正确需要人眼检查