#! /usr/bin/perl -w #----------------------------------------------------------- # creates a web photo album from all *.jpg and *.gif 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/ #----------------------------------------------------------- # to make getting the basename of the dir easy use File::Basename; # args? if ($#ARGV == -1) { print "Usage webphoto.pl -o|-n\n"; print " (-o = overwrite; -n = no overwrite)\n"; } # overwrite without complaining? if ($ARGV[0] eq "-o") { if (! -e "thumbnails") { system "mkdir thumbnails"; } if (! -e "fullsize") { system "mkdir fullsize"; } } elsif ($ARGV[0] eq "-n") { # check the index.html page if (-e "index.html") { print "\nindex.html already exists. "; print "I refuse to overwrite and make an error you will regret later.\n"; print "You will need to delete this file manually and run me again.\n\n"; exit; } # check the thumbnails directory if (-e "thumbnails") { print "\nthumbnails directory already exists. "; print "I refuse to overwrite and make an error you will regret later.\n"; print "You will need to delete this directory manually and run me again.\n\n"; exit; } else { system "mkdir thumbnails"; } # check the fullsize directory if (-e "fullsize") { print "\nfullsize directory already exists. "; print "I refuse to overwrite and make an error you will regret later.\n"; print "You will need to delete this directory manually and run me again.\n\n"; exit; } else { system "mkdir fullsize"; } } else { print "Usage webphoto.pl -o|-n\n"; print " (-o = overwrite; -n = no overwrite)\n"; exit; } # list of jpeg and gif files in current dir @files = `ls *.jpg *.gif`; $numfiles = @files; # create thumbnails $imgcount = 1; foreach $imgfile (@files) { # how far have we gotten? print "$imgcount of $numfiles\n"; $imgcount++; # get the basename of the image file $basefile = basename($imgfile); chop $basefile; # try to nconvert just with the -info option to get file properties @info = `nconvert -quiet -info $basefile`; # 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) { $scale = 150 / $wid * 100 } else { $scale = 150 / $ht * 100 } # rescale to create the thumbnail at a max if 150 pixels system "nconvert -quiet -resize $scale% $scale% -o thumbnails/$basefile $basefile"; # copy the original file to the fullsize directory system "cp $basefile fullsize/$basefile"; } # mangle the list of files @filez = (); foreach $file (@files) { $filename = basename($file); chomp $filename; push @filez, $filename; } # get directory name $dirname = basename(`pwd`); chomp $dirname; # make a "pages" dir for individual pages if (! -e "pages") { system("mkdir pages"); } # start writing the index page open(MASTERPAGE, '>index.html'); print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "Web Photo Album: $dirname\n"; print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "
\n"; print MASTERPAGE "

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

\n"; print MASTERPAGE "\n"; # use a count for determining the number of columns of thumbnails $count = 0; # use a count of images $next = 0; # create a page for each image foreach $file (@filez) { # get the previous and next files $next = $next + 1; $prev = $next - 2; $prevfile = "$filez[$prev].html"; if ($next < $numfiles) { $nextfile = "$filez[$next].html"; } # 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\n"; print IMAGEPAGE "

\n"; # navigation print IMAGEPAGE "Index\n"; if ($next != 1) { print IMAGEPAGE "Previous\n"; } if ($next < $numfiles) { print IMAGEPAGE "Next\n"; } print IMAGEPAGE "
\n"; print IMAGEPAGE "
\n"; # the image print IMAGEPAGE "
\n"; print IMAGEPAGE "
"; 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"; # the URL of the individual page $url = " \n"; # start creating the table structure if ($count eq 0) { print MASTERPAGE "\n"; print MASTERPAGE "$url"; $count = $count + 1; } elsif (($count < 3) ) { print MASTERPAGE "$url"; $count = $count + 1; } else { print MASTERPAGE "$url"; print MASTERPAGE "\n"; $count = 0; } #$count = $count + 1; close IMAGEPAGE; } # end the HTML print MASTERPAGE "\n\n
\n"; print MASTERPAGE "


\n"; print MASTERPAGE "
\n"; print MASTERPAGE "Generated by \n"; print MASTERPAGE "webphoto.pl\n"; print MASTERPAGE "
\n"; print MASTERPAGE ""; print MASTERPAGE ""; close MASTERPAGE;