#! /usr/bin/perl -w #----------------------------------------------------------- # creates a web photo album from all *.jpg and files in current dir # creates a directory called pages containing the pages for each individual image # creates a file called index.html (the navigation page) # creates a dir called fullsize containing the images # creates a dir called thumbnails containing thumbnail images # you must have nconvert installed. Get from # http://www.xnview.com/ # # you must have lynx installed. Get from # http://www.lynx.org/ # # you must have which and ls installed. Get from # http://www.gnu.org/ #----------------------------------------------------------- #----------------------------------------------------------- # to make getting the basename of the dir easy use File::Basename; use Cwd; $command = basename ($0); # args? none if ($#ARGV == -1) { print "Usage $command <-o | n>\n"; print " (-o = overwrite; -n = no overwrite)\n"; exit; } #----------------------------------------------------------- # check for lynx and nconvert if ( "$^O" eq "MSWin32" ) { $lynxexists = system "which lynx > NUL"; if ( "$lynxexists" eq 2048 ) { print "lynx does not appear to be installed! www.lynx.org"; exit; } } else { $lynxexists = system "which lynx > /dev/null"; if ( "$lynxexists" eq 0 ) { print "lynx does not appear to be installed! www.lynx.org"; exit; } } if ( "$^O" eq "MSWin32" ) { $nconvertexists = system "which nconvert > NUL"; if ( "$nconvertexists" eq 2048 ) { print "nconvert does not appear to be installed! www.xnview.com"; exit; } } else { $nconvertexists = system "which nconvert > /dev/null"; if ( "$nconvertexists" eq 0 ) { print "nconvert does not appear to be installed! www.xnview.com"; exit; } } if ( "$^O" eq "MSWin32" ) { $lsexists = system "which ls > NUL"; if ( "$lsexists" eq 2048 ) { print "ls does not appear to be installed! www.gnu.org"; exit; } } else { $lsexists = system "which nconvert > /dev/null"; if ( "$lsexists" eq 0 ) { print "ls does not appear to be installed! www.gnu.org"; exit; } } #----------------------------------------------------------- # source dir? $srcdir = getcwd(); ($base, $path, $type) = fileparse("$srcdir", qr{\..*}); $dirname = $base; # convert backslashes to forward slashes $srcdir =~ s|\/\/|\/|; #----------------------------------------------------------- # do any image, or avi files exist? @fullnamefiles = `ls $srcdir/*.[Jj][Pp]*[Gg] $srcdir/*.[Aa][Vv][Ii]`; # counts files $numfiles = @fullnamefiles; if ( $numfiles eq 0 ) { print "\nThere appear to be no images or videos in $srcdir.\n"; exit; } #----------------------------------------------------------- # overwrite without complaining? if ($ARGV[0] eq "-o") { if (! -e "thumbnails") { system "mkdir thumbnails"; } if (! -e "fullsize") { system "mkdir fullsize"; } if (! -e "pages") { system "mkdir pages"; } } elsif ($ARGV[0] eq "-n") { # check the index.html page if (-e "index.html") { print "\nindex.html already exists. \n"; &error; exit; } # check the thumbnails directory if (-e "thumbnails") { print "\nthumbnails directory already exists. \n"; &error; exit; } else { system "mkdir thumbnails"; } # check the fullsize directory if (-e "fullsize") { print "\nfullsize directory already exists. \n"; &error; exit; } else { system "mkdir fullsize"; } if (-e "pages") { print "\npages directory already exists. \n"; &error; exit; } else { system("mkdir pages"); } } else { print "Usage webphoto.pl -o|-n\n"; print " (-o = overwrite; -n = no overwrite)\n"; exit; } #----------------------------------------------------------- # write the headers of the HTML &write_headers; #----------------------------------------------------------- # mangle the list of files @basenamefiles = (); @imagefiles = (); foreach $file (@fullnamefiles) { chomp $file; ($base, $path, $type) = fileparse("$file", qr{\..*}); $filename = "$base$type"; push @basenamefiles, $filename; if ( ( lc ( $type ) eq ".jpg" ) or ( lc ( $type ) eq ".jpeg" ) ) { push @imagefiles, $filename; } } $numimagefiles = @imagefiles; #print "\n\n\n@basenamefiles\n\n\n"; #----------------------------------------------------------- # the guts, as subroutines $imgcount = 1; $next = 0; $filenum = 0; #print "\n\n\n@fullnamefiles\n\n\n"; foreach $file (@basenamefiles) { $prevfile = ""; $nextfile = ""; # how far have we gotten? print "$imgcount of $numfiles... "; $imgcount++; # get the basename of the image file chomp $file; ($base, $path, $type) = fileparse("$file", qr{\..*}); $basefile = "$base$type"; $srcfile = "$srcdir/$basefile"; # if this is an image file if ( ( lc ( $type ) eq ".jpg" ) or ( lc ( $type ) eq ".jpeg" ) ) { # which file is this in the list? $filenum++; # get the previous and next files $next = $filenum ; $prev = $filenum - 2; $prevfile = "$imagefiles[$prev].html"; if ( "$prevfile" ne "$file.html" ) { #print "\nCurrent: $file (@imagefiles) \n"; #print "\nPrev: $prevfile\n"; } if ($next < $numimagefiles) { $nextfile = "$imagefiles[$next].html"; #print "\nnext: $next of $numimagefiles; Nextfile: $nextfile\n"; } ℑ } else { &video; } } &end_html; exit; #----------------------------------------------------------- sub image { # try to nconvert just with the -info option to get file properties @info = `nconvert -quiet -info \"$srcfile\"`; # did I find the word "Failed"? means the image has problems @match = grep(/.*Failed.*/, @info ); $count = @match; if ($count == 1) { print "Error in image file, or is not an image.\n"; exit; } # get the width & height of the image for resizing @width = split (/ */, $info[4]); $wid = ("$width[3]"); chop $wid; @height = split (/ */, $info[5]); $ht = ("$height[3]"); chop $ht; # I want the maximum thumbnail dimension to be 150 pixels if ($wid > $ht) { $thumbscale = 150 / $wid * 100; $maxdim = $wid; } else { $thumbscale = 150 / $ht * 100; $maxdim = $ht; } # rescale to create the thumbnail at a max if 150 pixels print "\nmaking thumbnail ($srcfile => thumbnails/$basefile)...\n"; system "nconvert -quiet -resize $thumbscale% $thumbscale% -o \"thumbnails/$basefile\" \"$srcfile\" "; # is the original image larger than 800 pixels wide or high? If so, rescale if ($maxdim > 800) { if ($wid >= $ht) { print "making rescaled image ($srcfile => fullsize/$basefile)... \n"; $fullscale = 800 / $wid * 100; system "nconvert -quiet -resize $fullscale% $fullscale% -o \"fullsize/$basefile\" \"$srcfile\" "; } elsif ($ht > $wid) { print "making rescaled image ($srcfile => fullsize/$basefile)... \n"; $fullscale = 800 / $ht * 100; system "nconvert -quiet -resize $fullscale% $fullscale% -o \"fullsize/$basefile\" \"$srcfile\" "; } } else { print "copying image ... \n"; system "cp \"$basefile\" fullsize"; } # create a page for each image # write the individual image page open(IMAGEPAGE, ">pages/$file.html"); print IMAGEPAGE "\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "$file\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "
\n"; print IMAGEPAGE "

\n"; print IMAGEPAGE "$file ($filenum of $numimagefiles)\n"; print IMAGEPAGE "

\n"; # navigation print IMAGEPAGE "Index\n"; if ($next != 1) { print IMAGEPAGE "Previous\n"; } if ($next < $numimagefiles) { print IMAGEPAGE "Next\n"; } print IMAGEPAGE "
\n"; print IMAGEPAGE "
\n"; # the image print IMAGEPAGE "\"$basefile\"
\n"; print IMAGEPAGE "
"; print IMAGEPAGE "
\n"; # there might be a sound associated with this file @base = split ( /\./, "$file" ); $b = $base[0]; $wav = "../$b.wav"; if ( -e "../$b.wav" ) { print IMAGEPAGE "\n"; } print IMAGEPAGE "
\n"; print IMAGEPAGE "Generated by \n"; print IMAGEPAGE "webphoto.pl\n"; print IMAGEPAGE "
\n"; print IMAGEPAGE "\n"; print IMAGEPAGE "\n"; close IMAGEPAGE; # the URL of the individual page for the Java page #$url0 = " "; #$url1 = " \n"; #$url = "$url0\n $url1\n"; $url_java0 = ""; $url_java1 = "  \; \n"; $url_java = "$url_java0\n $url_java1\n"; print JAVAPAGE "$url_java"; # the thumbnail on the No Java page print NOJAVAPAGE "\"$basefile\"\n"; } #----------------------------------------------------------- sub video { # is the webphotomovie.gif here? if ( ! -e "thumbnails/webphotomovie.gif" ) { system "lynx -dump http://gis.washington.edu/phurvitz/bin/webphotomovie.gif > thumbnails/webphotomovie.gif"; } #print JAVAPAGE "\n"; #print JAVAPAGE "\n"; #print JAVAPAGE "\n"; #print JAVAPAGE "
\n"; print JAVAPAGE "\"$basefile\"\n"; #print JAVAPAGE "
\n"; #print JAVAPAGE "$file\n"; #print JAVAPAGE "
\n"; print NOJAVAPAGE "\"$basefile\"\n"; } #----------------------------------------------------------- sub write_headers { # start writing the index page open (NOJAVAPAGE, '>index.html'); print NOJAVAPAGE "\n"; print NOJAVAPAGE "\n"; print NOJAVAPAGE "\n"; print NOJAVAPAGE "Web Photo Album: $dirname\n"; print NOJAVAPAGE "\n"; print NOJAVAPAGE "\n"; print NOJAVAPAGE "\n"; print NOJAVAPAGE "
\n"; print NOJAVAPAGE "

\n"; print NOJAVAPAGE "Web Photo Album: $dirname\n"; print NOJAVAPAGE "

\n"; print NOJAVAPAGE "

Jump to Java Version

\n"; print NOJAVAPAGE "

Click on a thumbnail to start the photo album.

\n"; open ( JAVAPAGE, '>index_java.html'); print JAVAPAGE "\n"; print JAVAPAGE "\n"; print JAVAPAGE "\n"; print JAVAPAGE "Web Photo Album: $dirname\n"; print JAVAPAGE "\n"; # java stuff for the JAVA page print JAVAPAGE "\n"; print JAVAPAGE "\n"; print JAVAPAGE "\n"; print JAVAPAGE "\n"; print JAVAPAGE "
\n"; print JAVAPAGE "

\n"; print JAVAPAGE "Web Photo Album: $dirname\n"; print JAVAPAGE "

\n"; print JAVAPAGE "

Jump to Photo Album Version

\n"; print JAVAPAGE "Click on a thumbnail to open the fullsize image.
\n"; print JAVAPAGE "After you are finished viewing the larger image opens, click anywhere to close it.

\n"; } #----------------------------------------------------------- sub end_html { # end the HTML print NOJAVAPAGE "


\n"; print NOJAVAPAGE "
\n"; print NOJAVAPAGE "Generated by \n"; print NOJAVAPAGE "webphoto.pl\n"; print NOJAVAPAGE "
\n"; print NOJAVAPAGE ""; print NOJAVAPAGE ""; close NOJAVAPAGE; print JAVAPAGE "


\n"; print JAVAPAGE "
\n"; print JAVAPAGE "Generated by \n"; print JAVAPAGE "webphoto.pl\n"; print JAVAPAGE "
\n"; print JAVAPAGE ""; print JAVAPAGE ""; close JAVAPAGE; } #----------------------------------------------------------- # error message sub error { print "I refuse to overwrite and make an error you will regret later.\n"; print "You will need to delete this manually and run me again\n"; print "or run with the -o option.\n\n"; }