#!/usr/bin/perl use strict; use lib "/home/jason/lib"; use lib "/home/jason/public_html/lib"; use web::file; use web::error; use web::targets; use getopts ""; my $OUTPUT_FILE = $ARGV[0]; my $QUERIER = "jpegsize.prl"; my $GALLERIES = "/home/jason/public_html/alum/pictures/*/*.jtml"; my $IMAGE_ROOT = "/home/jason/public_html/born/images"; my @FILES; my %DATA; # --------------------------------------------- # Get file date in seconds since (sometime). # --------------------------------------------- sub file_date { my $file = shift; return $^T - (-M $file) * 24*3600; } # ---------------------------- # Gather list of images. # ---------------------------- my $targets = target_list(); foreach (sort keys %$targets) { next if !s(^born/images/)() || !/\.(jpg|gif|wav|mp3)$/; push @FILES, $_; } # ---------------------------- # Read old file. # ---------------------------- open(my $fh, "<$OUTPUT_FILE"); while (<$fh>) { next if /^\s*(#|$)/; my ($file, $date, $w, $h, $alt) = /^(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S.*\S|$)/; $DATA{$file} = [$date, $w, $h, $alt] if $file !~ /^200[567]\d\d\d\d/; } close($fh); # -------------------------------------- # Query the size of all those images. # -------------------------------------- open(my $fh, ">.tmp") or fatal("Can't create temp file \".tmp\": $!"); foreach my $file (@FILES) { next if $file !~ /\.(jpg|gif)$/; my $full_name = "$IMAGE_ROOT/$file"; my $date = file_date($full_name); print $fh "$full_name\n" if $date > $DATA{$file}[0]; $DATA{$file}[0] = $date; } close($fh); my $command = "$QUERIER -f .tmp"; open(my $fh, "$command |") or fatal("Can't run $command: $!"); while (<$fh>) { my ($file, $w, $h) = /^(\S+.\w+): (\d+) (\d+)/; if ($file) { $file =~ s/^$IMAGE_ROOT.//; $DATA{$file}[1] = $w; $DATA{$file}[2] = $h; } else { error("Bad image: $_"); } } close($fh); unlink ".tmp"; # ----------------------------------------------------- # Go through picture galleries looking for captions. # ----------------------------------------------------- my $in_layout = 0; foreach my $gallery (glob $GALLERIES) { next if $gallery =~ /2.jtml$/; open(my $fh, "<$gallery"); my ($dir) = $gallery =~ m(pictures/(.*)/[^/]*.\w+$); my $gallery_error; $gallery =~ s/.*pictures\///; while (<$fh>) { if (!$in_layout && /<< layout_images/) { $in_layout = 1; } elsif ($in_layout && />>/) { $in_layout = 0; } elsif ($in_layout && /\[\s*(\d+)\s*,\s*(\d+)\s*,\s*"([^"]+)"(\s*,\s*(".*"|'.*'))?/) { my ($w, $h, $file, $alt) = ($1, $2, $3, $5); $file = "$dir/$file" if $file !~ m(/); $alt =~ s/^'(.*)'$/\1/ or $alt =~ s/^"(.*)"$/\1/; my $rec = $DATA{$file}; next if !$rec || $alt eq "" || $rec->[3] =~ /^[^\[]/; $rec->[3] = "[$alt]"; } } close($fh); } # ----------------------------- # Go through captions files. # ----------------------------- foreach my $cap_file (glob "*/captions.txt") { my ($dir) = $cap_file =~ m(^(.*?)/); open(my $fh, "<$cap_file"); while (<$fh>) { next if !/^(\w+)!?\s+(\S(.*\S)?)/; my ($name, $alt) = ($1, $2); my $file = "$dir/$name"; $file = -f "$file.jpg" ? "$file.jpg" : -f "$file.gif" ? "$file.gif" : -f "$file.mp3" ? "$file.mp3" : -f "$file.wav" ? "$file.wav" : error("Can't find extension for image $file.xxx\n"); my $rec = $DATA{$file}; next if !$rec || $rec->[3] =~ /^[^\[]/; $rec->[3] = "[$alt]"; } close($fh); } # ---------------------------- # Write new file. # ---------------------------- writing($OUTPUT_FILE); open(my $fh, ">$OUTPUT_FILE"); print $fh "# vim: noet ts=60\n"; foreach my $file (sort keys %DATA) { my $rec = $DATA{$file}; printf $fh "%s\t%d %4d %4d %s\n", $file, @$rec; } # Add these old flower photos to it, even though they no longer live locally. foreach my $file (glob "images.[0-9][0-9][0-9][0-9]") { open(my $fh2, "<$file"); while (<$fh2>) { print $fh $_; } close($fh2); } close($fh); exit 0; ################################################################################ __END__ =head USAGE make_image_table.prl =head DESCRIPTION Gets the size of all the pictures in the subdirectories and writes out an ASCII table with a full list of images and sizes: F. This is used by for error-checking, and it is used by the picture viewer F<../cgi-bin/picture.cgi>. This needs to be built before anything else or you will get thousands of warnings about missing image files! Note: all images must be ".jpg" or ".gif" at the moment, sorry. =cut