Created
April 19, 2016 01:14
-
-
Save DocumentAlchemy/018b36667d4e44cff4c29561395f2a44 to your computer and use it in GitHub Desktop.
Shell script that will resizes one or more images to a fixed width using the DocumentAlchemy API
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
#!/bin/bash | |
# Resizes one or more images to a fixed width using the DocumentAlchemy API | |
# USAGE: resize.sh <NEW-WIDTH> <FILES> | |
# | |
# EXAMPLE: resize.sh 180 images/*.png | |
# The generated files will have the same name (and location) as their source | |
# image, but with '-resized' inserted between the filename and the extension. | |
# This is your DocumentAlchemy API key. It may be set via the | |
# environment variable named `DA_API_KEY`. | |
# (See <https://documentalchemy.com/> to get one of your own.) | |
API_KEY=${DA_API_KEY:-"403l1zh3dkbakyb9"} | |
# This is a flag that can prevent this script from `echo`ing | |
# unnecessary information. It can be set via the environment | |
# variable `QUIET`. A clever person could make this into a | |
# command line parameter like `-q`. | |
QUIET=${QUIET:-FALSE} | |
# Note that you can set environment variables on a per-invocation | |
# basis by prefixing NAME=VALUE before the command. For example: | |
# API_KEY=my-key QUIET=true doc2docx *.doc | |
# utility function to show simple help | |
function usage { | |
echo "USE: $0 <SIZE> <IMAGES>"; | |
} | |
# EXIT_CODE tracks the number of IMAGES we couldn't resize. | |
EXIT_CODE=0 | |
# The first argument contains the desired size: | |
NEW_WIDTH=$1 | |
# The rest will be filenames, so "shift" the first argument away and prcess the rest. | |
shift; | |
# Loop over the command line parameters.... | |
for img in "$@"; do | |
# ...testing that is is an accessible file... | |
if ! [ -s "$img" ]; then | |
$QUIET || echo "WARNING: File '$img' was not found and will be ignored." | |
EXIT_CODE=$((EXIT_CODE+1)) | |
else | |
# ...if so, POST to DocumentAlchemy to resize the image... | |
outfile="`echo "${img}" | sed -E 's/\.[a-z]+$/-resized.png/'`"; | |
$QUIET || echo "Resizing '$img' into '`basename "$outfile"`'..."; | |
response=$(curl --silent \ | |
--write-out %{http_code} -H "Authorization: da.key=$API_KEY" \ | |
-X POST --form "document=@$img" \ | |
https://documentalchemy.com/api/v1/document/-/rendition/png/transform/S$NEW_WIDTH,0 \ | |
-o "$outfile") | |
# ...and report success or failure. | |
if ! [ "$response" -eq "200" ]; then | |
$QUIET || echo "WARNING: Expected a 200 response for file '$img', found $response instead."; | |
EXIT_CODE=$((EXIT_CODE+1)) | |
else | |
$QUIET || echo "...OK. File '$outfile' created." | |
fi | |
fi | |
done | |
# Exit with the number of documents that could not be converted. | |
exit $EXIT_CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment