#!/bin/env perl # Look for images in current directory and subdirectories, # create thumbnails and an index.html. ImageMagick is needed! # Author: A. Parenti, Nov21 2007 # Modified: AP, Jul07 2010 use warnings; use strict; use File::Basename; # Parse file paths into directory, filename and suffix. sub TrimR { # Trim <$nchar> chars from right my ($str,$nchar) = @_; my $newstr=substr($str,0,length($str)-$nchar); return $newstr; } sub TrimL { # Trim <$nchar> chars from right my ($str,$nchar) = @_; my $newstr=substr($str,$nchar,length($str)-$nchar); return $newstr; } my $i; # counter my $j; # another counter # Thumbnail resolution my $thumbres=200; # Check dependencies my @cmds=qw(identify convert); my $command; foreach $command(@cmds) { $i=system("which $command >& /dev/null"); if ($i != 0) { print "$command: Command is needed but not found. Quitting \n"; exit -1; } } # Print help if ($#ARGV==0 && ($ARGV[0] eq '-h' || $ARGV[0] eq "--help")) { print "Look for images in current directory, create thumbnails and an index.html.\n"; print "NB: ImageMagick is needed!\n"; print "Usage: $0\n"; exit 0; } # Search for jhead command my $jheadfound = 0; if (!system("which jhead >& /dev/null")) { $jheadfound=1; # jhead has been found } # Search for the comment file my $cmntfile = 0; my $cmntfname = "list.txt"; if (-e $cmntfname) { $cmntfile = 1; } ### Create list of jpg's and gif's my @exts=qw(jpg JPG jpeg JPEG gif GIF); # Look for this extensions my @imglist; # List of images my @extlist; # List of extensions my @dirlist; # List of directories for ($i=0; $i", $idxhtml); # Album Name my $albumname; print "Album name? "; $albumname=; if (length($albumname)<=1) { $albumname='Photo Album'; } chomp($albumname); print $idxhandle "<\!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> \n\n \n \n $albumname\n \n \n

$albumname

\n"; ### Process images for ($i=0; $i 0) { $prevhtml = $homedir.$dirlist[$i-1]."/".$imglist[$i-1].".html"; $prevhtml =~ s/\/.\//\//g;# Replace "/./" with "/" if (substr($prevhtml,0,2) eq "./") { $prevhtml=TrimL($prevhtml,2); # Trim "./" from left } } if ($i < scalar(@imglist)-1) { $nexthtml = $homedir.$dirlist[$i+1]."/".$imglist[$i+1].".html"; $nexthtml =~ s/\/.\//\//g;# Replace "/./" with "/" if (substr($nexthtml,0,2) eq "./") { $nexthtml=TrimL($nexthtml,2); # Trim "./" from left } } print "\nProcessing image $thisimg\n"; # Get original size my $tmpstr = `identify \"$dirlist[$i]/$thisimg\"`; $tmpstr =~ s/^.+$thisimg//; # Take apart the filename my @resolution=split(" ",$tmpstr); # The resolution is the second substring ### print "*** ".$tmpstr." ***\n"; my $resx = $resolution[1]; $resx =~ s/x.+$//; # Take the half before "x" character my $resy = $resolution[1]; $resy =~ s/^.+x//; # Take the half after "x" character $resy =~ s/\+.+$//; # Trim offset info, if any # Calculate thumbnail size my $newresx; my $newresy; if ($resx > $resy) { $newresx=$thumbres; $newresy=$thumbres*$resy/$resx; } else { $newresy=$thumbres; $newresx=$thumbres*$resx/$resy; } print "Thumbnail size: ${newresx}x${newresy}\n"; # Create thumbnail (if not done previously) if ($newresx != 0 && $newresy != 0) { if (!-e $dirlist[$i]."/".$imgthumb) { system("convert -resize ${newresx}x${newresy} \"$dirlist[$i]/$thisimg\" \"$dirlist[$i]/$imgthumb\""); } } my $cmnt=""; my $datetime=""; if ($jheadfound) { # Get comment and date from EXIF $cmnt=`jhead \"$dirlist[$i]/$thisimg\" | grep Comment`; $datetime=`jhead \"$dirlist[$i]/${thisimg}\" | grep 'Date/Time'`; } if ($cmntfile) { # When possible, get comment from file $tmpstr=`cat $cmntfname | grep \"$thisimg\"`; $tmpstr =~ s/$thisimg *//; # Trim away the filename $tmpstr =~ s/\n$//; # Trim away the carriage return chomp($tmpstr); if ($tmpstr ne "") { $cmnt = "Comment: ".$tmpstr; } } # Open html my $thishandle; open($thishandle, ">", $thishtml); print $thishandle "<\!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"> \n\n \n \n $thisimg\n \n \n

- $albumname -

\n"; # Links to previous/next images, index my $goprev; my $goidx; my $gonext; if ($i > 0) { # Link to previous image $goprev = "Previous"; } else { $goprev = "Previous"; } $goidx = "Index"; if ($i < scalar(@imglist)-1) { # Link to next image $gonext = "Next"; } else { $gonext = "Next"; } print $thishandle "

$goprev $goidx $gonext

\n"; if ($resx > $resy) { print $thishandle " $thisimg\n"; } else { print $thishandle " $thisimg\n"; } print $thishandle "

$cmnt

\n"; print $thishandle "

$datetime

\n"; print $thishandle "

$goprev $goidx $gonext

\n"; print $thishandle "\n \n\n"; # Create entry in index.html print $idxhandle "$thisimg\n"; print "Done!\n"; # Close html close($thishandle); } my $curdate=`date`; print $idxhandle "\n
\n

\n Valid HTML 4.01 Transitional\n

\n

Album created with PhotoAlbum v2.0 by A. Parenti

\n\nLast modified: $curdate\n\n \n"; # Close index.html close $idxhandle;