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
132 $self->{-firstimg} = $imglist[0];
134 print "Dir: $self->{-fullpath}\n" if ($debug);
136 # 1. first of all, fill title for this directory and create hidden subdirs
140 # 2. recurse into subdirectories to get their titles filled
141 # before we start writing out subalbum list
143 foreach my $dir(@dirlist) {
147 # 3. iterate through images to build cross-links,
150 foreach my $img(@imglist) {
151 # list-linking must be done before generating
152 # aux html because aux pages rely on prev/next refs
154 $previmg->{-nextimg} = $img;
155 $img->{-previmg} = $previmg;
160 # 4. create scaled versions and aux html pages
162 foreach my $img(@imglist) {
163 # scaled versions must be generated before aux html
164 # and main image index because they both rely on
165 # refs to scaled images and they may be just original
166 # images, this is not known before we try scaling.
168 # finally, make aux html pages
172 # 5. start building index.html for the directory
176 # 6. iterate through subdirectories to build subalbums list
180 foreach my $dir(@dirlist) {
186 # 7. iterate through images to build thumb list
190 foreach my $img(@imglist) {
191 print "Img: $img->{-fullpath}\n" if ($debug);
197 # 8. comlplete building index.html for the directory
204 return ( -d $self->{-fullpath} );
209 my $fullpath = $self->{-fullpath};
210 return 0 unless ( -f $fullpath );
211 my $info = image_info($fullpath);
212 if (my $error = $info->{error}) {
213 if (($error !~ "Unrecognized file format") &&
214 ($error !~ "Can't read head")) {
215 warn "File \"$fullpath\": $error\n";
220 $self->{-info} = $info;
226 my $fullpath = $self->{-fullpath};
227 for my $subdir(@sizes, 'html') {
228 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
229 mkdir($tdir,0755) unless ( -d $tdir );
236 my $fullpath = $self->{-fullpath};
239 if (open($T,'<'.$fullpath.'/.title')) {
241 $title =~ s/[\r\n]*$//;
244 if ($asktitle || (!$title && !$noasktitle)) {
245 my $prompt = $self->{-base};
246 $prompt = '/' unless ($prompt);
247 my $OUT = $term->OUT || \*STDOUT;
248 print $OUT "Enter title for $fullpath\n";
249 $title = $term->readline($prompt.' >',$title);
250 $term->addhistory($title) if ($title);
251 if (open($T,'>'.$fullpath.'/.title')) {
252 print $T $title,"\n";
257 $title=substr($fullpath,length($self->{-root}));
259 $self->{-title}=$title;
260 print "title in $fullpath is $title\n" if ($debug);
265 my $fn = $self->{-fullpath};
266 my $name = $self->{-base};
267 my $dn = $self->{-parent}->{-fullpath};
268 my ($w, $h) = dim($self->{-info});
269 my $max = ($w > $h)?$w:$h;
271 foreach my $size(@sizes) {
272 my $nref = '.'.$size.'/'.$name;
273 my $nfn = $dn.'/'.$nref;
274 my $factor=$size/$max;
276 $self->{$size} = $name; # unscaled version will do
278 $self->{$size} = $nref;
279 if (isnewer($fn,$nfn)) {
280 doscaling($fn,$nfn,$factor,$w,$h);
287 my ($fn1,$fn2) = @_; # this is not a method
288 my @stat1=stat($fn1);
289 my @stat2=stat($fn2);
290 return (!@stat2 || ($stat1[9] > $stat2[9]));
291 # true if $fn2 is absent or is older than $fn1
295 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
296 my $im = new Image::Magick;
298 print "doscaling $src -> $dest by $factor\n" if ($debug);
299 $err = $im->Read($src);
301 $im->Scale(width=>$w*$factor,height=>$h*$factor);
302 $err=$im->Write($dest);
303 warn "ImageMagick: write \"$dest\": $err" if ($err);
304 } else { # fallback to command-line tools
305 warn "ImageMagick: read \"$src\": $err";
306 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
313 my $name = $self->{-base};
314 my $dn = $self->{-parent}->{-fullpath};
315 my $pref = $self->{-previmg}->{-base};
316 my $nref = $self->{-nextimg}->{-base};
317 my $inc = $self->{-inc};
318 my $title = $self->{-info}->{'Comment'};
319 $title = $name unless ($title);
321 print "slide: \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
324 for my $refresh('static', 'slide') {
325 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
326 my $imgsrc = sprintf("../.%s/%s",$sizes[1],$name);
330 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
332 $fwdref = '../index.html';
335 $bakref = sprintf("%s-%s.html",$pref,$refresh);
337 $bakref = '../index.html';
341 if ($refresh eq 'slide') {
342 $toggleref=sprintf("%s-static.html",$name);
343 $toggletext = 'Stop!';
345 $toggleref=sprintf("%s-slide.html",$name);
346 $toggletext = 'Play->';
349 unless (open($F,'>'.$fn)) {
350 warn "cannot open \"$fn\": $!";
353 if ($refresh eq 'slide') {
354 print $F start_html(-title=>$title,
356 -head=>meta({-http_equiv=>'Refresh',
357 -content=>"3; url=$fwdref"}),
358 -style=>{-src=>$inc."gallery.css"},
362 print $F start_html(-title=>$title,
364 -style=>{-src=>$inc."gallery.css"},
367 print $F start_center,"\n",
369 a({-href=>"../index.html"},"Index")," | ",
370 a({-href=>$bakref},"<<Prev")," | ",
371 a({-href=>$toggleref},$toggletext)," | ",
372 a({-href=>$fwdref},"Next>>"),
374 img({-src=>$imgsrc}),"\n",
383 my $fn = $self->{-fullpath}.'/index.html';
385 unless (open($IND,'>'.$fn)) {
386 warn "cannot open $fn: $!";
389 $self->{-IND} = $IND;
391 my $inc = $self->{-inc};
392 my $title = $self->{-title};
393 print $IND start_html(-title => $title,
394 -style=>{-src=>[$inc."gallery.css",
395 $inc."lightbox.css"]},
396 -script=>[{-code=>"var incPrefix='$inc';"},
397 {-src=>$inc."gallery.js"},
398 {-src=>$inc."lightbox.js"}]),
399 a({-href=>"../index.html"},"UP"),"\n",
407 my $IND = $self->{-IND};
409 print $IND end_center,end_html,"\n";
411 close($IND) if ($IND);
417 my $IND = $self->{-IND};
419 print $IND h2("Albums"),"\n",start_table,"\n";
424 my $IND = $self->{-parent}->{-IND};
425 my $name = $self->{-base};
426 my $title = $self->{-title};
428 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
429 td(a({-href=>$name.'/index.html'},$title))),"\n";
434 my $IND = $self->{-IND};
436 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
441 my $IND = $self->{-IND};
442 my $first = $self->{-firstimg}->{-base};
443 my $slideref = sprintf(".html/%s-slide.html",$first);
445 print $IND h2("Images"),
446 a({-href=>$slideref},'Slideshow');
452 my $IND = $self->{-parent}->{-IND};
453 my $name = $self->{-base};
454 my $title = $self->{-info}->{'Comment'};
455 $title = $name unless ($title);
456 my $thumb = $self->{$sizes[0]};
457 my $medium = $self->{$sizes[1]};
458 my $info = $self->{-info};
459 my ($w, $h) = dim($info);
461 #print &infobox($info,$base,$fn),"\n";
462 print $IND table({-class=>'slide'},Tr(td(
463 a({-href=>".html/$name-info.html",
464 -onClick=>"return showIbox('$name');"},$title),
466 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
467 img({-src=>$thumb})),
469 a({-href=>$name},"($w x $h)"),
475 my $IND = $self->{-IND};
477 print $IND br({-clear=>'all'}),hr,"\n\n";
480 ######################################################################
482 ######################################################################
489 $dn .= "/".$dir if ($dir);
491 warn "not a directory: $dn";
495 unless (opendir($D,$dn)) {
496 warn "cannot opendir $dn: $!";
500 # recurse into subdirectories BEFORE opening index file
502 &iteratedir($D,$start,$dir,sub {
503 my ($start,$dir,$base)=@_;
505 $ndir .= "/" if ($ndir);
507 return unless ( -d $start."/".$ndir );
508 &processdir($start,$ndir);
513 my $title=&gettitle($dn,$dir);
517 my $inc=&getinclude($dn);
519 # generate directory index unless suppressed
521 if ( -e $dn."/.noindex" ) {
522 open(STDOUT,">/dev/null");
524 open(STDOUT,">".$dn."/index.html");
529 -style=>{-src=>[$inc."gallery.css",
530 $inc."lightbox.css"]},
531 -script=>[{-code=>"var incPrefix='$inc';"},
532 {-src=>$inc."gallery.js"},
533 {-src=>$inc."lightbox.js"}]),"\n";
534 print a({-href=>"../index.html"},"UP");
535 print start_center,"\n";
536 print h1($title),"\n";
538 # create list of sub-albums
541 &iteratedir($D,$start,$dir,sub {
542 my ($start,$dir,$base)=@_;
543 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
544 return unless ( -d $en );
545 unless ($hassubdirs) {
546 print hr,h2("Albums"),start_table,"\n";
549 &subalbum($base,&gettitle($en,$dir."/".$base));
551 print end_table,hr,"\n" if ($hassubdirs);
553 # create picture gallery
559 &iteratedir($D,$start,$dir,sub {
560 my ($start,$dir,$base)=@_;
561 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
562 return unless ( -f $en );
563 my $info = image_info($en);
564 if (my $error = $info->{error}) {
565 if (($error !~ "Unrecognized file format") &&
566 ($error !~ "Can't read head")) {
567 print STDERR "File \"$en\": $error\n";
571 if (&processfile($start,$dir,$base,$en,$info)) {
573 push(@piclist,$base);
574 push(@infolist,$info);
580 print br({-clear=>"all"}),"\n";
581 print a({-href=>".html/".$piclist[0]."-slide.html"},"Slideshow");
582 print hr,"\n" if ($haspics);
583 print end_center,"\n";
589 # generate html files for slideshow from @piclist
591 for (my $i=0;$i<=$#piclist;$i++) {
592 my $base=$piclist[$i];
595 $pbase=$piclist[$i-1] if ($i>0);
596 $nbase=$piclist[$i+1] if ($i<$#piclist);
597 for my $refresh('static','slide') {
598 &mkauxfile($start,$dir,$pbase,$base,$nbase,
599 $refresh,$infolist[$i]);
605 #############################################################
607 #############################################################
610 my ($D,$start,$dir,$prog)=@_;
612 while (my $de=readdir($D)) {
613 next if ($de =~ /^\./);
616 foreach my $de(sort @list) {
617 &$prog($start,$dir,$de);
627 #print STDERR "start include ",$dn."/".$str.".include","\n";
628 while ( ! -d $dn."/".$str.".include" ) {
629 #print STDERR "not include ",$dn."/".$str.".include","\n";
631 last unless ($depth--);
633 #print STDERR "end include ",$dn."/".$str.".include","\n";
634 if ( -d $dn."/".$str.".include" ) {
635 #print STDERR "return include ".$str.".include/".$fn,"\n";
636 return $str.".include/";
638 return ""; # won't work anyway but return something
647 if (open($F,"<".$dir."/.title")) {
652 print STDERR "enter title for $dir\n";
654 if ($str =~ /^\s*$/) {
657 if (open($F,">".$dir."/.title")) {
661 print STDERR "cant open .title in $dir for writing: $!";
668 my ($base,$title)=@_;
670 print Tr({-bgcolor=>"#c0c0c0"},
671 td(a({-href=>$base."/index.html"},$base)),
672 td(a({-href=>$base."/index.html"},$title))),"\n";
676 my ($start,$dir,$base,$fn,$info)=@_;
678 my ($w,$h) = dim($info);
679 my $title=$info->{'Comment'};
680 $title=$base unless ($title);
681 my $thumb=&scale($start,$dir,$base,$fn,160,$info);
682 my $medium=&scale($start,$dir,$base,$fn,640,$info);
683 print &infobox($info,$base,$fn),"\n";
684 print table({-class=>'slide'},Tr(td(
685 a({-href=>".html/$base-info.html",
686 -onClick=>"return showIbox('$base');"},$title),
688 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
689 img({-src=>$thumb})),
691 a({-href=>$base},"($w x $h)"),
697 my ($info,$base,$fn)=@_;
714 my $msg=start_div({-class=>'ibox',-id=>$base,-OnClick=>"HideIbox('$base');"});
715 $msg.=span({-style=>'float: left;'},"Info for $base").
716 span({-style=>'float: right;'},
717 a({-href=>"#",-OnClick=>"HideIbox('$base');"},"Close"));
718 $msg.=br({-clear=>'all'});
720 foreach my $k(@infokeys) {
721 $msg.=Tr(td($k.":"),td($info->{$k}));
729 my ($start,$dir,$pbase,$base,$nbase,$refresh,$info) =@_;
730 my $en=sprintf("%s/%s/.html/%s-%s.html",$start,$dir,$base,$refresh);
734 $pref=sprintf("%s-%s.html",$pbase,$refresh);
736 $pref="../index.html";
739 $nref=sprintf("%s-%s.html",$nbase,$refresh);
741 $nref="../index.html";
745 if ($refresh eq 'slide') {
747 $toggleref=sprintf("%s-static.html",$base);
750 $toggleref=sprintf("%s-slide.html",$base);
753 my $tdir=sprintf "%s/%s/.html",$start,$dir;
754 mkdir($tdir,0755) unless ( -d $tdir );
756 unless (open(STDOUT,">".$en)) {
757 warn "cannot open $en: $!";
760 my $title=$info->{'Comment'};
761 $title=$base unless ($title);
762 if ($refresh eq 'slide') {
763 print start_html(-title=>$title,
765 -head=>meta({-http_equiv=>'Refresh',
766 -content=>"3; url=$nref"})),"\n";
768 print start_html(-title=>$title,
769 -bgcolor=>"#808080"),"\n";
771 print start_center,"\n";
773 print a({-href=>"../index.html"},"Index")," | ";
774 print a({-href=>$pref},"<<Prev")," | ";
775 print a({-href=>$toggleref},$toggle)," | ";
776 print a({-href=>$nref},"Next>>");
778 print img({-src=>"../.640/".$base}),"\n";
779 print end_center,"\n";
785 my ($start,$dir,$base,$fn,$tsize,$info)=@_;
786 my ($w,$h) = dim($info);
787 my $max=($w>$h)?$w:$h;
788 my $factor=$tsize/$max;
790 return $base if ($factor >= 1);
792 my $tdir=sprintf "%s/%s/.%s",$start,$dir,$tsize;
793 mkdir($tdir,0755) unless ( -d $tdir );
794 my $tbase=sprintf ".%s/%s",$tsize,$base;
795 my $tfn=sprintf "%s/%s",$tdir,$base;
797 my @tstat=stat($tfn);
798 return $tbase if (@tstat && ($sstat[9] < $tstat[9])); # [9] -> mtime
800 print STDERR "scale by $factor from $fn to $tfn\n";
801 &doscaling($fn,$tfn,$factor,$w,$h);
806 my ($src,$dest,$factor,$w,$h)=@_;
808 my $im=new Image::Magick;
810 #print STDERR "doscale $src -> $dest by $factor\n";
811 $err=$im->Read($src);
813 $im->Scale(width=>$w*$factor,height=>$h*$factor);
814 $err=$im->Write($dest);
815 warn "ImageMagic: write \"$dest\": $err" if ($err);
817 warn "ImageMagic: read \"$src\": $err";
818 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");