#!/usr/local/bin/perl
use Getopt::Std;
# By John Flynn - lynx@zorin.org. Started 6/15/2002
# Copyleft 2002 KKI
# Requires cjpeg, pnmscale, djpeg
# Version 1.01 -- Release -- 7/24/2002
# This is hacker-ware. Distribution is completely free. You are encouraged
# to make improvements and distribute it further. I'd appreciate your sending
# me a copy of whatever improvements you make, though, as a diff.
# Makes a quick photo album.
# Image files: blah.jpg in the root directory
# Caption files: blah.jpg.txt in the root directory
# Album page descriptions: albumN-header.html in root dir where
# N is the page number
# Album title: albumtitle.txt in root dir
# Options:
# -d : If no caption, insert file date
getopts("hrfpvdDlt:s:T:S:H:W:");
# Defaults
$imageswide=4;
$imageshigh=30;
$smallerquality=90;
$thumbquality=100;
$smallersize=500000;
$thumbsize=22000;
$srcdirectory=".";
# Page colors
$bgcolor="#222222";
$textcolor="#FFFFFF";
$linkcolor="#9999CC";
$vlinkcolor="#CCCC99";
$alinkcolor="#CCCCCC";
$tablebgcolor="#333333";
if ($opt_h) {
print STDERR "Usage: makealbum2.pl [ options ]\n\nPage generation options:\n(You must delete the album*.html files and imagepages directory if you don't\n want stray files when the number of images is reduced.)\n\n -W : n images wide per page (default: $imageswide)\n -H : n images high per page (default: $imageshigh)\n -d: Add date to images with no caption\n -r: Reverse sort the album\n -D: Sort (or reverse sort) by date\n -h: You're looking at it\n -l: Make links to other directories containing albums\n -p: Generate seperate HTML pages for each image to allow for navigation\n -v: Be verbose (VERY SPAMMY!!)";
print STDERR "\nOptions for thumbnails and smaller images:\n(You must delete the thumbs and smaller directories before re-running with\nthese options changed.)\n\n -s : Smaller image size (in total pixels) Default: $smallersize\n -t : Thumbnail image size (in total pixels) Default: $thumbsize\n -S : Smaller image quality (1-100), default is $smallerquality\n -T : Thumbnail quality (1-100), default is $thumbquality\n -f: DON'T create \"smaller\" images. Make links to the full sized image only.\n";
exit(0);
}
if ($opt_W) {
$imageswide=$opt_W;
}
if ($opt_H) {
$imageshigh=$opt_H;
}
if ($opt_d) {
$insertdate=1;
} else {
$insertdate=0;
}
if ($opt_D) {
$sortbydate=1;
} else {
$sortbydate=0;
}
if ($opt_p) {
$seperatepages=1;
} else {
$seperatepages=0;
}
if ($opt_T) {
if ($opt_T < 1 || $opt_T > 100) {
print STDERR "Qualities must be between 1 and 100! Exiting...\n";
exit(1);
}
$thumbquality=$opt_T;
}
if ($opt_S) {
if ($opt_S < 1 || $opt_S > 100) {
print STDERR "Qualities must be between 1 and 100! Exiting...\n";
exit(1);
}
$smallerquality=$opt_S;
}
if ($opt_t) {
if ($opt_t < 1) {
print STDERR "Image size must be greater than 1! (preferably greater than 1000) Exiting...\n";
exit(1);
}
$thumbsize=$opt_t;
}
if ($opt_s) {
if ($opt_s < 1) {
print STDERR "Image size must be greater than 1! (preferably greater than 1000) Exiting...\n";
exit(1);
}
$smallersize=$opt_s;
}
if ($opt_r) {
$reversesort=1;
} else {
$reversesort=0;
}
sub by_file_date {
($dud,$dud,$dud,$dud,$dud,$dud,$dud,$dud,$dud,$mtimea,$dud,$dud,$dud) = stat "$srcdirectory/$a";
($dud,$dud,$dud,$dud,$dud,$dud,$dud,$dud,$dud,$mtimeb,$dud,$dud,$dud) = stat "$srcdirectory/$b";
($mtimea <=> $mtimeb);
}
# Get the album's title
#$albumtitle = `cat $srcdirectory/albumtitle.txt`;
if ( -e "$srcdirectory/albumtitle.txt" ) {
open (TITLE,"$srcdirectory/albumtitle.txt");
while () {
$line=$_;
chomp($line); # Fucking newlines...
$albumtitle = $albumtitle . $_;
}
} else {
$albumtitle = "The 2003 GETS Internship Pictures";
}
# First, let's shove all the files in the current directory into an array.
# They will be shoved in alphabetical order; this is how the order in
# the album will be determined.
$imagecount=0;
opendir (DIR, $srcdirectory);
while (defined($srcfile = readdir(DIR))) {
# jpe? I dunno what software uses this extension for jpegs, but..hey
if ($srcfile =~ /.*(jpg|JPG|jpeg|JPEG|jpe|JPE)$/) { # only deal with jpgs
push @IMAGELIST,$srcfile;
++$imagecount;
}
# If it's a directory, see if there's an album in it.
# If so, add it to the dirlist.
if ( $opt_l && -d $srcfile && -e "$srcfile/album1.html" && !($srcfile eq ".")) {
push @DIRLIST,$srcfile;
$dirs=1;
}
}
if ($sortbydate) {
@IMAGELIST = sort by_file_date (@IMAGELIST);
} else {
@IMAGELIST = sort (@IMAGELIST);
}
if ($reversesort) {
@IMAGELIST = reverse(@IMAGELIST);
}
# Create the "smaller" and "thumbs" directories if they don't exist already.
if (!$opt_f) {
mkdir "$srcdirectory/smaller", 0755 unless -d "$srcdirectory/smaller";
}
mkdir "$srcdirectory/thumbs", 0755 unless -d "$srcdirectory/thumbs";
if ($seperatepages) {
mkdir "$srcdirectory/imagepages", 0755 unless -d "$srcdirectory/imagepages";
}
# Find out how many pages there will be and round the number up.
$imagesperpage = $imageswide * $imageshigh;
$totalpages=$imagecount / $imagesperpage;
if ($totalpages != int($totalpages)) {
$totalpages = int($totalpages) + 1;
}
# Make smaller versions of all the images. Might as well get it over
# with.
print STDERR "...creating thumbnails. This may take a while!\n" unless !$opt_v;
foreach $image (@IMAGELIST) {
if (!$opt_f) {
system("djpeg \"$srcdirectory/$image\" | pnmscale -pixels $smallersize |cjpeg -quality $smallerquality > \"$srcdirectory/smaller/$image\"") unless -e "$srcdirectory/smaller/$image";
}
system("djpeg \"$srcdirectory/$image\" | pnmscale -pixels $thumbsize |cjpeg -quality $thumbquality > \"$srcdirectory/thumbs/$image\"") unless -e "$srcdirectory/thumbs/$image";
}
# If we DON'T want smaller images, make the $smaller variable . so that
# This directory name isn't provided.
if ($opt_f) {
$smaller=".";
} else {
$smaller="smaller";
}
# Now let's create all the HTML pages.
$currentimagenum=0;
for ($pagenum=1;$pagenum<=$totalpages;++$pagenum) {
print STDERR "...Writing out $srcdirectory/album$pagenum.html\n" unless !$opt_v;
open (HTMLFILE,">$srcdirectory/album$pagenum.html");
print HTMLFILE "\n";
print HTMLFILE "$albumtitle - Page $pagenum\n";
print HTMLFILE "\n";
print HTMLFILE "$albumtitle
\n";
print HTMLFILE "Check out The 2003 GETS Internship Videos / Slides
\n";
# Make links to other directories that have albums.
if ($dirs) {
print HTMLFILE "Other Directories: ";
foreach $dir (@DIRLIST) {
print HTMLFILE "$dir ";
}
print HTMLFILE "
";
}
# Make the "navigator" line
$navigator= "";
if ($pagenum!=1) {
$navigator = $navigator . "<< ";
}
$navigator = $navigator . "Go to page: ";
for ($i=1; $i <= $totalpages; ++$i) {
if ($pagenum==$i) {
$navigator = $navigator . "$i ";
} else {
$navigator = $navigator . "$i ";
}
}
if ($pagenum!=$totalpages) {
$navigator = $navigator . " >>";
}
$navigator = $navigator . "";
# If there's an albumN-header.html file, print its contents before the images.
if ( -e "$srcdirectory/header-album$pagenum.html" ) {
open (HEADER,"$srcdirectory/header-album$pagenum.html");
foreach () {
print HTMLFILE "$_\n";
}
print HTMLFILE "
\n";
}
print HTMLFILE "$navigator
";
# Now let's put $imagesperpage images on the page. If we run out of
# images, just stop there.
print HTMLFILE "\n";
for ($i=1 ; $i <= $imagesperpage ; ++$i) {
if ( -f "$srcdirectory/$IMAGELIST[$currentimagenum]" ) {
if ($seperatepages) {
# Open the upcoming HTML image file
print STDERR "...writing out $srcdirectory/imagepages/$IMAGELIST[$currentimagenum].html\n" unless !$opt_v;
open (IMAGEHTMLFILE,">$srcdirectory/imagepages/$IMAGELIST[$currentimagenum].html");
# Write the HTML that links to the page
print HTMLFILE " fullsized";
if (!$opt_f) {
print HTMLFILE " | websized";
}
print HTMLFILE " ";
} else {
print HTMLFILE " fullsized";
if (!$opt_f) {
print HTMLFILE " | websized";
}
print HTMLFILE " ";
}
if ($seperatepages) {
print IMAGEHTMLFILE "$IMAGELIST[$currentimagenum]\n";
print IMAGEHTMLFILE "";
# Print "navigation" bar. Note, if sort was reversed, print it
# Backwards so previous and next still make sense.
if ($reversesort) {
$qualifier=-1;
} else {
$qualifier=1;
}
if ((0 <= ($currentimagenum-$qualifier)) && (($currentimagenum-$qualifier) < $imagecount)) {
print IMAGEHTMLFILE "<< Previous Image | ";
}
print IMAGEHTMLFILE "Back to Album";
if ((0 <= ($currentimagenum+$qualifier)) && (($currentimagenum+$qualifier) < $imagecount)) {
print IMAGEHTMLFILE " | Next Image >>";
}
print IMAGEHTMLFILE " ";
print IMAGEHTMLFILE " \n";
}
# If there is a caption file, throw it in.
if ( -e "$srcdirectory/$IMAGELIST[$currentimagenum].txt" ) {
open (CAPTION,"$srcdirectory/$IMAGELIST[$currentimagenum].txt");
foreach () {
chomp ($_);
print HTMLFILE "$_";
if ($seperatepages) {
print IMAGEHTMLFILE "$_ \n";
}
}
} else {
if ($insertdate) {
($dud,$dud,$dud,$dud,$dud,$dud,$dud,$dud,$dud,$mtime,$dud,$dud,$dud) = stat "$srcdirectory/$IMAGELIST[$currentimagenum]";
($dud,$dud,$dud,$mday,$mon,$year,$dud,$dud,$dud)=localtime($mtime);
$year += 1900;
++$mon;
$datestring="$mon/$mday/$year";
print HTMLFILE "$datestring";
if ($seperatepages) {
print IMAGEHTMLFILE "$datestring \n";
}
}
}
if ($seperatepages) {
print IMAGEHTMLFILE "";
close (IMAGEHTMLFILE);
}
++$currentimagenum;
++$imagesonthisline;
print HTMLFILE " | \n";
if ($imagesonthisline >= $imageswide) {
print HTMLFILE " |
";
$imagesonthisline = 0;
}
} else {
break;
}
}
print HTMLFILE "
\n";
print HTMLFILE "$navigator
\n";
print HTMLFILE "Check out The 2003 GETS Internship Videos / Slides \n";
print HTMLFILE "Please send all flames, complaints, and praises to Ubaid R Khan . This page generated by Ubaid Khan.\n";
print HTMLFILE "\n";
close (HTMLFILE);
}
print STDERR "Album created with $totalpages pages, $imagecount images.\n";