Work the Shell - Displaying Image Directories in Apache, Part III

The incredible shrinking script knows a better way to resize your thumbnails.

where I'll set size to the desired thumbnail size—75 in our example script.

I'm also going to add a few counters so we can summarize images displayed versus total files displayed at the end. Just because it's, uh, interesting, right?

Here's the latest version of the loop, and as you might expect, it's getting more complicated as it becomes more sophisticated:

for name in *
  if [ ! -z "$(file -b $name|grep 'image data')" ]
    imgcount=$(( $imgcount + 1 ))
    figuresize $name
    if [ ! -z "$height" ] ; then
      if [ $height -gt $width ] ; then
      echo "<a href=$name><img src=$name border=0"
      echo "alt=$name $dimensionlabel=$size" 
      echo "align="absmiddle" />"
      echo "$name ($height x $width)</a>"
      echo "<a href=$name><img src=$name border=0"
      echo "alt=$name height=$size" 
      echo "align="absmiddle" />"
      echo "$name</a>"
    echo "<a href=$name>$name</a><br />"
  echo "<hr />"
  totcount=$(( $totcount + 1 ))

echo "<i>Displayed $imgcount images out of 
$totcount entries total.</i>"

The resultant output, which is hopefully more attractive, is shown in Figure 2.

Figure 2. More Attractive Output

Now that we can normalize these thumbnails in the script (at least for non-JPEG images, due to a limitation in the file command), the next thing to examine is how to display the results with multiple images across, in a grid or table, rather than one per line as we see now. That's a bit more complicated, because it involves yet another counter, but while you're waiting for your next issue of Linux Journal, you might bone up on the basic HTML table tags, because that's what we'll be using. Then, finally, we'll switch to ImageMagick from file, so we can get the dimensions of all image files, not only GIF and PNG files.

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, and he also offers up tech support at


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

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