#! /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; use File::Copy; use Cwd; $command = basename ($0); # args? if ($#ARGV == -1) { print "Usage $command -on srcdir}\n"; print " (-o = overwrite; -n = no overwrite)\n"; exit; } # source dir? if ($#ARGV == 1) { if (-d $ARGV[1]) { $srcdir = "$ARGV[1]/"; } } else { $srcdir = ""; } # convert backslashes to forward slashes $srcdir =~ s|\/\/|\/|; # 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. \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"; } } 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 $srcdir*.[Jj][Pp][Gg] $srcdir*.gif`; $numfiles = @files; # copy the perl file to this directory copy ( "$0", "webphoto.pl" ); # create thumbnails $imgcount = 1; foreach $imgfile (@files) { # how far have we gotten? print "$imgcount of $numfiles... "; $imgcount++; # get the basename of the image file chomp $imgfile; ($base, $path, $type) = fileparse("$imgfile", qr{\..*}); $basefile = "$base$type"; $srcfile = "$srcdir/$basefile"; # 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"; } } # mangle the list of files @filez = (); foreach $file (@files) { chomp $file; ($base, $path, $type) = fileparse("$file", qr{\..*}); $filename = "$base$type"; push @filez, $filename; } # get directory name $pwd = getcwd; $dirname = basename( $pwd ); chomp $dirname; # make a "pages" dir for individual pages if (! -e "pages") { system("mkdir pages"); } # start writing the index page open(SLIDESHOWPAGE, '>index_slideshow.html'); print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "Web Photo Album: $dirname\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "
\n"; print SLIDESHOWPAGE "

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

\n"; print SLIDESHOWPAGE "

Jump to Java Version

\n"; print SLIDESHOWPAGE "

Click on a thumbnail to start the photo album.

\n"; print SLIDESHOWPAGE "\n"; open(MASTERPAGE, '>index.html'); print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "\n"; print MASTERPAGE "Web Photo Album: $dirname\n"; print MASTERPAGE "\n"; # java stuff for the JAVA page 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 "

Jump to Photo Album Version

\n"; print MASTERPAGE "Click on a thumbnail to open the fullsize image.
\n"; print MASTERPAGE "When the larger image opens, click on it or back on the index page to close the larger image.

\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 $filenum = 0; foreach $file (@filez) { $filenum++; # 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 ($filenum of $numfiles)\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"; # there might be a sound associated with this file @base = split ( /\./, "$file" ); $b = $base[0]; $wav = "../$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"; # the URL of the individual page $url0 = "
\n"; $url = "$url0\n $url1\n"; ($soundfile = $file) =~ s|[Jj][Pp][Gg]|wav| ; $url_java0 = ""; $url_java1 = "  \; \n"; $url_java = "$url_java0\n $url_java1\n"; print MASTERPAGE "$url_java"; # start creating the table structure if ($count eq 0) { print SLIDESHOWPAGE "\n"; print SLIDESHOWPAGE "$url"; $count = $count + 1; } elsif (($count < 3) ) { print SLIDESHOWPAGE "$url"; $count = $count + 1; } else { print SLIDESHOWPAGE "$url"; print SLIDESHOWPAGE "\n"; $count = 0; } #$count = $count + 1; close IMAGEPAGE; } # end the HTML print SLIDESHOWPAGE "\n\n
"; $url1 = "
\n"; print SLIDESHOWPAGE "


\n"; print SLIDESHOWPAGE "
\n"; print SLIDESHOWPAGE "Generated by \n"; print SLIDESHOWPAGE "webphoto.pl\n"; print SLIDESHOWPAGE "
\n"; print SLIDESHOWPAGE ""; print SLIDESHOWPAGE ""; close SLIDESHOWPAGE; 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; # 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"; }