Work the Shell - Displaying Image Directories in Apache, Part IV
This is the fourth of four columns on how to write a shell script to make the display of directories full of images more useful than the default Apache ls -l style output.
In the first column, I explained how to drop a script in place to improve the Apache directory listing capability, and in the latter two columns, I showed how to work with images within a shell script, including a shell function that extracted height and width from most image file types.
I ended that column with a teaser, highlighting that if you really want to work with images on the command line, there's no better package than ImageMagick. You'll want it for this month's installment (www.imagemagick.org), if it's not already installed on your server.
The first stab at the image size function figuresize() leaned on the file command to figure out image size. This works for GIF and PNG images, but it turns out that the file command can't figure out the image size for JPEG images, alas. So, we need to rewrite it using the ImageMagick identify script instead. Here's a sample (pruned) output:
$ identify teamgeist.jpg hentai-manga-example.gif archos-av700.png teamgeist.jpg JPEG 350x350 350x350+0+0 DirectClass 8-bit 62.7734kb hentai-manga-example.gif GIF 358x313 358x313+0+0 PseudoClass 256c ↪8-bit 86.4551kb archos-av700.png PNG 567x294 567x294+0+0 DirectClass ↪8-bit 341.498kb
Notice that in all three cases, the image dimensions are shown as field three, in width x height format (for example, archos-av700.png is 567 pixels wide and 294 pixels high).
This means we can use cut to grab only those values and cut again to strip out field one and field two, like this:
width="$(identify $1|cut -d\ -f3|cut -dx -f1)" height="$(identify $1|cut -d\ -f3|cut -dx -f2)"
If we add an echo, we have a rudimentary image size shell script. With that done, let's test it out with the Archos PNG file and the Teamgeist image that the file command couldn't handle:
$ sh myidentify.sh archos-av700.png archos-av700.png: height=294 and width=567 $ sh myidentify.sh teamgeist.jpg teamgeist.jpg: height=350 and width=350
Perfect. The figuresize() shell function is given an image filename and sets the global variables height and width, so it's easy to rewrite it to work with identify:
figuresize()
{
width="$(identify $1|cut -d\ -f3|cut -dx -f1)"
height="$(identify $1|cut -d\ -f3|cut -dx -f2)"
}
This is much smaller, much more efficient, and it works with JPEG images too—an all-around win!
The last step in our script development is to let more than one image be displayed on a line, because we now can reduce thumbnails as needed, whether they're wide or tall. Here, I write this to have three images abreast, but you can tweak it if you have a bigger screen, of course.
To have three images across in a window that'll be no wider than 700 pixels (to fit easily on an 800x600 screen), we want the thumbnail images to be no wider than 200 pixels. This means we want to call figuresize(), and then do some math to figure out the best reduced dimensions to get to that max.
The challenge is that the shell doesn't really let you work with floating-point (non-integer) numbers, so we need to trick bc into doing the work for us. Here's how that looks if height is the larger dimension:
factor="$(echo "scale=4;$maxsize/$height"|bc)" newwidth="$(echo "$factor*$width"|bc|cut -d. -f1)"
To figure out how to scale the smaller dimension proportionally, we divide MAXSIZE/actual height, which will be a value less than 1.0, and then use that as the multiplier for the other dimension.
For example, let's say I have an image that's 313x358 but want to reduce it to no bigger than 200x200, proportionally; factor can be calculated as 200/358 (or .558), and then the smaller dimension is multiplied by .558 (that is, 313*0.558) to produce 174. The proportionally scaled image, then, is 174x200.
In script form, here's what I wrote:
if [ $height -gt $maxsize -o $width -gt $maxsize ] ; then
if [ $height -gt $width ] ; then
# we'll want to constrain height
factor="$(echo "scale=4;$maxsize/$height"|bc)"
nh=$maxsize
nw="$(echo "$factor*$width"|bc|cut -d. -f1)"
else
factor="$(echo "scale=4;$maxsize/$width"|bc)"
nw=$maxsize
nh="$(echo "$factor*$height"|bc|cut -d. -f1)"
fi
echo "Given $width x $height, scaled to "
echo "$nw x $nh"
width=$nw
height=$nh
fi
Cool. Now if the image is too big, we can scale it automatically and adjust the height and width parameters as needed. If it's sufficiently small, nothing changes. A test run:
Given 161x230, scaled to 139x200.
Given 268x202, scaled to 200x150.
Given 567x294, scaled to 200x103.
Given 358x313, scaled to 200x174.
Given 350x350, scaled to 200x199.
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Nice article, thanks for the
1 hour 4 min ago - I once had a better way I
6 hours 50 min ago - Not only you I too assumed
7 hours 8 min ago - another very interesting
9 hours 1 min ago - Reply to comment | Linux Journal
10 hours 54 min ago - Reply to comment | Linux Journal
17 hours 48 min ago - Reply to comment | Linux Journal
18 hours 4 min ago - Favorite (and easily brute-forced) pw's
19 hours 56 min ago - Have you tried Boxen? It's a
1 day 1 hour ago - seo services in india
1 day 6 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
finally
i agree, finally this series is over.
hey dave, on a different note, your example picture kind of shows what kind of stuff you're into, huh?
"hentai-manga-example.gif"?
http://en.wikipedia.org/wiki/Hentai
"[I]n slang situations it often means 'perverted' and is subsequently used in many other countries to refer to anime, manga and computer games with explicit sexual or pornographic content.
"The term "hentai" is also commonly used (outside of Japan) to refer to pornographic animation in general that is not necessarily anime or manga. This is most often the case if the said animation is an imitation of a pre-existing cartoon or character."
finally
finally this series is over...