Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
OS X script for resizing and uploading images
#!/bin/sh
# A script for resizing images (using OS X's sips), uploading to a server
# and pasting HTML code to your (OS X) clipboard.
# If you haven't guessed, it requires OS X.
# more info:
# http://jon.smajda.com/blog/2008/04/12/image-upload-script/
set -e
####### set all your variables
imagedir="/tmp" # which local dir should images be saved to?
webhost="something.com" # domain/ip of your web server
webuser="yourusername" # your username on your web server
uploadpath="/home/yourusername/public_html/wp-content/uploads" # upload path on server
uploadurl="http://something.com/wp-content/uploads" # url to view your uploads at
maxwidth="500" # in pixels, the widest image you want to allow.
###### show help if no arguments are given
if [ $# = 0 ]; then
echo "This script will:"
echo " 1. Download an image to work with (if you give it a url for an image),"
echo " or make a copy of a local image (if you give it a local image)."
echo " Images will be saved locally in $imagedir."
echo " 2. Resize the image (if it's too big)."
echo " The current default max width is $maxwidth""px."
echo " Change this with '-w NUM'."
echo " 3. Upload it to your web server."
echo " 4. Copy the <img> code to your clipboard.";
echo "Uses the 'sips' and 'pbcopy' commands in Mac OS X.";
echo "Adjust the variables in lines 4-9 to fit your needs.";
echo "Usage: `basename $0` [-w NUM] [imagefile]";
exit;
fi
#### grab new maxwidth if -w flag is used
while [ "${1:0:1}" = "-" ]; do
case "$1" in
'-w')
maxwidth=$2
echo "Maximum width changed to $maxwidth"
;;
esac
shift
shift
done
##### if the image is at a url, download the image to $imagedir, cd there and work from there
imagefile="$@"
if [ `echo "$imagefile" | grep ^"http"` ]; then
imgurl="$imagefile"
cd $imagedir
echo "Remote image requested. Starting download..."
curl -f -O $imgurl
imagefile=`echo "$imgurl" | awk -F "/" '{print $NF}'`
if [ `ls $imagefile` ]; then
echo "Image downloaded to $imagedir/$imagefile:"
echo "`ls -lh $imagedir/$imagefile`"
fi
##### if the image is a local file, copy the image to $imagedir, cd there and work from there
else
cp "$imagefile" "$imagedir"/
echo "Image copied to "$imagedir"/"$imagefile":"
cd "$imagedir"
echo "`ls -lh "$imagedir"/"$imagefile"`"
fi
###### check the file exists and resize the image if necessary
if [ -f "$imagefile" ]; then
imgwidth=`sips --getProperty pixelWidth "$imagefile" | awk '/pixelWidth/ {print $2}'`
imgheight=`sips --getProperty pixelHeight "$imagefile" | awk '/pixelHeight/ {print $2}'`
echo "Size of "$imagefile": $imgwidth""px wide by $imgheight""px tall.";
else
echo "Oops, "$imagefile" does not exist."
exit
fi
if [ $imgwidth -gt $maxwidth ]; then
echo " - Image too big. Resizing..."
sips --resampleWidth $maxwidth "$imagefile" > /dev/null 2>&1 # to hide sips' ugly output
imgwidth=`sips --getProperty pixelWidth "$imagefile" | awk '/pixelWidth/ {print $2}'`
imgheight=`sips --getProperty pixelHeight "$imagefile" | awk '/pixelHeight/ {print $2}'`
echo " - Resized "$imagefile" to $imgwidth""px wide by $imgheight""px tall";
fi
###### ask to rename
echo "The file is named \"$imagefile\"."
echo " - To rename "$imagefile", type the new name now (including the extension)."
echo " - To keep the current name, just hit return.";
read newfilename
if [ -z "$newfilename" ]; then
echo " - Ok, file is still named "$imagefile"."
else
echo " - Ok, renaming image to "$newfilename"..."
mv "$imagefile" "$newfilename";
imagefile="$newfilename";
fi
####### send to the web server
echo "Sending to web server..."
scp "$imagefile" $webuser@$webhost:$uploadpath/
###### add the alt text & create the url
echo "Describe the image (for ALT and TITLE):"
read alttext
while [ -z "$alttext" ]
do
echo "You really should have alt text. Try again:";
read "alttext"
done
imgurl="<img src=\"$uploadurl/"$imagefile"\" height=\"$imgheight""px\" width=\"$imgwidth""px\" alt=\"$alttext\" title=\"$alttext\" />"
###### copy to the clipboard
echo -n $imgurl | pbcopy
echo "<img> code has been copied to your clipboard.";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment