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/;
39 my @sizes = (160, 640);
41 ######################################################################
47 GetOptions('asktitle'=>\$asktitle,
48 'noasktitle'=>\$noasktitle,
51 my $term = new Term::ReadLine "Edit Title";
53 FsObj->new(getcwd)->iterate;
63 my $fullpath = $parent->{-fullpath}.'/'.$name;
66 -root=>$parent->{-root},
69 -inc=>'../'.$parent->{-inc},
82 print "new $class:\n";
83 foreach my $k(keys %$self) {
84 print "\t$k\t=\t$self->{$k}\n";
91 my $fullpath=shift; # this is not a method
92 my $depth=20; # arbitrary max depth
95 while ( ! -d $fullpath."/".$inc ) {
97 last unless ($depth-- > 0);
100 return $inc.'/'; # prefix with trailing slash
102 return 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
108 my $fullpath .= $self->{-fullpath};
109 print "iterate in dir $fullpath\n" if ($debug);
114 unless (opendir($D,$fullpath)) {
115 warn "cannot opendir $fullpath: $!";
118 while (my $de = readdir($D)) {
119 next if ($de =~ /^\./);
120 my $child = $self->new($de);
122 push(@rdirlist,$child);
123 } elsif ($child->isimg) {
124 push(@rimglist,$child);
128 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
129 undef @rdirlist; # inplace sorting would be handy here
130 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
131 undef @rimglist; # optimize away unsorted versions
133 # 1. first of all, fill title for this directory and create hidden subdirs
137 # 2. iterate through subdirectories to get their titles filled
138 # before we start writing out subalbum list
140 foreach my $dir(@dirlist) {
141 print "Dir: $dir->{-fullpath}\n" if ($debug);
145 # 3. start building index.html for the directory
146 # 4. iterate through subdirectories to build subalbums list
147 # 5. iterate through images to build cross-links
149 foreach my $img(@imglist) {
150 print "Img: $img->{-fullpath}\n" if ($debug);
153 # 6. iterate through images to build thumb list and aux html files
154 # 7. comlplete building index.html for the directory
160 return ( -d $self->{-fullpath} );
165 my $fullpath = $self->{-fullpath};
166 return 0 unless ( -f $fullpath );
167 my $info = image_info($fullpath);
168 if (my $error = $info->{error}) {
169 if (($error !~ "Unrecognized file format") &&
170 ($error !~ "Can't read head")) {
171 warn "File \"$fullpath\": $error\n";
176 $self->{-info} = $info;
182 my $fullpath = $self->{-fullpath};
183 for my $subdir(@sizes, 'html') {
184 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
185 mkdir($tdir,0755) unless ( -d $tdir );
192 my $fullpath = $self->{-fullpath};
195 if (open($T,'<'.$fullpath.'/.title')) {
197 $title =~ s/[\r\n]*$//;
200 if ($asktitle || (!$title && !$noasktitle)) {
201 my $prompt = $self->{-base};
202 $prompt = '/' unless ($prompt);
203 my $OUT = $term->OUT || \*STDOUT;
204 print $OUT "Enter title for $fullpath\n";
205 $title = $term->readline($prompt.' >',$title);
206 $term->addhistory($title) if ($title);
207 if (open($T,'>'.$fullpath.'/.title')) {
208 print $T $title,"\n";
213 $title=substr($fullpath,length($self->{-root}))
215 $self->{-title}=$title;
216 print "title in $fullpath is $title\n" if ($debug);
219 ######################################################################
221 ######################################################################
228 $dn .= "/".$dir if ($dir);
230 warn "not a directory: $dn";
234 unless (opendir($D,$dn)) {
235 warn "cannot opendir $dn: $!";
239 # recurse into subdirectories BEFORE opening index file
241 &iteratedir($D,$start,$dir,sub {
242 my ($start,$dir,$base)=@_;
244 $ndir .= "/" if ($ndir);
246 return unless ( -d $start."/".$ndir );
247 &processdir($start,$ndir);
252 my $title=&gettitle($dn,$dir);
256 my $inc=&getinclude($dn);
258 # generate directory index unless suppressed
260 if ( -e $dn."/.noindex" ) {
261 open(STDOUT,">/dev/null");
263 open(STDOUT,">".$dn."/index.html");
268 print start_html(-title => $title,
269 -style=>{-src=>[$inc."gallery.css",
270 $inc."lightbox.css"]},
271 -script=>[{-code=>"var incPrefix='$inc';"},
272 {-src=>$inc."gallery.js"},
273 {-src=>$inc."lightbox.js"}]),"\n";
274 print a({-href=>"../index.html"},"UP");
275 print start_center,"\n";
276 print h1($title),"\n";
278 # create list of sub-albums
281 &iteratedir($D,$start,$dir,sub {
282 my ($start,$dir,$base)=@_;
283 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
284 return unless ( -d $en );
285 unless ($hassubdirs) {
286 print hr,h2("Albums"),start_table,"\n";
289 &subalbum($base,&gettitle($en,$dir."/".$base));
291 print end_table,hr,"\n" if ($hassubdirs);
293 # create picture gallery
299 &iteratedir($D,$start,$dir,sub {
300 my ($start,$dir,$base)=@_;
301 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
302 return unless ( -f $en );
303 my $info = image_info($en);
304 if (my $error = $info->{error}) {
305 if (($error !~ "Unrecognized file format") &&
306 ($error !~ "Can't read head")) {
307 print STDERR "File \"$en\": $error\n";
311 if (&processfile($start,$dir,$base,$en,$info)) {
313 push(@piclist,$base);
314 push(@infolist,$info);
320 print br({-clear=>"all"}),"\n";
321 print a({-href=>".html/".$piclist[0]."-slide.html"},"Slideshow");
322 print hr,"\n" if ($haspics);
323 print end_center,"\n";
329 # generate html files for slideshow from @piclist
331 for (my $i=0;$i<=$#piclist;$i++) {
332 my $base=$piclist[$i];
335 $pbase=$piclist[$i-1] if ($i>0);
336 $nbase=$piclist[$i+1] if ($i<$#piclist);
337 for my $refresh('static','slide') {
338 &mkauxfile($start,$dir,$pbase,$base,$nbase,
339 $refresh,$infolist[$i]);
345 #############################################################
347 #############################################################
350 my ($D,$start,$dir,$prog)=@_;
352 while (my $de=readdir($D)) {
353 next if ($de =~ /^\./);
356 foreach my $de(sort @list) {
357 &$prog($start,$dir,$de);
367 #print STDERR "start include ",$dn."/".$str.".include","\n";
368 while ( ! -d $dn."/".$str.".include" ) {
369 #print STDERR "not include ",$dn."/".$str.".include","\n";
371 last unless ($depth--);
373 #print STDERR "end include ",$dn."/".$str.".include","\n";
374 if ( -d $dn."/".$str.".include" ) {
375 #print STDERR "return include ".$str.".include/".$fn,"\n";
376 return $str.".include/";
378 return ""; # won't work anyway but return something
387 if (open($F,"<".$dir."/.title")) {
392 print STDERR "enter title for $dir\n";
394 if ($str =~ /^\s*$/) {
397 if (open($F,">".$dir."/.title")) {
401 print STDERR "cant open .title in $dir for writing: $!";
408 my ($base,$title)=@_;
410 print Tr({-bgcolor=>"#c0c0c0"},
411 td(a({-href=>$base."/index.html"},$base)),
412 td(a({-href=>$base."/index.html"},$title))),"\n";
416 my ($start,$dir,$base,$fn,$info)=@_;
418 my ($w,$h) = dim($info);
419 my $title=$info->{'Comment'};
420 $title=$base unless ($title);
421 my $thumb=&scale($start,$dir,$base,$fn,160,$info);
422 my $medium=&scale($start,$dir,$base,$fn,640,$info);
423 print &infobox($info,$base,$fn),"\n";
424 print table({-class=>'slide'},Tr(td(
425 a({-href=>".html/$base-info.html",
426 -onClick=>"return showIbox('$base');"},$title),
428 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
429 img({-src=>$thumb})),
431 a({-href=>$base},"($w x $h)"),
437 my ($info,$base,$fn)=@_;
454 my $msg=start_div({-class=>'ibox',-id=>$base,-OnClick=>"HideIbox('$base');"});
455 $msg.=span({-style=>'float: left;'},"Info for $base").
456 span({-style=>'float: right;'},
457 a({-href=>"#",-OnClick=>"HideIbox('$base');"},"Close"));
458 $msg.=br({-clear=>'all'});
460 foreach my $k(@infokeys) {
461 $msg.=Tr(td($k.":"),td($info->{$k}));
469 my ($start,$dir,$pbase,$base,$nbase,$refresh,$info) =@_;
470 my $en=sprintf("%s/%s/.html/%s-%s.html",$start,$dir,$base,$refresh);
474 $pref=sprintf("%s-%s.html",$pbase,$refresh);
476 $pref="../index.html";
479 $nref=sprintf("%s-%s.html",$nbase,$refresh);
481 $nref="../index.html";
485 if ($refresh eq 'slide') {
487 $toggleref=sprintf("%s-static.html",$base);
490 $toggleref=sprintf("%s-slide.html",$base);
493 my $tdir=sprintf "%s/%s/.html",$start,$dir;
494 mkdir($tdir,0755) unless ( -d $tdir );
496 unless (open(STDOUT,">".$en)) {
497 warn "cannot open $en: $!";
500 my $title=$info->{'Comment'};
501 $title=$base unless ($title);
502 if ($refresh eq 'slide') {
503 print start_html(-title=>$title,
505 -head=>meta({-http_equiv=>'Refresh',
506 -content=>"3; url=$nref"})),"\n";
508 print start_html(-title=>$title,
509 -bgcolor=>"#808080"),"\n";
511 print start_center,"\n";
513 print a({-href=>"../index.html"},"Index")," | ";
514 print a({-href=>$pref},"<<Prev")," | ";
515 print a({-href=>$toggleref},$toggle)," | ";
516 print a({-href=>$nref},"Next>>");
518 print img({-src=>"../.640/".$base}),"\n";
519 print end_center,"\n";
525 my ($start,$dir,$base,$fn,$tsize,$info)=@_;
526 my ($w,$h) = dim($info);
527 my $max=($w>$h)?$w:$h;
528 my $factor=$tsize/$max;
530 return $base if ($factor >= 1);
532 my $tdir=sprintf "%s/%s/.%s",$start,$dir,$tsize;
533 mkdir($tdir,0755) unless ( -d $tdir );
534 my $tbase=sprintf ".%s/%s",$tsize,$base;
535 my $tfn=sprintf "%s/%s",$tdir,$base;
537 my @tstat=stat($tfn);
538 return $tbase if (@tstat && ($sstat[9] < $tstat[9])); # [9] -> mtime
540 print STDERR "scale by $factor from $fn to $tfn\n";
541 &doscaling($fn,$tfn,$factor,$w,$h);
546 my ($src,$dest,$factor,$w,$h)=@_;
548 my $im=new Image::Magick;
550 #print STDERR "doscale $src -> $dest by $factor\n";
551 $err=$im->Read($src);
553 $im->Scale(width=>$w*$factor,height=>$h*$factor);
554 $err=$im->Write($dest);
555 warn "ImageMagic: write \"$dest\": $err" if ($err);
557 warn "ImageMagic: read \"$src\": $err";
558 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");