Skip to content

Instantly share code, notes, and snippets.

@swichers
Created September 26, 2014 01:17
Show Gist options
  • Save swichers/79d6c2d6d402dc6b1ef7 to your computer and use it in GitHub Desktop.
Save swichers/79d6c2d6d402dc6b1ef7 to your computer and use it in GitHub Desktop.
Scan directory for large images and size them down
# Command to run on a bash shell
# Adjust as necessary to increase/decrease size of files matched, extensions, exclusions, etc.
# Will find images larger than 500k and not in the styles directory.
find . -regex ".*\.\(jpg\|gif\|png\|JPG\|PNG\|jpeg\)" -type f -size +500k -not -path "./styles/*" -exec image-resize.sh "{}" \;
#!/usr/bin/env bash
# Place in /usr/local/bin or wherever desired.
MAX_WIDTH=2560
MAX_HEIGHT=1600
IMAGE_NAME=$1
IMAGE_IDENTIFY=`identify "${IMAGE_NAME}"`
IMAGE_WIDTH=`echo "${IMAGE_IDENTIFY}" | ack ' ([0-9]+)x([0-9]+) ' -o | cut -dx -f1`
IMAGE_HEIGHT=`echo "${IMAGE_IDENTIFY}" | ack ' ([0-9]+)x([0-9]+) ' -o | cut -dx -f2`
if [ "${MAX_WIDTH}" -lt "${IMAGE_WIDTH}" ] || [ "${MAX_HEIGHT}" -lt "${IMAGE_HEIGHT}" ] ; then
echo Resizing "${IMAGE_NAME}" from ${IMAGE_WIDTH}x${IMAGE_HEIGHT} to ${MAX_WIDTH}x${MAX_HEIGHT}
convert "${IMAGE_NAME}" -resize ${MAX_WIDTH}x${MAX_HEIGHT}\> -strip "${IMAGE_NAME}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment