5 # Recursively create image gallery index and slideshow wrappings.
6 # Makes use of (slightly modified) "lightbox" Javascript/CSS as published
7 # at http://www.huddletogether.com/projects/lightbox/
9 # Copyright (c) 2006 Eugene G. Crosser
11 # This software is provided 'as-is', without any express or implied
12 # warranty. In no event will the authors be held liable for any damages
13 # arising from the use of this software.
15 # Permission is granted to anyone to use this software for any purpose,
16 # including commercial applications, and to alter it and redistribute it
17 # freely, subject to the following restrictions:
19 # 1. The origin of this software must not be misrepresented; you must not
20 # claim that you wrote the original software. If you use this software
21 # in a product, an acknowledgment in the product documentation would be
22 # appreciated but is not required.
23 # 2. Altered source versions must be plainly marked as such, and must not be
24 # misrepresented as being the original software.
25 # 3. This notice may not be removed or altered from any source distribution.
30 use CGI qw/:html *table *center *div/;
31 use Image::Info qw/image_info dim/;
37 ######################################################################
39 &processdir($startdir);
44 $dn .= "/".$dir if ($dir);
46 warn "not a directory: $dn";
50 unless (opendir($D,$dn)) {
51 warn "cannot opendir $dn: $!";
55 # recurse into subdirectories BEFORE opening index file
57 &iteratedir($D,$start,$dir,sub {
58 my ($start,$dir,$base)=@_;
60 $ndir .= "/" if ($ndir);
62 return unless ( -d $start."/".$ndir );
63 &processdir($start,$ndir);
68 my $title=&gettitle($dn,$dir);
72 my $inc=&getinclude($dn);
74 # generate directory index unless suppressed
76 if ( -e $dn."/.noindex" ) {
77 open(STDOUT,">/dev/null");
79 open(STDOUT,">".$dn."/index.html");
84 print start_html(-title => $title,
85 -style=>{-src=>[$inc."gallery.css",
86 $inc."lightbox.css"]},
87 -script=>[{-code=>"var incPrefix='$inc';"},
88 {-src=>$inc."gallery.js"},
89 {-src=>$inc."lightbox.js"}]),"\n";
90 print a({-href=>"../index.html"},"UP");
91 print start_center,"\n";
92 print h1($title),"\n";
94 # create list of sub-albums
97 &iteratedir($D,$start,$dir,sub {
98 my ($start,$dir,$base)=@_;
99 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
100 return unless ( -d $en );
101 unless ($hassubdirs) {
102 print hr,h2("Albums"),start_table,"\n";
105 &subalbum($base,&gettitle($en,$dir."/".$base));
107 print end_table,hr,"\n" if ($hassubdirs);
109 # create picture gallery
115 &iteratedir($D,$start,$dir,sub {
116 my ($start,$dir,$base)=@_;
117 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
118 return unless ( -f $en );
119 my $info = image_info($en);
120 if (my $error = $info->{error}) {
121 if (($error !~ "Unrecognized file format") &&
122 ($error !~ "Can't read head")) {
123 print STDERR "File \"$en\": $error\n";
127 if (&processfile($start,$dir,$base,$en,$info)) {
129 push(@piclist,$base);
130 push(@infolist,$info);
136 print br({-clear=>"all"}),"\n";
137 print a({-href=>".html/".$piclist[0]."-slide.html"},"Slideshow");
138 print hr,"\n" if ($haspics);
139 print end_center,"\n";
145 # generate html files for slideshow from @piclist
147 for (my $i=0;$i<=$#piclist;$i++) {
148 my $base=$piclist[$i];
151 $pbase=$piclist[$i-1] if ($i>0);
152 $nbase=$piclist[$i+1] if ($i<$#piclist);
153 for my $refresh('static','slide') {
154 &mkauxfile($start,$dir,$pbase,$base,$nbase,
155 $refresh,$infolist[$i]);
161 #############################################################
163 #############################################################
166 my ($D,$start,$dir,$prog)=@_;
168 while (my $de=readdir($D)) {
169 next if ($de =~ /^\./);
172 foreach my $de(sort @list) {
173 &$prog($start,$dir,$de);
183 #print STDERR "start include ",$dn."/".$str.".include","\n";
184 while ( ! -d $dn."/".$str.".include" ) {
185 #print STDERR "not include ",$dn."/".$str.".include","\n";
187 last unless ($depth--);
189 #print STDERR "end include ",$dn."/".$str.".include","\n";
190 if ( -d $dn."/".$str.".include" ) {
191 #print STDERR "return include ".$str.".include/".$fn,"\n";
192 return $str.".include/";
194 return ""; # won't work anyway but return something
203 if (open($F,"<".$dir."/.title")) {
208 print STDERR "enter title for $dir\n";
210 if ($str =~ /^\s*$/) {
213 if (open($F,">".$dir."/.title")) {
217 print STDERR "cant open .title in $dir for writing: $!";
224 my ($base,$title)=@_;
226 print Tr({-bgcolor=>"#c0c0c0"},
227 td(a({-href=>$base."/index.html"},$base)),
228 td(a({-href=>$base."/index.html"},$title))),"\n";
232 my ($start,$dir,$base,$fn,$info)=@_;
234 my ($w,$h) = dim($info);
235 my $title=$info->{'Comment'};
236 $title=$base unless ($title);
237 my $thumb=&scale($start,$dir,$base,$fn,160,$info);
238 my $medium=&scale($start,$dir,$base,$fn,640,$info);
239 print &infobox($info,$base,$fn),"\n";
240 print table({-class=>'slide'},Tr(td(
241 a({-href=>".html/$base-info.html",
242 -onClick=>"return showIbox('$base');"},$title),
244 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
245 img({-src=>$thumb})),
247 a({-href=>$base},"($w x $h)"),
253 my ($info,$base,$fn)=@_;
270 my $msg=start_div({-class=>'ibox',-id=>$base,-OnClick=>"HideIbox('$base');"});
271 $msg.=span({-style=>'float: left;'},"Info for $base").
272 span({-style=>'float: right;'},
273 a({-href=>"#",-OnClick=>"HideIbox('$base');"},"Close"));
274 $msg.=br({-clear=>'all'});
276 foreach my $k(@infokeys) {
277 $msg.=Tr(td($k.":"),td($info->{$k}));
285 my ($start,$dir,$pbase,$base,$nbase,$refresh,$info) =@_;
286 my $en=sprintf("%s/%s/.html/%s-%s.html",$start,$dir,$base,$refresh);
290 $pref=sprintf("%s-%s.html",$pbase,$refresh);
292 $pref="../index.html";
295 $nref=sprintf("%s-%s.html",$nbase,$refresh);
297 $nref="../index.html";
301 if ($refresh eq 'slide') {
303 $toggleref=sprintf("%s-static.html",$base);
306 $toggleref=sprintf("%s-slide.html",$base);
309 my $tdir=sprintf "%s/%s/.html",$start,$dir;
310 mkdir($tdir,0755) unless ( -d $tdir );
312 unless (open(STDOUT,">".$en)) {
313 warn "cannot open $en: $!";
316 my $title=$info->{'Comment'};
317 $title=$base unless ($title);
318 if ($refresh eq 'slide') {
319 print start_html(-title=>$title,
321 -head=>meta({-http_equiv=>'Refresh',
322 -content=>"3; url=$nref"})),"\n";
324 print start_html(-title=>$title,
325 -bgcolor=>"#808080"),"\n";
327 print start_center,"\n";
329 print a({-href=>"../index.html"},"Index")," | ";
330 print a({-href=>$pref},"<<Prev")," | ";
331 print a({-href=>$toggleref},$toggle)," | ";
332 print a({-href=>$nref},"Next>>");
334 print img({-src=>"../.640/".$base}),"\n";
335 print end_center,"\n";
341 my ($start,$dir,$base,$fn,$tsize,$info)=@_;
342 my ($w,$h) = dim($info);
343 my $max=($w>$h)?$w:$h;
344 my $factor=$tsize/$max;
346 return $base if ($factor >= 1);
348 my $tdir=sprintf "%s/%s/.%s",$start,$dir,$tsize;
349 mkdir($tdir,0755) unless ( -d $tdir );
350 my $tbase=sprintf ".%s/%s",$tsize,$base;
351 my $tfn=sprintf "%s/%s",$tdir,$base;
353 my @tstat=stat($tfn);
354 return $tbase if (@tstat && ($sstat[9] < $tstat[9])); # [9] -> mtime
356 print STDERR "scale by $factor from $fn to $tfn\n";
357 &doscaling($fn,$tfn,$factor,$w,$h);
362 my ($src,$dest,$factor,$w,$h)=@_;
364 my $im=new Image::Magick;
366 #print STDERR "doscale $src -> $dest by $factor\n";
367 $err=$im->Read($src);
369 $im->Scale(width=>$w*$factor,height=>$h*$factor);
370 $err=$im->Write($dest);
371 warn "ImageMagic: write \"$dest\": $err" if ($err);
373 warn "ImageMagic: read \"$src\": $err";
374 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");