CLI Tips: Working with Images

Of all the tools I’ve added to my #webdev toolbelt during the last five years, gaining skill with the command line interface (CLI) is at the top of my list. With the CLI, I work quicker than using a mouse and a GUI, and there are a vast quantity of freely available solutions to just about any problem. Here are two image related/disk space issues I’ve dealt with lately, and the CLI solutions I used to fix them:

Problem: The user is uploading too many large images

One of my clients likes to regularly upload full resolution images from her DSLR camera. After a couple of months, this user was using most of the disk space on the shared server where I’m hosting her. Further analysis showed that her images were not optimized. She was embedding her images at much lower resolutions than their full-size sources. I needed a way to quickly optimize all those full-resolution images.

Solution: Utilize `mogrify` to resize and optimize the images

Image Magick is a software which is often pre-installed on most servers. It has all sorts of commands for manipulating graphics files. In this case, `mogrify` is the command I needed. I was able to use the following script to find JPG images of a certain dimension, resize them to 1200x1200px, and save them out at 55% quality:

https://gist.github.com/mwender/8c59587b409453ca71e7

Running `mogrify` yielded a 70% reduction in this user’s disk space usage without sacrificing image quality.

Problem: I need to quickly move many gigabytes of images to a CDN

My client Case Antiques runs several large auctions each year. For each auction, they’ll typically list six-hundred or more items with each item having anywhere from three to twenty photos. All of those photos for each auction take up a couple gigabytes of disk space. To save on disk space and reduce the load on the server, we serve those images via a Content Delivery Network (CDN) I’ve setup inside Rackspace Cloud Files. However, while the CDN solves a couple of problems, actually getting all those images to the CDN is another one.

Solution: Quickly transfer gigabytes of data to Rackspace Cloud Files with Turbolift

The solution to the above problem is a python script called Turbolift. With a simple command line call, I can upload several gigabytes of images in a matter of minutes.

Previously I was using cloud based web apps like mover.io to move my files. Transfers took anywhere from three to twelve hours not to mention I had to pay a subscription fee to use the service. Now with a routine command line instruction, I’ve been able to make this routine maintenance a five-minute exercise rather than a drawn out task dependent on a third-party file moving service.

Learn how to use the command line

Start down your own path of learning how to use the command line with this course at Codecademy.