]> www.wagner.pp.ru Git - oss/imgwww.git/blob - imagedir
Initial release
[oss/imgwww.git] / imagedir
1 #!/usr/bin/perl
2 use constant THUMBSIZE => 150;
3 use constant COLUMNS=>5;
4 use constant THUMBDIR=>".thumbs";
5 use constant INLINESIZE=> 600;
6 use constant INLINEDIR=>".inline";
7 use Image::Info qw(image_info dim);
8 use Cwd;
9 use Getopt::Std;
10
11 =head1 NAME
12
13 imagedir - generates HTML index for directory of pictures.
14
15 =head1 SINOPSIS
16
17 imagedir [-f] [B<-n>] [B<-l>] [B<-t> I<title>]
18
19 =head1 DESCRIPTION
20
21 Generates two hidden subdirs under current directory - B<.thumbs> and
22 B<.inlines> with smaller copies of the images and generates
23 B<index.html> with small (fit in the square 150x150) copies of the
24 images as table.
25
26 Each image is a link to generated HTML page with bigger (fit into
27 600x600) copy of the image and (unless supressed) link to full sized
28 image. If image contain JPEG or GIF comment, comment is inserted into
29 HTML.  
30
31 =head1 OPTIONS
32
33 =over 4
34
35 =item B<-f>
36  
37  Don't use filenames as headers of individual picture page
38
39 =item B<-l>
40
41 Supress link to fullsized images
42
43 =item B<-n>
44
45 If specified, all links in B<index.html> would have target="_blank"
46 attribute.
47
48 =item B<-t> I<string>
49
50 Title. If none given, directory name is used. 
51
52 =back
53
54 =cut
55
56 use vars qw($opt_t $opt_n $opt_l $opt_f);
57 getopts("t:lnf");
58
59 my $dir = $opt_t || (split ("/",cwd()))[-1];
60 mkdir THUMBDIR if (! -d THUMBDIR);
61 mkdir INLINEDIR if (! -d INLINEDIR);
62 my $i=0;
63 open OUT,">index.html";
64 print OUT "<HTML><HEAD><TITLE>$dir</TITLE>\n<BODY>\n<H1>$dir</H1>\n"
65 ."<p align=\"center\"><A HREF=\"..\">back</A></p>". 
66  "<TABLE CELLSPACING=10 CELLPADDING=0 BORDER=0>\n";
67 my @piclist=(<*.jpg>,<*.gif>,<*.png>) ;
68 print STDERR "@piclist\n";
69 my ($prev,$next);
70 for ($j=0;$j<=$#piclist;$j++) {
71   $_ = $piclist[$j];
72   $prev = $piclist[$j-1] if $j>0;
73   if ($j<$#piclist) {
74   $next = $piclist[$j+1];
75   } else {
76    $next = undef;
77   } 
78   print STDERR "$j:$_";
79   my $info = image_info($_);
80   my $thumbname=rescale($_,THUMBDIR,THUMBSIZE,$info);
81   my $inlinename=rescale($_,INLINEDIR,INLINESIZE,$info);
82   my $comment = make_comment_html($info);
83   my ($w,$h) = dim(image_info(THUMBDIR."/$_"));
84   print OUT "<tr>\n" if ($i % COLUMNS == 0);
85   print OUT "<td valign=top align=center><a href=\"$_.html\"",($opt_n?"
86   target=\"_blank\"":""),"><img border=0 width=$w height=$h src=\"$thumbname\"></a><br>$comment</td>\n";
87   print OUT "</tr>\n" if (++$i % COLUMNS == 0); 
88   print STDERR " html...";      
89   make_html($_,$info,$comment,$inlinename);
90   print STDERR "\b\b\b done"; 
91   print STDERR "\n";    
92
93 }
94 print OUT "</tr>\n" unless ($i % COLUMNS == 0);
95 print OUT "</table></body></html>\n";
96 close OUT;
97
98 sub rescale {
99   my ($name,$out_dir,$maxsize,$info) = @_;
100   my ($w,$h) = dim($info);
101   if ($w<$maxsize && $h<$maxsize) {
102         #picture is small enough to be used without rescaling
103         return $name;
104   }     
105   my $result = "$out_dir/$name";
106   if ( ! -f $result || -M $name < -M $result) {
107     print STDERR "  $out_dir...";
108
109     system "convert", "-geometry", $maxsize."x".$maxsize,$_,$result;
110     print STDERR "\b\b\b ";
111   } 
112   return $result;
113 }  
114
115 sub make_comment_html {
116   my $info = shift;
117 my $comment =
118
119         ref($info->{"Comment"})?join("\n",@{$info->{"Comment"}}):$info->{"Comment"};
120         $comment =~s/\&/&amp;/;
121         $comment =~s/"/&quot;/;
122         $comment =~s/>/&gt;/;
123         $comment =~s/</&lt;/;
124         return $comment;
125 }       
126
127 sub make_html {
128         my $imgfile = shift;
129         my $info = shift;
130         my $comment = shift;
131         my $inline = shift;
132         open HTML, ">$imgfile.html";
133         my ($w,$h) = dim($info);
134         my ($w1,$h1) = dim(image_info(INLINEDIR."/".$imgfile));
135         print HTML "<html><head><title>$dir:$imgfile</title></head><body>
136 <h2>$dir</h2>",($opt_f?"":"<h1>$imgfile</h1>"),(defined($info->{'DateTime'})?"<p
137 class=\"timestamp\">Time: $info->{'DateTime'}</p>":""),"<p>$comment</p>",
138 "<img src=\"".$inline."\" width=$w1 height=$h1>",
139 "<br clear=all><hr>
140
141 <hr>
142 <table width=100%><tr><td align=left>
143 ",($prev?"<a href=\"$prev.html\">&lt;&lt</a>":"&nbsp;"),
144 "</td><td align=center><a href=\".\">Up</A></td><td align=right>\n",
145 ,($next?"<a href=\"$next.html\">&gt;&gt</a>":"&nbsp;"),
146 "</td></tr></table>\n",
147 ($opt_l?"":"<a href=\"$imgfile\">$imgfile ($w x $h)</a>"),"
148 </body></html>
149 ";
150 close HTML;
151 }
152
153 sub fix_html {
154         my $imgfile = shift;
155         unlink "$imgfile.html";
156         make_html($imgfile);
157 }