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=>"../"},"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 hr,"\n" if ($haspics);
138 print end_center,"\n";
144 # generate html files for slideshow from @piclist
146 for (my $i=0;$i<=$#piclist;$i++) {
147 my $base=$piclist[$i];
150 $pbase=$piclist[$i-1] if ($i>0);
151 $nbase=$piclist[$i+1] if ($i<$#piclist);
152 for my $refresh('static','slide') {
153 &mkauxfile($start,$dir,$pbase,$base,$nbase,
154 $refresh,$infolist[$i]);
160 #############################################################
162 #############################################################
165 my ($D,$start,$dir,$prog)=@_;
167 while (my $de=readdir($D)) {
168 next if ($de =~ /^\./);
171 foreach my $de(sort @list) {
172 &$prog($start,$dir,$de);
182 #print STDERR "start include ",$dn."/".$str.".include","\n";
183 while ( ! -d $dn."/".$str.".include" ) {
184 #print STDERR "not include ",$dn."/".$str.".include","\n";
186 last unless ($depth--);
188 #print STDERR "end include ",$dn."/".$str.".include","\n";
189 if ( -d $dn."/".$str.".include" ) {
190 #print STDERR "return include ".$str.".include/".$fn,"\n";
191 return $str.".include/";
193 return ""; # won't work anyway but return something
202 if (open($F,"<".$dir."/.title")) {
207 print STDERR "enter title for $dir\n";
209 if ($str =~ /^\s*$/) {
212 if (open($F,">".$dir."/.title")) {
216 print STDERR "cant open .title in $dir for writing: $!";
223 my ($base,$title)=@_;
225 print Tr({-bgcolor=>"#c0c0c0"},
226 td(a({-href=>$base."/"},$base)),
227 td(a({-href=>$base."/"},$title))),"\n";
231 my ($start,$dir,$base,$fn,$info)=@_;
233 my ($w,$h) = dim($info);
234 my $title=$info->{'Comment'};
235 $title=$base unless ($title);
236 my $thumb=&scale($start,$dir,$base,$fn,160,$info);
237 my $medium=&scale($start,$dir,$base,$fn,640,$info);
238 print &infobox($info,$base,$fn),"\n";
239 print table({-class=>'slide'},Tr(td(
240 a({-href=>".html/$base-info.html",
241 -onClick=>"return showIbox('$base');"},$title),
243 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
244 img({-src=>$thumb})),
246 a({-href=>$base},"($w x $h)"),
252 my ($info,$base,$fn)=@_;
269 my $msg=start_div({-class=>'ibox',-id=>$base,-OnClick=>"HideIbox('$base');"});
270 $msg.=span({-style=>'float: left;'},"Info for $base").
271 span({-style=>'float: right;'},
272 a({-href=>"#",-OnClick=>"HideIbox('$base');"},"Close"));
273 $msg.=br({-clear=>'all'});
275 foreach my $k(@infokeys) {
276 $msg.=Tr(td($k.":"),td($info->{$k}));
284 my ($start,$dir,$pbase,$base,$nbase,$refresh,$info) =@_;
285 my $en=sprintf("%s/%s/.html/%s-%s.html",$start,$dir,$base,$refresh);
289 $pref=sprintf("%s-%s.html",$pbase,$refresh);
294 $nref=sprintf("%s-%s.html",$nbase,$refresh);
299 my $tdir=sprintf "%s/%s/.html",$start,$dir;
300 mkdir($tdir,0755) unless ( -d $tdir );
302 unless (open(STDOUT,">".$en)) {
303 warn "cannot open $en: $!";
306 my $title=$info->{'Comment'};
307 $title=$base unless ($title);
308 if ($refresh eq 'slide') {
309 print start_html(-title=>$title,
310 -head=>meta({-http_equiv=>'Refresh',
311 -content=>"3; url=$nref"})),"\n";
313 print start_html(-title=>$title),"\n";
315 print img({-src=>"../.640/".$base});
321 my ($start,$dir,$base,$fn,$tsize,$info)=@_;
322 my ($w,$h) = dim($info);
323 my $max=($w>$h)?$w:$h;
324 my $factor=$tsize/$max;
326 return $base if ($factor >= 1);
328 my $tdir=sprintf "%s/%s/.%s",$start,$dir,$tsize;
329 mkdir($tdir,0755) unless ( -d $tdir );
330 my $tbase=sprintf ".%s/%s",$tsize,$base;
331 my $tfn=sprintf "%s/%s",$tdir,$base;
333 my @tstat=stat($tfn);
334 return $tbase if (@tstat && ($sstat[9] < $tstat[9])); # [9] -> mtime
336 print STDERR "scale by $factor from $fn to $tfn\n";
337 &doscaling($fn,$tfn,$factor,$w,$h);
342 my ($src,$dest,$factor,$w,$h)=@_;
344 my $im=new Image::Magick;
346 #print STDERR "doscale $src -> $dest by $factor\n";
347 $err=$im->Read($src);
349 $im->Scale(width=>$w*$factor,height=>$h*$factor);
350 $err=$im->Write($dest);
351 warn "ImageMagic: write \"$dest\": $err" if ($err);
353 warn "ImageMagic: read \"$src\": $err";
354 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");