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/;
36 ######################################################################
38 FsObj->new(getcwd)->iterate;
47 my $path = $parent->{-path};
49 $path .= '/' if ($path);
51 my $fullpath = $parent->{-fullpath}.'/'.$name;
52 $self = {-root=>$parent->{-root}, -path=>$path, -base=>$name,
53 -fullpath=>$fullpath};
57 $self = {-root=>$root, -fullpath=>$root};
60 print "new $class: ($self->{-root}, $self->{-path}, $self->{-base}, $self->{-fullpath})\n";
66 my $fullpath .= $self->{-fullpath};
67 print "iterate in dir $fullpath\n";
72 unless (opendir($D,$fullpath)) {
73 warn "cannot opendir $fullpath: $!";
76 while (my $de = readdir($D)) {
77 next if ($de =~ /^\./);
78 my $child = $self->new($de);
80 push(@rdirlist,$child);
81 } elsif ($child->isimg) {
82 push(@rimglist,$child);
86 my @sdirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
87 undef @rdirlist; # inplace sorting would be handy here
88 my @simglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
89 undef @rimglist; # optimize away unsorted versions
91 foreach my $dir(@sdirlist) {
92 print "Dir: $dir->{-fullpath}\n";
95 foreach my $img(@simglist) {
96 print "Img: $img->{-fullpath}\n";
102 return ( -d $self->{-fullpath} );
107 my $fullpath = $self->{-fullpath};
108 return 0 unless ( -f $fullpath );
109 my $info = image_info($fullpath);
110 if (my $error = $info->{error}) {
111 if (($error !~ "Unrecognized file format") &&
112 ($error !~ "Can't read head")) {
113 warn "File \"$fullpath\": $error\n";
117 $self->{-info} = $info;
121 ######################################################################
123 ######################################################################
130 $dn .= "/".$dir if ($dir);
132 warn "not a directory: $dn";
136 unless (opendir($D,$dn)) {
137 warn "cannot opendir $dn: $!";
141 # recurse into subdirectories BEFORE opening index file
143 &iteratedir($D,$start,$dir,sub {
144 my ($start,$dir,$base)=@_;
146 $ndir .= "/" if ($ndir);
148 return unless ( -d $start."/".$ndir );
149 &processdir($start,$ndir);
154 my $title=&gettitle($dn,$dir);
158 my $inc=&getinclude($dn);
160 # generate directory index unless suppressed
162 if ( -e $dn."/.noindex" ) {
163 open(STDOUT,">/dev/null");
165 open(STDOUT,">".$dn."/index.html");
170 print start_html(-title => $title,
171 -style=>{-src=>[$inc."gallery.css",
172 $inc."lightbox.css"]},
173 -script=>[{-code=>"var incPrefix='$inc';"},
174 {-src=>$inc."gallery.js"},
175 {-src=>$inc."lightbox.js"}]),"\n";
176 print a({-href=>"../index.html"},"UP");
177 print start_center,"\n";
178 print h1($title),"\n";
180 # create list of sub-albums
183 &iteratedir($D,$start,$dir,sub {
184 my ($start,$dir,$base)=@_;
185 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
186 return unless ( -d $en );
187 unless ($hassubdirs) {
188 print hr,h2("Albums"),start_table,"\n";
191 &subalbum($base,&gettitle($en,$dir."/".$base));
193 print end_table,hr,"\n" if ($hassubdirs);
195 # create picture gallery
201 &iteratedir($D,$start,$dir,sub {
202 my ($start,$dir,$base)=@_;
203 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
204 return unless ( -f $en );
205 my $info = image_info($en);
206 if (my $error = $info->{error}) {
207 if (($error !~ "Unrecognized file format") &&
208 ($error !~ "Can't read head")) {
209 print STDERR "File \"$en\": $error\n";
213 if (&processfile($start,$dir,$base,$en,$info)) {
215 push(@piclist,$base);
216 push(@infolist,$info);
222 print br({-clear=>"all"}),"\n";
223 print a({-href=>".html/".$piclist[0]."-slide.html"},"Slideshow");
224 print hr,"\n" if ($haspics);
225 print end_center,"\n";
231 # generate html files for slideshow from @piclist
233 for (my $i=0;$i<=$#piclist;$i++) {
234 my $base=$piclist[$i];
237 $pbase=$piclist[$i-1] if ($i>0);
238 $nbase=$piclist[$i+1] if ($i<$#piclist);
239 for my $refresh('static','slide') {
240 &mkauxfile($start,$dir,$pbase,$base,$nbase,
241 $refresh,$infolist[$i]);
247 #############################################################
249 #############################################################
252 my ($D,$start,$dir,$prog)=@_;
254 while (my $de=readdir($D)) {
255 next if ($de =~ /^\./);
258 foreach my $de(sort @list) {
259 &$prog($start,$dir,$de);
269 #print STDERR "start include ",$dn."/".$str.".include","\n";
270 while ( ! -d $dn."/".$str.".include" ) {
271 #print STDERR "not include ",$dn."/".$str.".include","\n";
273 last unless ($depth--);
275 #print STDERR "end include ",$dn."/".$str.".include","\n";
276 if ( -d $dn."/".$str.".include" ) {
277 #print STDERR "return include ".$str.".include/".$fn,"\n";
278 return $str.".include/";
280 return ""; # won't work anyway but return something
289 if (open($F,"<".$dir."/.title")) {
294 print STDERR "enter title for $dir\n";
296 if ($str =~ /^\s*$/) {
299 if (open($F,">".$dir."/.title")) {
303 print STDERR "cant open .title in $dir for writing: $!";
310 my ($base,$title)=@_;
312 print Tr({-bgcolor=>"#c0c0c0"},
313 td(a({-href=>$base."/index.html"},$base)),
314 td(a({-href=>$base."/index.html"},$title))),"\n";
318 my ($start,$dir,$base,$fn,$info)=@_;
320 my ($w,$h) = dim($info);
321 my $title=$info->{'Comment'};
322 $title=$base unless ($title);
323 my $thumb=&scale($start,$dir,$base,$fn,160,$info);
324 my $medium=&scale($start,$dir,$base,$fn,640,$info);
325 print &infobox($info,$base,$fn),"\n";
326 print table({-class=>'slide'},Tr(td(
327 a({-href=>".html/$base-info.html",
328 -onClick=>"return showIbox('$base');"},$title),
330 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
331 img({-src=>$thumb})),
333 a({-href=>$base},"($w x $h)"),
339 my ($info,$base,$fn)=@_;
356 my $msg=start_div({-class=>'ibox',-id=>$base,-OnClick=>"HideIbox('$base');"});
357 $msg.=span({-style=>'float: left;'},"Info for $base").
358 span({-style=>'float: right;'},
359 a({-href=>"#",-OnClick=>"HideIbox('$base');"},"Close"));
360 $msg.=br({-clear=>'all'});
362 foreach my $k(@infokeys) {
363 $msg.=Tr(td($k.":"),td($info->{$k}));
371 my ($start,$dir,$pbase,$base,$nbase,$refresh,$info) =@_;
372 my $en=sprintf("%s/%s/.html/%s-%s.html",$start,$dir,$base,$refresh);
376 $pref=sprintf("%s-%s.html",$pbase,$refresh);
378 $pref="../index.html";
381 $nref=sprintf("%s-%s.html",$nbase,$refresh);
383 $nref="../index.html";
387 if ($refresh eq 'slide') {
389 $toggleref=sprintf("%s-static.html",$base);
392 $toggleref=sprintf("%s-slide.html",$base);
395 my $tdir=sprintf "%s/%s/.html",$start,$dir;
396 mkdir($tdir,0755) unless ( -d $tdir );
398 unless (open(STDOUT,">".$en)) {
399 warn "cannot open $en: $!";
402 my $title=$info->{'Comment'};
403 $title=$base unless ($title);
404 if ($refresh eq 'slide') {
405 print start_html(-title=>$title,
407 -head=>meta({-http_equiv=>'Refresh',
408 -content=>"3; url=$nref"})),"\n";
410 print start_html(-title=>$title,
411 -bgcolor=>"#808080"),"\n";
413 print start_center,"\n";
415 print a({-href=>"../index.html"},"Index")," | ";
416 print a({-href=>$pref},"<<Prev")," | ";
417 print a({-href=>$toggleref},$toggle)," | ";
418 print a({-href=>$nref},"Next>>");
420 print img({-src=>"../.640/".$base}),"\n";
421 print end_center,"\n";
427 my ($start,$dir,$base,$fn,$tsize,$info)=@_;
428 my ($w,$h) = dim($info);
429 my $max=($w>$h)?$w:$h;
430 my $factor=$tsize/$max;
432 return $base if ($factor >= 1);
434 my $tdir=sprintf "%s/%s/.%s",$start,$dir,$tsize;
435 mkdir($tdir,0755) unless ( -d $tdir );
436 my $tbase=sprintf ".%s/%s",$tsize,$base;
437 my $tfn=sprintf "%s/%s",$tdir,$base;
439 my @tstat=stat($tfn);
440 return $tbase if (@tstat && ($sstat[9] < $tstat[9])); # [9] -> mtime
442 print STDERR "scale by $factor from $fn to $tfn\n";
443 &doscaling($fn,$tfn,$factor,$w,$h);
448 my ($src,$dest,$factor,$w,$h)=@_;
450 my $im=new Image::Magick;
452 #print STDERR "doscale $src -> $dest by $factor\n";
453 $err=$im->Read($src);
455 $im->Scale(width=>$w*$factor,height=>$h*$factor);
456 $err=$im->Write($dest);
457 warn "ImageMagic: write \"$dest\": $err" if ($err);
459 warn "ImageMagic: read \"$src\": $err";
460 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");