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.
32 use CGI qw/:html *table *center *div/;
33 use Image::Info qw/image_info dim/;
40 ######################################################################
42 FsObj->new(getcwd)->iterate;
51 my $path = $parent->{-path};
53 $path .= '/' if ($path);
55 my $fullpath = $parent->{-fullpath}.'/'.$name;
58 -root=>$parent->{-root},
62 -inc=>'../'.$parent->{-inc},
75 print "new $class:\n";
76 foreach my $k(keys %$self) {
77 print "\t$k\t=\t$self->{$k}\n";
84 my $fullpath=shift; # this is not a method
85 my $depth=20; # arbitrary max depth
88 while ( ! -d $fullpath."/".$inc ) {
90 last unless ($depth-- > 0);
93 return $inc.'/'; # prefix with trailing slash
95 return 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
101 my $fullpath .= $self->{-fullpath};
102 print "iterate in dir $fullpath\n" if ($debug);
107 unless (opendir($D,$fullpath)) {
108 warn "cannot opendir $fullpath: $!";
111 while (my $de = readdir($D)) {
112 next if ($de =~ /^\./);
113 my $child = $self->new($de);
115 push(@rdirlist,$child);
116 } elsif ($child->isimg) {
117 push(@rimglist,$child);
121 my @sdirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
122 undef @rdirlist; # inplace sorting would be handy here
123 my @simglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
124 undef @rimglist; # optimize away unsorted versions
126 # 1. first of all, fill title for this directory and create hidden subdirs
130 # 2. iterate through subdirectories to get their titles filled
132 foreach my $dir(@sdirlist) {
133 print "Dir: $dir->{-fullpath}\n" if ($debug);
137 # 3. start building directory index.html
138 # 4. iterate through subdirectories to build subalbums list
139 # 5. iterate through images to build cross-links
141 foreach my $img(@simglist) {
142 print "Img: $img->{-fullpath}\n" if ($debug);
145 # 6. iterate through images to build thumb list and aux html files
151 return ( -d $self->{-fullpath} );
156 my $fullpath = $self->{-fullpath};
157 return 0 unless ( -f $fullpath );
158 my $info = image_info($fullpath);
159 if (my $error = $info->{error}) {
160 if (($error !~ "Unrecognized file format") &&
161 ($error !~ "Can't read head")) {
162 warn "File \"$fullpath\": $error\n";
167 $self->{-info} = $info;
173 my $fullpath = $self->{-fullpath};
177 ######################################################################
179 ######################################################################
186 $dn .= "/".$dir if ($dir);
188 warn "not a directory: $dn";
192 unless (opendir($D,$dn)) {
193 warn "cannot opendir $dn: $!";
197 # recurse into subdirectories BEFORE opening index file
199 &iteratedir($D,$start,$dir,sub {
200 my ($start,$dir,$base)=@_;
202 $ndir .= "/" if ($ndir);
204 return unless ( -d $start."/".$ndir );
205 &processdir($start,$ndir);
210 my $title=&gettitle($dn,$dir);
214 my $inc=&getinclude($dn);
216 # generate directory index unless suppressed
218 if ( -e $dn."/.noindex" ) {
219 open(STDOUT,">/dev/null");
221 open(STDOUT,">".$dn."/index.html");
226 print start_html(-title => $title,
227 -style=>{-src=>[$inc."gallery.css",
228 $inc."lightbox.css"]},
229 -script=>[{-code=>"var incPrefix='$inc';"},
230 {-src=>$inc."gallery.js"},
231 {-src=>$inc."lightbox.js"}]),"\n";
232 print a({-href=>"../index.html"},"UP");
233 print start_center,"\n";
234 print h1($title),"\n";
236 # create list of sub-albums
239 &iteratedir($D,$start,$dir,sub {
240 my ($start,$dir,$base)=@_;
241 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
242 return unless ( -d $en );
243 unless ($hassubdirs) {
244 print hr,h2("Albums"),start_table,"\n";
247 &subalbum($base,&gettitle($en,$dir."/".$base));
249 print end_table,hr,"\n" if ($hassubdirs);
251 # create picture gallery
257 &iteratedir($D,$start,$dir,sub {
258 my ($start,$dir,$base)=@_;
259 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
260 return unless ( -f $en );
261 my $info = image_info($en);
262 if (my $error = $info->{error}) {
263 if (($error !~ "Unrecognized file format") &&
264 ($error !~ "Can't read head")) {
265 print STDERR "File \"$en\": $error\n";
269 if (&processfile($start,$dir,$base,$en,$info)) {
271 push(@piclist,$base);
272 push(@infolist,$info);
278 print br({-clear=>"all"}),"\n";
279 print a({-href=>".html/".$piclist[0]."-slide.html"},"Slideshow");
280 print hr,"\n" if ($haspics);
281 print end_center,"\n";
287 # generate html files for slideshow from @piclist
289 for (my $i=0;$i<=$#piclist;$i++) {
290 my $base=$piclist[$i];
293 $pbase=$piclist[$i-1] if ($i>0);
294 $nbase=$piclist[$i+1] if ($i<$#piclist);
295 for my $refresh('static','slide') {
296 &mkauxfile($start,$dir,$pbase,$base,$nbase,
297 $refresh,$infolist[$i]);
303 #############################################################
305 #############################################################
308 my ($D,$start,$dir,$prog)=@_;
310 while (my $de=readdir($D)) {
311 next if ($de =~ /^\./);
314 foreach my $de(sort @list) {
315 &$prog($start,$dir,$de);
325 #print STDERR "start include ",$dn."/".$str.".include","\n";
326 while ( ! -d $dn."/".$str.".include" ) {
327 #print STDERR "not include ",$dn."/".$str.".include","\n";
329 last unless ($depth--);
331 #print STDERR "end include ",$dn."/".$str.".include","\n";
332 if ( -d $dn."/".$str.".include" ) {
333 #print STDERR "return include ".$str.".include/".$fn,"\n";
334 return $str.".include/";
336 return ""; # won't work anyway but return something
345 if (open($F,"<".$dir."/.title")) {
350 print STDERR "enter title for $dir\n";
352 if ($str =~ /^\s*$/) {
355 if (open($F,">".$dir."/.title")) {
359 print STDERR "cant open .title in $dir for writing: $!";
366 my ($base,$title)=@_;
368 print Tr({-bgcolor=>"#c0c0c0"},
369 td(a({-href=>$base."/index.html"},$base)),
370 td(a({-href=>$base."/index.html"},$title))),"\n";
374 my ($start,$dir,$base,$fn,$info)=@_;
376 my ($w,$h) = dim($info);
377 my $title=$info->{'Comment'};
378 $title=$base unless ($title);
379 my $thumb=&scale($start,$dir,$base,$fn,160,$info);
380 my $medium=&scale($start,$dir,$base,$fn,640,$info);
381 print &infobox($info,$base,$fn),"\n";
382 print table({-class=>'slide'},Tr(td(
383 a({-href=>".html/$base-info.html",
384 -onClick=>"return showIbox('$base');"},$title),
386 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
387 img({-src=>$thumb})),
389 a({-href=>$base},"($w x $h)"),
395 my ($info,$base,$fn)=@_;
412 my $msg=start_div({-class=>'ibox',-id=>$base,-OnClick=>"HideIbox('$base');"});
413 $msg.=span({-style=>'float: left;'},"Info for $base").
414 span({-style=>'float: right;'},
415 a({-href=>"#",-OnClick=>"HideIbox('$base');"},"Close"));
416 $msg.=br({-clear=>'all'});
418 foreach my $k(@infokeys) {
419 $msg.=Tr(td($k.":"),td($info->{$k}));
427 my ($start,$dir,$pbase,$base,$nbase,$refresh,$info) =@_;
428 my $en=sprintf("%s/%s/.html/%s-%s.html",$start,$dir,$base,$refresh);
432 $pref=sprintf("%s-%s.html",$pbase,$refresh);
434 $pref="../index.html";
437 $nref=sprintf("%s-%s.html",$nbase,$refresh);
439 $nref="../index.html";
443 if ($refresh eq 'slide') {
445 $toggleref=sprintf("%s-static.html",$base);
448 $toggleref=sprintf("%s-slide.html",$base);
451 my $tdir=sprintf "%s/%s/.html",$start,$dir;
452 mkdir($tdir,0755) unless ( -d $tdir );
454 unless (open(STDOUT,">".$en)) {
455 warn "cannot open $en: $!";
458 my $title=$info->{'Comment'};
459 $title=$base unless ($title);
460 if ($refresh eq 'slide') {
461 print start_html(-title=>$title,
463 -head=>meta({-http_equiv=>'Refresh',
464 -content=>"3; url=$nref"})),"\n";
466 print start_html(-title=>$title,
467 -bgcolor=>"#808080"),"\n";
469 print start_center,"\n";
471 print a({-href=>"../index.html"},"Index")," | ";
472 print a({-href=>$pref},"<<Prev")," | ";
473 print a({-href=>$toggleref},$toggle)," | ";
474 print a({-href=>$nref},"Next>>");
476 print img({-src=>"../.640/".$base}),"\n";
477 print end_center,"\n";
483 my ($start,$dir,$base,$fn,$tsize,$info)=@_;
484 my ($w,$h) = dim($info);
485 my $max=($w>$h)?$w:$h;
486 my $factor=$tsize/$max;
488 return $base if ($factor >= 1);
490 my $tdir=sprintf "%s/%s/.%s",$start,$dir,$tsize;
491 mkdir($tdir,0755) unless ( -d $tdir );
492 my $tbase=sprintf ".%s/%s",$tsize,$base;
493 my $tfn=sprintf "%s/%s",$tdir,$base;
495 my @tstat=stat($tfn);
496 return $tbase if (@tstat && ($sstat[9] < $tstat[9])); # [9] -> mtime
498 print STDERR "scale by $factor from $fn to $tfn\n";
499 &doscaling($fn,$tfn,$factor,$w,$h);
504 my ($src,$dest,$factor,$w,$h)=@_;
506 my $im=new Image::Magick;
508 #print STDERR "doscale $src -> $dest by $factor\n";
509 $err=$im->Read($src);
511 $im->Scale(width=>$w*$factor,height=>$h*$factor);
512 $err=$im->Write($dest);
513 warn "ImageMagic: write \"$dest\": $err" if ($err);
515 warn "ImageMagic: read \"$src\": $err";
516 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");