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,
149 foreach my $img(@imglist) {
150 # list-linking must be done before generating
151 # aux html because aux pages rely on prev/next refs
153 $previmg->{-nextimg} = $img;
154 $img->{-previmg} = $previmg;
159 # 4. create scaled versions and aux html pages
161 foreach my $img(@imglist) {
162 # scaled versions must be generated before aux html
163 # and main image index because they both rely on
164 # refs to scaled images and they may be just original
165 # images, this is not known before we try scaling.
167 # finally, make aux html pages
171 # 5. start building index.html for the directory
175 # 6. iterate through subdirectories to build subalbums list
179 foreach my $dir(@dirlist) {
185 # 7. iterate through images to build thumb list
189 foreach my $img(@imglist) {
190 print "Img: $img->{-fullpath}\n" if ($debug);
196 # 8. comlplete building index.html for the directory
203 return ( -d $self->{-fullpath} );
208 my $fullpath = $self->{-fullpath};
209 return 0 unless ( -f $fullpath );
210 my $info = image_info($fullpath);
211 if (my $error = $info->{error}) {
212 if (($error !~ "Unrecognized file format") &&
213 ($error !~ "Can't read head")) {
214 warn "File \"$fullpath\": $error\n";
219 $self->{-info} = $info;
225 my $fullpath = $self->{-fullpath};
226 for my $subdir(@sizes, 'html') {
227 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
228 mkdir($tdir,0755) unless ( -d $tdir );
235 my $fullpath = $self->{-fullpath};
238 if (open($T,'<'.$fullpath.'/.title')) {
240 $title =~ s/[\r\n]*$//;
243 if ($asktitle || (!$title && !$noasktitle)) {
244 my $prompt = $self->{-base};
245 $prompt = '/' unless ($prompt);
246 my $OUT = $term->OUT || \*STDOUT;
247 print $OUT "Enter title for $fullpath\n";
248 $title = $term->readline($prompt.' >',$title);
249 $term->addhistory($title) if ($title);
250 if (open($T,'>'.$fullpath.'/.title')) {
251 print $T $title,"\n";
256 $title=substr($fullpath,length($self->{-root}));
258 $self->{-title}=$title;
259 print "title in $fullpath is $title\n" if ($debug);
264 my $fn = $self->{-fullpath};
265 my $name = $self->{-base};
266 my $dn = $self->{-parent}->{-fullpath};
267 my ($w, $h) = dim($self->{-info});
268 my $max = ($w > $h)?$w:$h;
270 foreach my $size(@sizes) {
271 my $nref = '.'.$size.'/'.$name;
272 my $nfn = $dn.'/'.$nref;
273 my $factor=$size/$max;
275 $self->{$size} = $name; # unscaled version will do
277 $self->{$size} = $nref;
278 if (isnewer($fn,$nfn)) {
279 doscaling($fn,$nfn,$factor,$w,$h);
286 my ($fn1,$fn2) = @_; # this is not a method
287 my @stat1=stat($fn1);
288 my @stat2=stat($fn2);
289 return (!@stat2 || ($stat1[9] > $stat2[9]));
290 # true if $fn2 is absent or is older than $fn1
294 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
295 my $im = new Image::Magick;
297 print "doscaling $src -> $dest by $factor\n" if ($debug);
298 $err = $im->Read($src);
300 $im->Scale(width=>$w*$factor,height=>$h*$factor);
301 $err=$im->Write($dest);
302 warn "ImageMagick: write \"$dest\": $err" if ($err);
303 } else { # fallback to command-line tools
304 warn "ImageMagick: read \"$src\": $err";
305 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
312 my $name = $self->{-base};
313 my $dn = $self->{-parent}->{-fullpath};
314 my $pref = $self->{-previmg}->{-base};
315 my $nref = $self->{-nextimg}->{-base};
316 my $inc = $self->{-inc};
317 my $title = $self->{-info}->{'Comment'};
318 $title = $name unless ($title);
320 print "slide: \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
323 for my $refresh('static', 'slide') {
324 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
325 my $bakref = sprintf("%s-%s.html",$pref,$refresh);
326 my $fwdref = sprintf("%s-%s.html",$nref,$refresh);
327 my $imgsrc = sprintf("../.%s/%s",$sizes[1],$name);
330 if ($refresh eq 'slide') {
331 $toggleref=sprintf("%s-static.html",$name);
332 $toggletext = 'Stop!';
334 $toggleref=sprintf("%s-slide.html",$name);
335 $toggletext = 'Play->';
338 unless (open($F,'>'.$fn)) {
339 warn "cannot open \"$fn\": $!";
342 if ($refresh eq 'slide') {
343 print $F start_html(-title=>$title,
345 -head=>meta({-http_equiv=>'Refresh',
346 -content=>"3; url=$fwdref"}),
347 -style=>{-src=>$inc."gallery.css"},
351 print $F start_html(-title=>$title,
353 -style=>{-src=>$inc."gallery.css"},
356 print $F start_center,"\n",
358 a({-href=>"../index.html"},"Index")," | ",
359 a({-href=>$bakref},"<<Prev")," | ",
360 a({-href=>$toggleref},$toggletext)," | ",
361 a({-href=>$fwdref},"Next>>"),
363 img({-src=>$imgsrc}),"\n",
372 my $fn = $self->{-fullpath}.'/index.html';
374 unless (open($IND,'>'.$fn)) {
375 warn "cannot open $fn: $!";
378 $self->{-IND} = $IND;
380 my $inc = $self->{-inc};
381 my $title = $self->{-title};
382 print $IND start_html(-title => $title,
383 -style=>{-src=>[$inc."gallery.css",
384 $inc."lightbox.css"]},
385 -script=>[{-code=>"var incPrefix='$inc';"},
386 {-src=>$inc."gallery.js"},
387 {-src=>$inc."lightbox.js"}]),
388 a({-href=>"../index.html"},"UP"),"\n",
396 my $IND = $self->{-IND};
398 print $IND end_center,end_html,"\n";
400 close($IND) if ($IND);
406 my $IND = $self->{-IND};
408 print $IND h2("Albums"),"\n",start_table,"\n";
413 my $IND = $self->{-parent}->{-IND};
414 my $name = $self->{-base};
415 my $title = $self->{-title};
417 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
418 td(a({-href=>$name.'/index.html'},$title))),"\n";
423 my $IND = $self->{-IND};
425 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
430 my $IND = $self->{-IND};
432 print $IND h2("Images"),"\n";
437 my $IND = $self->{-parent}->{-IND};
438 my $name = $self->{-base};
440 print $IND a({-href=>$name},$name),"\n";
445 my $IND = $self->{-IND};
447 print $IND br({-clear=>'all'}),hr,"\n\n";
450 ######################################################################
452 ######################################################################
459 $dn .= "/".$dir if ($dir);
461 warn "not a directory: $dn";
465 unless (opendir($D,$dn)) {
466 warn "cannot opendir $dn: $!";
470 # recurse into subdirectories BEFORE opening index file
472 &iteratedir($D,$start,$dir,sub {
473 my ($start,$dir,$base)=@_;
475 $ndir .= "/" if ($ndir);
477 return unless ( -d $start."/".$ndir );
478 &processdir($start,$ndir);
483 my $title=&gettitle($dn,$dir);
487 my $inc=&getinclude($dn);
489 # generate directory index unless suppressed
491 if ( -e $dn."/.noindex" ) {
492 open(STDOUT,">/dev/null");
494 open(STDOUT,">".$dn."/index.html");
499 -style=>{-src=>[$inc."gallery.css",
500 $inc."lightbox.css"]},
501 -script=>[{-code=>"var incPrefix='$inc';"},
502 {-src=>$inc."gallery.js"},
503 {-src=>$inc."lightbox.js"}]),"\n";
504 print a({-href=>"../index.html"},"UP");
505 print start_center,"\n";
506 print h1($title),"\n";
508 # create list of sub-albums
511 &iteratedir($D,$start,$dir,sub {
512 my ($start,$dir,$base)=@_;
513 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
514 return unless ( -d $en );
515 unless ($hassubdirs) {
516 print hr,h2("Albums"),start_table,"\n";
519 &subalbum($base,&gettitle($en,$dir."/".$base));
521 print end_table,hr,"\n" if ($hassubdirs);
523 # create picture gallery
529 &iteratedir($D,$start,$dir,sub {
530 my ($start,$dir,$base)=@_;
531 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
532 return unless ( -f $en );
533 my $info = image_info($en);
534 if (my $error = $info->{error}) {
535 if (($error !~ "Unrecognized file format") &&
536 ($error !~ "Can't read head")) {
537 print STDERR "File \"$en\": $error\n";
541 if (&processfile($start,$dir,$base,$en,$info)) {
543 push(@piclist,$base);
544 push(@infolist,$info);
550 print br({-clear=>"all"}),"\n";
551 print a({-href=>".html/".$piclist[0]."-slide.html"},"Slideshow");
552 print hr,"\n" if ($haspics);
553 print end_center,"\n";
559 # generate html files for slideshow from @piclist
561 for (my $i=0;$i<=$#piclist;$i++) {
562 my $base=$piclist[$i];
565 $pbase=$piclist[$i-1] if ($i>0);
566 $nbase=$piclist[$i+1] if ($i<$#piclist);
567 for my $refresh('static','slide') {
568 &mkauxfile($start,$dir,$pbase,$base,$nbase,
569 $refresh,$infolist[$i]);
575 #############################################################
577 #############################################################
580 my ($D,$start,$dir,$prog)=@_;
582 while (my $de=readdir($D)) {
583 next if ($de =~ /^\./);
586 foreach my $de(sort @list) {
587 &$prog($start,$dir,$de);
597 #print STDERR "start include ",$dn."/".$str.".include","\n";
598 while ( ! -d $dn."/".$str.".include" ) {
599 #print STDERR "not include ",$dn."/".$str.".include","\n";
601 last unless ($depth--);
603 #print STDERR "end include ",$dn."/".$str.".include","\n";
604 if ( -d $dn."/".$str.".include" ) {
605 #print STDERR "return include ".$str.".include/".$fn,"\n";
606 return $str.".include/";
608 return ""; # won't work anyway but return something
617 if (open($F,"<".$dir."/.title")) {
622 print STDERR "enter title for $dir\n";
624 if ($str =~ /^\s*$/) {
627 if (open($F,">".$dir."/.title")) {
631 print STDERR "cant open .title in $dir for writing: $!";
638 my ($base,$title)=@_;
640 print Tr({-bgcolor=>"#c0c0c0"},
641 td(a({-href=>$base."/index.html"},$base)),
642 td(a({-href=>$base."/index.html"},$title))),"\n";
646 my ($start,$dir,$base,$fn,$info)=@_;
648 my ($w,$h) = dim($info);
649 my $title=$info->{'Comment'};
650 $title=$base unless ($title);
651 my $thumb=&scale($start,$dir,$base,$fn,160,$info);
652 my $medium=&scale($start,$dir,$base,$fn,640,$info);
653 print &infobox($info,$base,$fn),"\n";
654 print table({-class=>'slide'},Tr(td(
655 a({-href=>".html/$base-info.html",
656 -onClick=>"return showIbox('$base');"},$title),
658 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
659 img({-src=>$thumb})),
661 a({-href=>$base},"($w x $h)"),
667 my ($info,$base,$fn)=@_;
684 my $msg=start_div({-class=>'ibox',-id=>$base,-OnClick=>"HideIbox('$base');"});
685 $msg.=span({-style=>'float: left;'},"Info for $base").
686 span({-style=>'float: right;'},
687 a({-href=>"#",-OnClick=>"HideIbox('$base');"},"Close"));
688 $msg.=br({-clear=>'all'});
690 foreach my $k(@infokeys) {
691 $msg.=Tr(td($k.":"),td($info->{$k}));
699 my ($start,$dir,$pbase,$base,$nbase,$refresh,$info) =@_;
700 my $en=sprintf("%s/%s/.html/%s-%s.html",$start,$dir,$base,$refresh);
704 $pref=sprintf("%s-%s.html",$pbase,$refresh);
706 $pref="../index.html";
709 $nref=sprintf("%s-%s.html",$nbase,$refresh);
711 $nref="../index.html";
715 if ($refresh eq 'slide') {
717 $toggleref=sprintf("%s-static.html",$base);
720 $toggleref=sprintf("%s-slide.html",$base);
723 my $tdir=sprintf "%s/%s/.html",$start,$dir;
724 mkdir($tdir,0755) unless ( -d $tdir );
726 unless (open(STDOUT,">".$en)) {
727 warn "cannot open $en: $!";
730 my $title=$info->{'Comment'};
731 $title=$base unless ($title);
732 if ($refresh eq 'slide') {
733 print start_html(-title=>$title,
735 -head=>meta({-http_equiv=>'Refresh',
736 -content=>"3; url=$nref"})),"\n";
738 print start_html(-title=>$title,
739 -bgcolor=>"#808080"),"\n";
741 print start_center,"\n";
743 print a({-href=>"../index.html"},"Index")," | ";
744 print a({-href=>$pref},"<<Prev")," | ";
745 print a({-href=>$toggleref},$toggle)," | ";
746 print a({-href=>$nref},"Next>>");
748 print img({-src=>"../.640/".$base}),"\n";
749 print end_center,"\n";
755 my ($start,$dir,$base,$fn,$tsize,$info)=@_;
756 my ($w,$h) = dim($info);
757 my $max=($w>$h)?$w:$h;
758 my $factor=$tsize/$max;
760 return $base if ($factor >= 1);
762 my $tdir=sprintf "%s/%s/.%s",$start,$dir,$tsize;
763 mkdir($tdir,0755) unless ( -d $tdir );
764 my $tbase=sprintf ".%s/%s",$tsize,$base;
765 my $tfn=sprintf "%s/%s",$tdir,$base;
767 my @tstat=stat($tfn);
768 return $tbase if (@tstat && ($sstat[9] < $tstat[9])); # [9] -> mtime
770 print STDERR "scale by $factor from $fn to $tfn\n";
771 &doscaling($fn,$tfn,$factor,$w,$h);
776 my ($src,$dest,$factor,$w,$h)=@_;
778 my $im=new Image::Magick;
780 #print STDERR "doscale $src -> $dest by $factor\n";
781 $err=$im->Read($src);
783 $im->Scale(width=>$w*$factor,height=>$h*$factor);
784 $err=$im->Write($dest);
785 warn "ImageMagic: write \"$dest\": $err" if ($err);
787 warn "ImageMagic: read \"$src\": $err";
788 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");