Skip to content

Instantly share code, notes, and snippets.

@cbertelli
Created July 22, 2018 10:18
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 cbertelli/67b384f4d4841a58ed80f2e891dc5ca9 to your computer and use it in GitHub Desktop.
Save cbertelli/67b384f4d4841a58ed80f2e891dc5ca9 to your computer and use it in GitHub Desktop.
Gets the URL of an image and spits an optimized one in the format you choose
#!/bin/sh
# please configure by adding the username (mandatory)
# go to https://imageoptim.com/api/register to get your username
# username by imageoptim
# put here the username registered automatically by imageoptim
USRNM=""
# function to display usage message and exit
usage()
{
cat << eof
$(basename $0) URL-of-image-to-convert output-filename [quality size]
Quality (high, medium, low) is optional; output takes the best out of the input file.
Size is optional and may be expressed as WIDTH (eg. 600) or WIDTHxHEIGHT.
eof
exit 1
}
errurl()
{
#echo The URL is not a valid http or https URL.
cat << eof
The URL is not a valid http or https URL.
eof
exit 1
}
missinguser()
{
#echo The username issued by ImageOptim is missing. Please get one.
cat << eof
The username issued by ImageOptim is missing. Please get one
visiting https://imageoptim.com/api/register.
See https://imageoptim.com/api for more information.
eof
exit 1
}
# if number args less than 2...
if [ $# -lt 2 ]; then
usage
fi
# if first arg isn't an URL
if [[ ! $1 =~ https?://.*$ ]]; then
errurl
fi
# do not specify quality if parameter is not set
if [ ! -z "$3" ]; then
QUAL=",quality=$3"
fi
# get size from $4
if [ -z "$4" ]; then
SIZE="full"
else
SIZE="$4"
fi
# verify if the imageoptim username is set
if [ -z $USRNM ];
then
missinguser
fi
# set format from output filename
FILENAME1=$2
EXT="${FILENAME1##*.}"
curl -XPOST -o $2 -L "https://im2.io/$USRNM/$SIZE,format=$EXT$QUAL/$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment