Tips and tricks
How to use tinypng cli to compress all pictures in a directory
First you need to install tinypng-cli and provide an api key
sudo npm install -g tinypng-cli
Use this code to compress in the same directory
If it has been compressed before and has the compressed- prefix, then other files will not get compressed.
Make a file named tinify.command and place it in your directory.
declare -a exclude_array declare -a compress_array echo "----------------" STR="`dirname $0`" cd $STR yourfilenames=`ls .` for eachfile in $yourfilenames do if [[ "$eachfile" == *"compressed-"* ]] then echo $eachfile dup=${eachfile/compressed-} exclude_array+=($eachfile) exclude_array+=($dup) fi done echo "----------------" for eachfile in $yourfilenames do if [[ "$eachfile" == *".png"* ]] || [[ "$eachfile" == *".jpg"* ]] ; then echo $eachfile compress_array+=($eachfile) fi done for target in "${exclude_array[@]}"; do for i in "${!compress_array[@]}"; do if [[ ${compress_array[i]} = $target ]]; then unset 'compress_array[i]' fi done done echo "---------------- [ exclude_array ]" echo "${exclude_array[@]}" echo "----------------" echo "---------------- [ compress_array ]" echo "${compress_array[@]}" echo "----------------" for target in "${compress_array[@]}"; do cp $target temp tinypng $target -k YOUR_API_KEY mv $target compressed-${target} mv temp $target done
How to permanently store api key ?
TinyPNG CLI allows you to provide your API key in two different ways. The more convenient one is to save the API key into a file called
.tinypng
within your home directory. The other way is to provide it as an option while running the CLI.