#!/usr/bin/perl use utf8; use Encode; use locale; use open OUT => ":locale"; use constant THUMBSIZE => 150; use constant COLUMNS=>5; use constant THUMBDIR=>".thumbs"; use constant INLINESIZE=> 600; use constant INLINEDIR=>".inline"; use Image::Info qw(image_info dim); use Cwd; use Getopt::Std; =head1 NAME imagedir - generates HTML index for directory of pictures. =head1 SINOPSIS imagedir [-f] [B<-n>] [B<-l>] [B<-t> I] =head1 DESCRIPTION Generates two hidden subdirs under current directory - B<.thumbs> and B<.inlines> with smaller copies of the images and generates B<index.html> with small (fit in the square 150x150) copies of the images as table. Each image is a link to generated HTML page with bigger (fit into 600x600) copy of the image and (unless supressed) link to full sized image. If image contain JPEG or GIF comment, comment is inserted into HTML. =head1 OPTIONS =over 4 =item B<-f> Don't use filenames as headers of individual picture page =item B<-l> Supress link to fullsized images =item B<-n> If specified, all links in B<index.html> would have target="_blank" attribute. =item B<-t> I<string> Title. If none given, directory name is used. =back =cut use vars qw($opt_t $opt_n $opt_l $opt_f); our %decoders=('image/gif'=>'giftopnm','image/tiff'=>'tifftopnm','image/jpeg'=>'djpeg'); getopts("t:lnf"); my $dir = $opt_t || (split ("/",cwd()))[-1]; mkdir THUMBDIR if (! -d THUMBDIR); mkdir INLINEDIR if (! -d INLINEDIR); my $i=0; open OUT,">","index.html"; print OUT "<HTML><HEAD><TITLE>$dir\n\n

$dir

\n" ."

back

". "\n"; my @piclist=grep (/\.(jpe?g|png|tiff?|gif)$/i, <*>) ; my @movielist= grep(/\.(mp4|avi|flv|mpg)$/i,<*>); print STDERR "Pictures found @piclist\n"; print STDERR "Videos found @movielist\n"; my ($prev,$next); for ($j=0;$j<=$#piclist;$j++) { $_ = $piclist[$j]; $prev = $piclist[$j-1] if $j>0; if ($j<$#piclist) { $next = $piclist[$j+1]; } else { $next = undef; } print STDERR "$j:$_"; chmod 0644, $_; my $info = image_info($_); my $thumbname=rescale($_,THUMBDIR,THUMBSIZE,$info); my $inlinename=rescale($_,INLINEDIR,INLINESIZE,$info); my $comment = make_comment_html($info); my ($w,$h) = dim(image_info(THUMBDIR."/$_")); print OUT "\n" if ($i % COLUMNS == 0); print OUT "\n"; print OUT "\n" if (++$i % COLUMNS == 0); print STDERR " html..."; make_html($_,$info,$comment,$inlinename); print STDERR "\b\b\b done"; print STDERR "\n"; } print OUT "\n" unless ($i % COLUMNS == 0); print OUT "

$comment
\n"; close OUT; sub rescale { no locale; my ($name,$out_dir,$maxsize,$info) = @_; my ($w,$h) = dim($info); if ($w<$maxsize && $h<$maxsize) { #picture is small enough to be used without rescaling return $name; } my $result = "$out_dir/$name"; $result .= ".jpg" unless $result=~/\.jpe?g$/i; if ( ! -f $result || -M $name < -M $result) { print STDERR " $out_dir..."; my $scale; if ($w > $h) { $scale = $maxsize*1.0/$w; } else { $scale = $maxsize*1.0/$h; } my $ftype= $info->{file_media_type}; die "Unknown image type for $name: $ftype\n" if (not exists $decoders{$ftype}); my $decoder=$decoders{$ftype}; system "$decoder \"$name\" | pnmscale -xscale ". $scale . " -yscale " . "$scale |cjpeg > $result"; print STDERR "\b\b\b "; } return $result; } sub make_comment_html { my $info = shift; my $comment = Encode::decode("koi8-r",ref($info->{"Comment"})?join("\n",@{$info->{"Comment"}}):$info->{"Comment"}); $comment =~s/\&/&/; $comment =~s/"/"/; $comment =~s/>/>/; $comment =~s/", "$imgfile.html"; my ($w,$h) = dim($info); my ($w1,$h1) = dim(image_info(INLINEDIR."/".$imgfile)); print HTML "$dir:$imgfile

$dir

",($opt_f?"":"

$imgfile

"),(defined($info->{'DateTime'})?"

Time: $info->{'DateTime'}

":""),"

$comment

", "", "


",($prev?"<<":" "), "Up\n", ,($next?">>":" "), "
\n", ($opt_l?"":"$imgfile ($w x $h)")," "; close HTML; } sub fix_html { my $imgfile = shift; unlink "$imgfile.html"; make_html($imgfile); }