]> www.wagner.pp.ru Git - oss/imgwww.git/blob - imagedir
Support for uppercase extensions.
[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=grep (/\.(jpe?g|png|tiff?|gif)$/i, <*>) ;
68 my @movielist= grep(/\.(mp4|avi|flv|mpg)$/i,<*>); 
69 print STDERR "Pictures found @piclist\n";
70 print STDERR "Videos found @movielist\n";
71 my ($prev,$next);
72 for ($j=0;$j<=$#piclist;$j++) {
73   $_ = $piclist[$j];
74   $prev = $piclist[$j-1] if $j>0;
75   if ($j<$#piclist) {
76   $next = $piclist[$j+1];
77   } else {
78    $next = undef;
79   } 
80   print STDERR "$j:$_";
81   chmod 0644, $_;
82   my $info = image_info($_);
83   my $thumbname=rescale($_,THUMBDIR,THUMBSIZE,$info);
84   my $inlinename=rescale($_,INLINEDIR,INLINESIZE,$info);
85   my $comment = make_comment_html($info);
86   my ($w,$h) = dim(image_info(THUMBDIR."/$_"));
87   print OUT "<tr>\n" if ($i % COLUMNS == 0);
88   print OUT "<td valign=top align=center><a href=\"$_.html\"",($opt_n?"
89   target=\"_blank\"":""),"><img border=0 width=$w height=$h src=\"$thumbname\"></a><br>$comment</td>\n";
90   print OUT "</tr>\n" if (++$i % COLUMNS == 0); 
91   print STDERR " html...";      
92   make_html($_,$info,$comment,$inlinename);
93   print STDERR "\b\b\b done"; 
94   print STDERR "\n";    
95
96 }
97 print OUT "</tr>\n" unless ($i % COLUMNS == 0);
98 print OUT "</table></body></html>\n";
99 close OUT;
100
101 sub rescale {
102   my ($name,$out_dir,$maxsize,$info) = @_;
103   my ($w,$h) = dim($info);
104   if ($w<$maxsize && $h<$maxsize) {
105         #picture is small enough to be used without rescaling
106         return $name;
107   }     
108   my $result = "$out_dir/$name";
109   if ( ! -f $result || -M $name < -M $result) {
110     print STDERR "  $out_dir...";
111
112     system "convert", "-geometry", $maxsize."x".$maxsize,$_,$result;
113     print STDERR "\b\b\b ";
114   } 
115   return $result;
116 }  
117
118 sub make_comment_html {
119   my $info = shift;
120 my $comment =
121
122         ref($info->{"Comment"})?join("\n",@{$info->{"Comment"}}):$info->{"Comment"};
123         $comment =~s/\&/&amp;/;
124         $comment =~s/"/&quot;/;
125         $comment =~s/>/&gt;/;
126         $comment =~s/</&lt;/;
127         return $comment;
128 }       
129
130 sub make_html {
131         my $imgfile = shift;
132         my $info = shift;
133         my $comment = shift;
134         my $inline = shift;
135         open HTML, ">$imgfile.html";
136         my ($w,$h) = dim($info);
137         my ($w1,$h1) = dim(image_info(INLINEDIR."/".$imgfile));
138         print HTML "<html><head><title>$dir:$imgfile</title></head><body>
139 <h2>$dir</h2>",($opt_f?"":"<h1>$imgfile</h1>"),(defined($info->{'DateTime'})?"<p
140 class=\"timestamp\">Time: $info->{'DateTime'}</p>":""),"<p>$comment</p>",
141 "<img src=\"".$inline."\" width=$w1 height=$h1>",
142 "<br clear=all><hr>
143
144 <hr>
145 <table width=100%><tr><td align=left>
146 ",($prev?"<a href=\"$prev.html\">&lt;&lt</a>":"&nbsp;"),
147 "</td><td align=center><a href=\".\">Up</A></td><td align=right>\n",
148 ,($next?"<a href=\"$next.html\">&gt;&gt</a>":"&nbsp;"),
149 "</td></tr></table>\n",
150 ($opt_l?"":"<a href=\"$imgfile\">$imgfile ($w x $h)</a>"),"
151 </body></html>
152 ";
153 close HTML;
154 }
155
156 sub fix_html {
157         my $imgfile = shift;
158         unlink "$imgfile.html";
159         make_html($imgfile);
160 }