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