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.

{"type":"main_options","images_arr":"'#ffffff'","enable_ajax":"off","soundcloud_apikey":"","bg_isparallax":"off","bg_slideshow_time":"0","bg_transition":"slidedown","site_url":"https:\/\/digitalzoomstudio.net","theme_url":"https:\/\/digitalzoomstudio.net\/wp-content\/themes\/qucreative\/","blur_ammount":"26","width_column":"50","width_section_bg":"","width_gap":"30","border_width":"0","border_color":"#ffffff","translate_cancel_comment":"Cancel reply","translate_leave_a_comment":"Leave a comment","translate_leave_a_comment_to":"Leave a comment to","is_customize_preview":"off","width_blur_margin":"30","gallery_w_thumbs_autoplay_videos":"off","enable_native_scrollbar":"on","content_enviroment_opacity":"30","menu_enviroment_opacity":"70"}
{"type":"darkfull"}