April 08, 2004

Photojournal script

I was working on a script that aids in creating photojournal webpages.  I imagine there are plenty out there already, but I was in the mood to write it, and I learned a thing or two.  It is simple and allows a list of image filenames (one per line) to be passed to it as input.  Thumbnails are made from these image files if necessary.  There are some limitations on the filenames:  they cannot be of the form <H1> or <H2> ... <H9> and they cannot start with a pound sign (#).  The pound sign indicates that the line is a comment.  The <H?> means that it is a heading, so by following the <H?> by a tab and then a string, you can create an html heading of the desired level.  The image filenames may be also followed by a tab and a string, which will be used to describe the thumbnails.  The size of the thumbnails may be specified by using another tab character followed by a string, such as "20%" or "x100" or "200x300!".   See the convert command for details.  Additionally, the images are titled with their filename, size  in KB, resolution, and date of creation.

#!/bin/awk -f

# makethumbs
# Copyright (c) 2004 Sean Eron Anderson

# Take images filenames input and create thumbnails and output html to show them.
# Stdout will be a HTML table with the thumbnails and links to the full images.
# Each input line will have the image filename followed by an optional
# description, followed by an optional thumbnail size.

# Lines that start with # are comments
# Lines that start with <H followed by a digit 1-9 followed by > are headings, so
# So:
# <H2>\tExample Title 

# Simple example:  Go to a directory with images and type:
# ls -1 [^_]*.JPG | makethumbs >index.html

BEGIN {
  FS = "\t";
  intable = 0;
  }

/<[Hh][1-9]>\t(.*)/ {
  if (intable) {
    print "</table>";
    }
  match($1, /<(.*)>/, a);
  printf("<%s>%s</%s>\n\n", a[1], $2, a[1]);
  if (intable) {
    print "<table>"
    }
  }

!/^<[Hh][1-9]>\t/ && !/^[#]/ && /.+/ {
  if (!intable) {
    printf("<table>\n");
    intable = 1;
    }
  size = ($3 == "") ? "x100" : $3;
  resizecmd = "test _" $1 " -nt " $1 " || convert -size " size " " $1 " -resize x100 +profile '*' _" $1;
  system(resizecmd);

  geomcmd = "identify -size 1x1 -verbose "$1" | grep 'Base geom' | sed 's/  Base geometry: //'";
  geomcmd | getline geom;

  sizecmd = "identify -size 1x1 -verbose "$1" | grep 'Filesize' | sed 's/  Filesize: //'";
  sizecmd | getline size;

  datecmd = "date -r " $1;
  datecmd | getline date;

  info = $1 "  " size "  " geom "  " date;
  printf("  <tr>\n    <td><a href='%s'><img src='_%s' alt='' title='%s'></a></td>\n", 
    $1, $1, info);
  printf("    <td>%s</td>\n  </tr>\n", $2);
  }

END {
  if (intable) {
    printf("</table>\n");
    }
  }
Posted by seander at April 8, 2004 07:36 PM
Comments