Skip to content

Instantly share code, notes, and snippets.

@vasylenko
Last active April 16, 2023 00:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasylenko/13cb423aa83265e79ac5ad900195603f to your computer and use it in GitHub Desktop.
Save vasylenko/13cb423aa83265e79ac5ad900195603f to your computer and use it in GitHub Desktop.
TinyPNG
# MacOS Automator Quick Action for image compression using Tinypng.com
# You need to register to obtain a personal API key
# https://tinypng.com/developers
#
# The code below should be put inside "Run Shell Script" Action with the following configuration in the Automator:
# workflow receives current: files or folders | in Finder
# shell: /bin/zsh
# pass input: as arguments
set -e -o pipefail
export PATH="$PATH:/usr/local/bin"
APIKEY=YOUR_API_KEY_HERE
tinypng () {
file_name="$1:t:r"
file_ext="$1:t:e"
file_dir="$1:h"
compressed_url="$(curl -D - -o /dev/null --user api:$APIKEY --data-binary @"$1" https://api.tinify.com/shrink|grep location|cut -d ' ' -f 2|sed 's/\r//')"
curl -o "${file_dir}/${file_name}_compressed.${file_ext}" "$compressed_url"
}
for f in "$@"
do
if [ -f "$f" ]; then
tinypng "$f"
elif [ -d "$f" ]; then
find "$f" -name "*.png" -o -name "*.jpeg" -o -name "*.jpg" | while read file_name; do
tinypng "$file_name"; done
fi
done
@oysteinhusby
Copy link

Hi!

I would like to use this script but also resize images to max height or width 2000. I'm OK having two separate automations to solve this.

Can you please help?

@vasylenko
Copy link
Author

@oysteinhusby hi there! ✋

TinyPNG does not support image resizing, but here is a blog post where I describe the approach on how to use a built-in resize tool in macOS: Image Resize with Monterey Shortcuts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment