Work the Shell - Resizing Images, Sort Of

 in
What are shell script programmers to do when they finds themselves constantly scaling image dimensions for their blogs? Write a script to automate the process, of course!
Making It Work as a Script

Here's the basic script at this point:

filename="edit.png"
multiplier="0.75"

width="$(file $filename | cut -f5 -d\  )"
height="$(file $filename | cut -f7 -d\  | sed 's/,//')"

width="$(echo "$width * $multiplier" | bc | cut -d. -f1)"
height="$(echo "$height * $multiplier" | bc | cut -d. -f1)"

echo "$filename scaled: width=$width height=$height"

Testing it with the filename specified produces the following:

$ sh scale-image.sh
edit.png scaled: width=541 height=539

That's not really exactly what I want, however. First, I want to be able to specify the filename and multiplier on the command line. Second, the output needs a slight tweak to be more useful—the values need to be surrounded by quotation marks.

Here's what I'd like to see:

$ sh scale-image.sh 0.75 edit.png
edit.png: width="541" height="539"
$

That's not too hard to accomplish given the basic script we already have. See if you can do it yourself.

Tip: I actually use a “for name; do; done” loop to step through the file scaling, so I can specify a group of images and calculate them all en masse. Try it, coupled with the shift command, to remove the multiplier value once it's saved into a named variable.

Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com. Follow him on Twitter if you'd like: twitter.com/DaveTaylor.

______________________

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState