#!/usr/local/bin/perl -w # $Id: fotokey,v 1.16 2003/09/27 07:57:06 pme Exp pme $ # fotokey software makes it easy to publish web photo galleries. # cf http://fotokey.org # # Copyright 2003 by Paul English # # This file is part of fotokey. # # fotokey is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # fotokey is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with fotokey; if not, write to the Free Software Foundation, # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA use strict; use File::Copy; my $furl = "http://fotokey.org"; my $fdir = "/usr/local/fotokey"; # See sub main() below. my $debug = 0; sub doit($) { my $command = shift; if ($debug) { print "doit $command\n"; } return system($command); } # ----------------------------------------------------------------- # the "o_" functions output different parts of the generated files. sub o_navigation($$$$) { # output the text navigation at top left of vga my ($n,$fileno,$w,$h) = @_; print "\n\n" . "\n\n\n" . "\n"; o_buttons($n,$w,$h); print "\n" . "\n\n"; } sub o_goto($$) { # output html/js to go to url w/o messing browser history my ($url,$text) = @_; print "" . $text; } sub o_keymap($$) { # output keymap nav for this vga; see next/prev below my ($n,$fileno) = @_; print "\n"; } sub o_buttonSetup() { # preloads the top right button images print "\n"; } sub o_buttons($$$) { # output the buttons on top right of vga my ($n,$w,$h) = @_; my $url = current_url(); my $ns = sprintf("%03d",$n); # 001 etc my $f = fotofile($n); # 001.jpg etc print < Email this photo. View camera settings for this photo. Info about fotokey galleries. EOF } # ----------------------------------------------------------------- # tiny utility functions: sub fotofile { # filename such as 001.jpg or 002.html my $suffix = (@_ == 2) ? $_[1] : "jpg"; return sprintf("%03d.%s",$_[0],$suffix); # bug: use File::Spec } sub o_updated($) { # output the updated time stamp at end of html my ($file_handle) = @_; print $file_handle "

Updated " . localtime() . "\n by fotokey\n"; } sub o_start($) { # output the start of the html file my ($file_handle) = @_; my $gen = ""; print $file_handle < $gen EOF } sub current_url() { # i'm sure there's an easier way to do this my $dir = $ENV{'PWD'}; if ($dir =~ m'^/v/') { # /v/domain.com/www/foo.html $dir =~ s (/www/) (/) ; $dir =~ s (^/v/) (http://www.); } else { # /u/paul/domain.com/www/foo.html $dir =~ s (/www/) (/) ; $dir =~ s (/u/\w+/) (http://www.) ; } return $dir; } sub get_dimensions($) { my $info = `pnmfile $_[0]`; my @words = split(" ",$info); if (@words == 8) { return ($words[3], $words[5]); } else { return (0, 0); } } # ----------------------------------------------------------------- sub setup_lib() { opendir (DH, "$fdir/lib") or die "Cannot open $fdir/lib: $!"; my ($fname, $file); foreach $fname (readdir DH) { my $file = "$fdir/lib/$fname"; if (! -d $file) { copy ($file,"lib/$fname") or die "Cannot copy $file to lib directory: $!"; if (-x $file) { # for email.cgi etc chmod (0555, "lib/$fname") or die "Cannot chmod +x lib/$fname: $!"; } } } closedir DH; } # ----------------------------------------------------------------- # main() sub main() { ( -d $fdir ) or die ("\$fdir $fdir does not exist\nstopped"); my $url = current_url(); opendir DIR, '.' or die "Could not read `.': $!"; my @photos; while (defined (my $f = readdir DIR)) { $f =~ /^\./ and next; # Skip dot files. $f =~ /\.jpe?g$/i or next; # Allow .jpg, .JPG, .jpeg, etc. push @photos, $f; } closedir DIR; @photos or die ("no jpg files in `.', stopped"); my ($file, $dir); foreach $dir (qw/lib thumbs vga huge/) { unless ( -d $dir ) { mkdir ($dir, 0755) or die ("Can't mkdir $dir: $!"); } } setup_lib(); # rename files so we can have prev/next buttons in the vga html files my $fileno = 0; foreach $file (sort @photos) { my $huge = "huge/" . fotofile(++$fileno); rename ($file, $huge) or die ("Can't move $file to $huge: $!"); } print "Processing $fileno photos for $url...\n"; my $n; # photo index if ( -f "index.html" ) { rename ("index.html", "index.save.html") or die ("Can't rename index.html: $!"); } open (INDEX, ">index.html") or die ("Can't create a new index.html: $!"); o_start(*INDEX); print (INDEX "\n\n"); if (-e "header.html") { print (INDEX "\n"); } # ----------------------------------------------------------------- foreach $n (1 .. $fileno) { # process each photo $file = fotofile($n); # 001.jpg etc my $ns = sprintf("%03d",$n); # just the 001 part print $file; # status to command line # create the thumbnail and vga sized photo !doit ("djpeg -pnm huge/$file > tmp") or die ("djpeg huge/$file, stopped"); my ($w,$h) = get_dimensions("tmp"); print " $w x $h $ns "; ### my $vga_html = "vga/$ns.html"; print INDEX "\"Click\n"; # " onmouseover=\"this.border=1;\" onmouseout=\"this.border=0;\"\n" . print ": "; unless (-e "thumbs/$file") { !doit ("pnmscale -xysize 300 300 < tmp | ppmtojpeg > thumbs/$file") or die ("pnmscale|ppmtojpeg for thumbs/$file failed, stopped"); print "thumbs "; } unless (-e "vga/$file") { !doit ("pnmscale -xysize 800 600 < tmp | ppmtojpeg > vga/$file") or die ("pnmscale|ppmtojpeg for vga/$file failed, stopped"); print "vga "; } unlink "tmp"; # create the vga wrapper html open (VGA, ">$vga_html") or die ("can't open >$vga_html: $!"); select (VGA); o_start(*VGA); print "\n"; o_keymap($n,$fileno); o_buttonSetup(); print "\n\n"; print "\n" . "\n"; o_navigation($n,$fileno,$w,$h); print "\n"; print "\n" . "\n\n"; print< EOF my @exif = `jhead huge/$file`; print "
\n"; if (@exif>0) { print "
\n";
      my $line;
      foreach $line (@exif) {
	print "$line";
      }
      print "
\n"; } else { print "No exif information available for this photo.\n"; } print "
\ntitle\n" . "($n of $fileno)
\n" . "
\n" . "
\n" . "
\n"; print<

Rating: EOF o_updated(*VGA); if ($n<$fileno) { # start download of next image... my $next = fotofile($n+1); print "\"[cache]\"\n"; } print VGA "\n"; close (VGA) or die ("$vga_html: write failed: $!"); select (STDOUT); print "\n"; } # foreach photo if (-e "footer.html") { print (INDEX "\n"); } o_updated(*INDEX); print INDEX "\"[cache]\"\n"; print INDEX "\n\n"; close (INDEX) or die ("index.html: write failed: $!"); print "Completed " . current_url() . "\n"; return 0; } exit(&main()); # ----------------------------------------------------------------- ### Local Variables: *** ### compile-command:"cd /v/fotokey.org/www/sample2;fotokey.reset;fotokey" *** ### End: ***