Skip to content

Instantly share code, notes, and snippets.

@dhoko
Created February 7, 2019 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhoko/804eaf2977b7c88166555288c64357b1 to your computer and use it in GitHub Desktop.
Save dhoko/804eaf2977b7c88166555288c64357b1 to your computer and use it in GitHub Desktop.
Resize Zip/rar with images
#!/usr/bin/env bash
set -eo pipefail
IFS=$'\n'
function getExtension {
local file =$(basename -- "$1");
echo "${1##*.}";
}
function convertImages {
cd "$1";
for file in $(ls *.{jpg,jpeg,png})
do
local size=$(identify "$file" | awk '{print $3}' | awk -F x '{print $2}');
if [ "$size" -ge 2400 ]; then
echo "[process]: $file";
convert "$file" -resize 50% "$file";
fi
if [ "$size" -ge 2000 ] && [ "$size" -lt 2400 ]; then
echo "[process]: $file";
convert "$file" -resize 70% "$file";
fi
done;
cd -
}
function main {
for zip in $(ls *.{zip,rar})
do
local name=$(basename -- "$zip");
local ext="${name##*.}";
local dir="${name%.*}";
if [[ "$ext" = "zip" ]]; then
unzip "$zip" -d "$dir";
fi;
if [[ "$ext" = "rar" ]]; then
unrar e "$zip" "$(pwd)/$dir/";
fi;
convertImages "$dir";
zip -r "$dir-2.zip" "$dir"
rm -r "$dir";
done;
}
main;
IFS='
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment