Last active
December 12, 2019 07:21
-
-
Save denisx/56d462660c6913f11ac792d2670e4e7d to your computer and use it in GitHub Desktop.
Check images at folder, and optimize them with tinypng.com service
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 denisx | |
# find iamges at folder and optimize them | |
# you need a key from https://tinypng.com/developers | |
# system dependence: jq, curl | |
# ---=== settings === --- | |
# you need a key from https://tinypng.com/developers | |
dev_key="xxx" | |
# image extension | |
ext="png" | |
# postfix for save original images | |
post_fix="tiny-orig" | |
# postfix for save a tip: touched image or not | |
# (if script or net down, just restart) | |
not_need_post_fix="not-need.tmp" | |
# do not optimize, if profit lower then x % | |
profit_percent_need=20 | |
# do not optimize, if profit lower then x kb | |
profit_size_need=10 | |
# ---=== script === --- | |
# work stat | |
skiped_not_need=0 | |
skiped_done=0 | |
skiped_error=0 | |
tiny_done=0 | |
i=0 | |
echo "start tiny foreach" | |
for f in $(find . -type f -size "+1M" -name "*.$ext") | |
do | |
i=$((i+1)) | |
echo "image n${i}: $f" | |
# no file - go to next image | |
if [[ -f "$f" ]] | |
then | |
# ok | |
skiped_not_need=$((skiped_not_need)) | |
else | |
echo "No file: $f" | |
skiped_error=$((skiped_error+1)) | |
continue | |
fi | |
# check, | |
# image is a original for optimized one, | |
# or has original sibling | |
if [[ $f == *".${post_fix}.$ext" || | |
-f "${f}.${post_fix}.$ext" ]] | |
then | |
echo "Not need, go to next" | |
echo "" | |
skiped_not_need=$((skiped_not_need+1)) | |
continue | |
fi | |
# check, if already optimized | |
if [[ -f "${f}.$not_need_post_fix" ]] | |
then | |
echo "Already optimized, go to next" | |
echo "" | |
skiped_done=$((skiped_done+1)) | |
continue | |
fi | |
json_path="${f}.src.tmp" | |
echo "$json_path" | |
# download image, get img.png.src from json responce | |
curl https://api.tinify.com/shrink \ | |
--user api:"$dev_key" \ | |
--data-binary @"$f" \ | |
--dump-header /dev/stdout \ | |
-o "$json_path" | |
# parse json | |
orig_size=$(jq '.input.size' $json_path) | |
# if json is empty - go to next | |
if [[ -z "$orig_size" ]] | |
then | |
echo "No json" | |
skiped_error=$((skiped_error+1)) | |
#touch "$f"."$not_need_post_fix" | |
continue | |
fi | |
res_size=$(jq '.output.size' $json_path) | |
res_url=$(jq '.output.url' $json_path) | |
# prevent float to int | |
profit_percent=$((100 - ((res_size * 100) / orig_size))) | |
profit_size=$(((orig_size - res_size) / 1024 )) | |
echo "$orig_size | $res_size" | |
# if condition true, download | |
if [[ "$profit_percent" -gt "$profit_percent_need" && "$profit_size" -gt "$profit_size_need" ]] | |
then | |
echo "--- === Save image! === ---" | |
#clear url | |
download_path=${res_url:1:${#res_url}-2} | |
# download optimized image, https | |
curl "$download_path" \ | |
--user api:"$dev_key" \ | |
--dump-header /dev/stdout \ | |
-o "${f}.tmp" | |
# finalized download | |
if [[ -f "${f}.tmp" ]] | |
then | |
# save orig image | |
cp "$f" "$f"."$post_fix"."$ext" | |
# move optimized to original image place | |
mv "${f}.tmp" "$f" | |
tiny_done=$((tiny_done+1)) | |
fi | |
else | |
echo "-- == Not need to save :/ == --" | |
touch "$f"."$not_need_post_fix" | |
skiped_not_need=$((skiped_not_need+1)) | |
fi | |
# remove temp json | |
if [[ -f "$json_path" ]] | |
then | |
rm "$json_path" | |
fi | |
echo "save $profit_percent %, $profit_size Kb" | |
echo "" | |
done | |
# work stats | |
echo "all=$i" | |
echo "skiped_not_need=$skiped_not_need" | |
echo "skiped_done=$skiped_done" | |
echo "skiped_error=$skiped_error" | |
echo "tiny_done=$tiny_done" | |
echo "exit tiny foreach" | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment