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 print "Dir: $self->{-fullpath}\n" if ($debug);
135 # 1. first of all, fill title for this directory and create hidden subdirs
139 # 2. recurse into subdirectories to get their titles filled
140 # before we start writing out subalbum list
142 foreach my $dir(@dirlist) {
146 # 3. iterate through images to build cross-links,
147 # create scaled versions and aux htmls
150 foreach my $img(@imglist) {
152 $previmg->{-nextimg} = $img;
153 $img->{-previmg} = $previmg;
161 # 4. start building index.html for the directory
165 # 5. iterate through subdirectories to build subalbums list
169 foreach my $dir(@dirlist) {
175 # 6. iterate through images to build thumb list
179 foreach my $img(@imglist) {
180 print "Img: $img->{-fullpath}\n" if ($debug);
186 # 7. comlplete building index.html for the directory
193 return ( -d $self->{-fullpath} );
198 my $fullpath = $self->{-fullpath};
199 return 0 unless ( -f $fullpath );
200 my $info = image_info($fullpath);
201 if (my $error = $info->{error}) {
202 if (($error !~ "Unrecognized file format") &&
203 ($error !~ "Can't read head")) {
204 warn "File \"$fullpath\": $error\n";
209 $self->{-info} = $info;
215 my $fullpath = $self->{-fullpath};
216 for my $subdir(@sizes, 'html') {
217 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
218 mkdir($tdir,0755) unless ( -d $tdir );
225 my $fullpath = $self->{-fullpath};
228 if (open($T,'<'.$fullpath.'/.title')) {
230 $title =~ s/[\r\n]*$//;
233 if ($asktitle || (!$title && !$noasktitle)) {
234 my $prompt = $self->{-base};
235 $prompt = '/' unless ($prompt);
236 my $OUT = $term->OUT || \*STDOUT;
237 print $OUT "Enter title for $fullpath\n";
238 $title = $term->readline($prompt.' >',$title);
239 $term->addhistory($title) if ($title);
240 if (open($T,'>'.$fullpath.'/.title')) {
241 print $T $title,"\n";
246 $title=substr($fullpath,length($self->{-root}));
248 $self->{-title}=$title;
249 print "title in $fullpath is $title\n" if ($debug);
254 my $fn = $self->{-fullpath};
255 my $name = $self->{-base};
256 my $dn = $self->{-parent}->{-fullpath};
257 my ($w, $h) = dim($self->{-info});
258 my $max = ($w > $h)?$w:$h;
260 foreach my $size(@sizes) {
261 my $nfn = $dn.'/.'.$size.'/'.$name;
262 my $factor=$size/$max;
268 my $fn = $self->{-fullpath};
269 my $name = $self->{-base};
270 my $dn = $self->{-parent}->{-fullpath};
275 my $fn = $self->{-fullpath}.'/index.html';
277 unless (open($IND,'>'.$fn)) {
278 warn "cannot open $fn: $!";
281 $self->{-IND} = $IND;
283 my $inc = $self->{-inc};
284 my $title = $self->{-title};
285 print $IND start_html(-title => $title,
286 -style=>{-src=>[$inc."gallery.css",
287 $inc."lightbox.css"]},
288 -script=>[{-code=>"var incPrefix='$inc';"},
289 {-src=>$inc."gallery.js"},
290 {-src=>$inc."lightbox.js"}]),
291 a({-href=>"../index.html"},"UP"),"\n",
299 my $IND = $self->{-IND};
301 print $IND end_center,end_html,"\n";
303 close($IND) if ($IND);
309 my $IND = $self->{-IND};
311 print $IND h2("Albums"),"\n",start_table,"\n";
316 my $IND = $self->{-parent}->{-IND};
317 my $name = $self->{-base};
318 my $title = $self->{-title};
320 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
321 td(a({-href=>$name.'/index.html'},$title))),"\n";
326 my $IND = $self->{-IND};
328 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
333 my $IND = $self->{-IND};
335 print $IND h2("Images"),"\n";
340 my $IND = $self->{-parent}->{-IND};
341 my $name = $self->{-base};
343 print $IND a({-href=>$name},$name),"\n";
348 my $IND = $self->{-IND};
350 print $IND br({-clear=>'all'}),hr,"\n\n";
353 ######################################################################
355 ######################################################################
362 $dn .= "/".$dir if ($dir);
364 warn "not a directory: $dn";
368 unless (opendir($D,$dn)) {
369 warn "cannot opendir $dn: $!";
373 # recurse into subdirectories BEFORE opening index file
375 &iteratedir($D,$start,$dir,sub {
376 my ($start,$dir,$base)=@_;
378 $ndir .= "/" if ($ndir);
380 return unless ( -d $start."/".$ndir );
381 &processdir($start,$ndir);
386 my $title=&gettitle($dn,$dir);
390 my $inc=&getinclude($dn);
392 # generate directory index unless suppressed
394 if ( -e $dn."/.noindex" ) {
395 open(STDOUT,">/dev/null");
397 open(STDOUT,">".$dn."/index.html");
402 -style=>{-src=>[$inc."gallery.css",
403 $inc."lightbox.css"]},
404 -script=>[{-code=>"var incPrefix='$inc';"},
405 {-src=>$inc."gallery.js"},
406 {-src=>$inc."lightbox.js"}]),"\n";
407 print a({-href=>"../index.html"},"UP");
408 print start_center,"\n";
409 print h1($title),"\n";
411 # create list of sub-albums
414 &iteratedir($D,$start,$dir,sub {
415 my ($start,$dir,$base)=@_;
416 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
417 return unless ( -d $en );
418 unless ($hassubdirs) {
419 print hr,h2("Albums"),start_table,"\n";
422 &subalbum($base,&gettitle($en,$dir."/".$base));
424 print end_table,hr,"\n" if ($hassubdirs);
426 # create picture gallery
432 &iteratedir($D,$start,$dir,sub {
433 my ($start,$dir,$base)=@_;
434 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
435 return unless ( -f $en );
436 my $info = image_info($en);
437 if (my $error = $info->{error}) {
438 if (($error !~ "Unrecognized file format") &&
439 ($error !~ "Can't read head")) {
440 print STDERR "File \"$en\": $error\n";
444 if (&processfile($start,$dir,$base,$en,$info)) {
446 push(@piclist,$base);
447 push(@infolist,$info);
453 print br({-clear=>"all"}),"\n";
454 print a({-href=>".html/".$piclist[0]."-slide.html"},"Slideshow");
455 print hr,"\n" if ($haspics);
456 print end_center,"\n";
462 # generate html files for slideshow from @piclist
464 for (my $i=0;$i<=$#piclist;$i++) {
465 my $base=$piclist[$i];
468 $pbase=$piclist[$i-1] if ($i>0);
469 $nbase=$piclist[$i+1] if ($i<$#piclist);
470 for my $refresh('static','slide') {
471 &mkauxfile($start,$dir,$pbase,$base,$nbase,
472 $refresh,$infolist[$i]);
478 #############################################################
480 #############################################################
483 my ($D,$start,$dir,$prog)=@_;
485 while (my $de=readdir($D)) {
486 next if ($de =~ /^\./);
489 foreach my $de(sort @list) {
490 &$prog($start,$dir,$de);
500 #print STDERR "start include ",$dn."/".$str.".include","\n";
501 while ( ! -d $dn."/".$str.".include" ) {
502 #print STDERR "not include ",$dn."/".$str.".include","\n";
504 last unless ($depth--);
506 #print STDERR "end include ",$dn."/".$str.".include","\n";
507 if ( -d $dn."/".$str.".include" ) {
508 #print STDERR "return include ".$str.".include/".$fn,"\n";
509 return $str.".include/";
511 return ""; # won't work anyway but return something
520 if (open($F,"<".$dir."/.title")) {
525 print STDERR "enter title for $dir\n";
527 if ($str =~ /^\s*$/) {
530 if (open($F,">".$dir."/.title")) {
534 print STDERR "cant open .title in $dir for writing: $!";
541 my ($base,$title)=@_;
543 print Tr({-bgcolor=>"#c0c0c0"},
544 td(a({-href=>$base."/index.html"},$base)),
545 td(a({-href=>$base."/index.html"},$title))),"\n";
549 my ($start,$dir,$base,$fn,$info)=@_;
551 my ($w,$h) = dim($info);
552 my $title=$info->{'Comment'};
553 $title=$base unless ($title);
554 my $thumb=&scale($start,$dir,$base,$fn,160,$info);
555 my $medium=&scale($start,$dir,$base,$fn,640,$info);
556 print &infobox($info,$base,$fn),"\n";
557 print table({-class=>'slide'},Tr(td(
558 a({-href=>".html/$base-info.html",
559 -onClick=>"return showIbox('$base');"},$title),
561 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
562 img({-src=>$thumb})),
564 a({-href=>$base},"($w x $h)"),
570 my ($info,$base,$fn)=@_;
587 my $msg=start_div({-class=>'ibox',-id=>$base,-OnClick=>"HideIbox('$base');"});
588 $msg.=span({-style=>'float: left;'},"Info for $base").
589 span({-style=>'float: right;'},
590 a({-href=>"#",-OnClick=>"HideIbox('$base');"},"Close"));
591 $msg.=br({-clear=>'all'});
593 foreach my $k(@infokeys) {
594 $msg.=Tr(td($k.":"),td($info->{$k}));
602 my ($start,$dir,$pbase,$base,$nbase,$refresh,$info) =@_;
603 my $en=sprintf("%s/%s/.html/%s-%s.html",$start,$dir,$base,$refresh);
607 $pref=sprintf("%s-%s.html",$pbase,$refresh);
609 $pref="../index.html";
612 $nref=sprintf("%s-%s.html",$nbase,$refresh);
614 $nref="../index.html";
618 if ($refresh eq 'slide') {
620 $toggleref=sprintf("%s-static.html",$base);
623 $toggleref=sprintf("%s-slide.html",$base);
626 my $tdir=sprintf "%s/%s/.html",$start,$dir;
627 mkdir($tdir,0755) unless ( -d $tdir );
629 unless (open(STDOUT,">".$en)) {
630 warn "cannot open $en: $!";
633 my $title=$info->{'Comment'};
634 $title=$base unless ($title);
635 if ($refresh eq 'slide') {
636 print start_html(-title=>$title,
638 -head=>meta({-http_equiv=>'Refresh',
639 -content=>"3; url=$nref"})),"\n";
641 print start_html(-title=>$title,
642 -bgcolor=>"#808080"),"\n";
644 print start_center,"\n";
646 print a({-href=>"../index.html"},"Index")," | ";
647 print a({-href=>$pref},"<<Prev")," | ";
648 print a({-href=>$toggleref},$toggle)," | ";
649 print a({-href=>$nref},"Next>>");
651 print img({-src=>"../.640/".$base}),"\n";
652 print end_center,"\n";
658 my ($start,$dir,$base,$fn,$tsize,$info)=@_;
659 my ($w,$h) = dim($info);
660 my $max=($w>$h)?$w:$h;
661 my $factor=$tsize/$max;
663 return $base if ($factor >= 1);
665 my $tdir=sprintf "%s/%s/.%s",$start,$dir,$tsize;
666 mkdir($tdir,0755) unless ( -d $tdir );
667 my $tbase=sprintf ".%s/%s",$tsize,$base;
668 my $tfn=sprintf "%s/%s",$tdir,$base;
670 my @tstat=stat($tfn);
671 return $tbase if (@tstat && ($sstat[9] < $tstat[9])); # [9] -> mtime
673 print STDERR "scale by $factor from $fn to $tfn\n";
674 &doscaling($fn,$tfn,$factor,$w,$h);
679 my ($src,$dest,$factor,$w,$h)=@_;
681 my $im=new Image::Magick;
683 #print STDERR "doscale $src -> $dest by $factor\n";
684 $err=$im->Read($src);
686 $im->Scale(width=>$w*$factor,height=>$h*$factor);
687 $err=$im->Write($dest);
688 warn "ImageMagic: write \"$dest\": $err" if ($err);
690 warn "ImageMagic: read \"$src\": $err";
691 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");