Last active
May 13, 2024 06:06
-
-
Save vasylenko/13cb423aa83265e79ac5ad900195603f to your computer and use it in GitHub Desktop.
TinyPNG
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
# 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 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
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?