Image Upload Script

The last few times I’ve put images in my blog posts, I’ve been annoyed at what a pain it is to download the image (if it’s online), resize the image, upload the image, write out the img code, etc. So I decided to write a shell script. In typical Me fashion, it started as a short five line script, but I kept thinking, “But wouldn’t it be cool if…”, so it grew quite a bit. You can get the script here. It uses some built-in Mac OS X commands (sips for image manipulation and pbcopy to copy stuff to your clipboard), so it’ll only work in OS X. You’ll also need ssh access to your web server.

I called it “uploadimg,” but you can rename it to whatever you want. The simplest use is to just give it an image and it will a) make a copy of it to a specified working directory, b) resize the copy if it’s larger than the maxwidth value (set at the top of the script), b) upload the image to your web server’s upload directory, and c) copy the HTML code for the image to your clipboard (The script even asks you for an ALT and TITLE value). So, for example, I took a picture in Photo Booth (“Photo 1.jpg”) and ran:

uploadimg “Photo 1.jpg”

The result:

The Two-Headed Chloe Monster

So here are a few of the “But wouldn’t it be cool if’s” I added:

So after uploading that last image, I ran this command:

uploadimg -w 200 /static/files/chloe-photobooth.jpg

Which downloaded the 500px image, resized it to 200px, asked me to rename it, uploaded it to the server, and copied the HTML code for this to my clipboard:

A Slightly Smaller Two-Headed Chloe Monster

And if you just type uploadimg with no arguments, you get some help text describing how to use the script.