5 # Recursively create image gallery index and slideshow wrappings.
6 # Makes use of modified "slideshow" javascript by Samuel Birch
7 # http://www.phatfusion.net/slideshow/
9 # Copyright (c) 2006-2008 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.
31 use POSIX qw/getcwd strftime/;
33 use CGI qw/:html *table *Tr *td *center *div *Link/;
34 use Image::Info qw/image_info dim/;
39 #use encoding 'utf-8';
40 binmode(STDOUT, ":utf8");
42 my $haveimagick = eval { require Image::Magick; };
43 { package Image::Magick; } # to make perl compiler happy
45 my $havefeed = eval { require XML::FeedPP; };
46 { package XML::FeedPP; } # to make perl compiler happy
48 my $havegeoloc = eval { require Image::ExifTool::Location; };
49 { package Image::ExifTool::Location; } # to make perl compiler happy
51 my @sizes = (160, 640, 1600);
52 my $incdir = ".gallery2";
54 ######################################################################
68 'asktitle'=>\$asktitle,
69 'noasktitle'=>\$noasktitle,
75 if ($feed && !$havefeed) {
76 print STDERR "You need to install XML::FeedPP to use --feed\n";
80 my $term = new Term::ReadLine "Edit Title";
82 FsObj->new(getcwd)->iterate;
84 $feedobj->{-feed}->pubDate(time);
85 $feedobj->{-feed}->to_file($feedobj->{-savepath});
90 print STDERR <<__END__;
92 --help: print help message and exit
93 --incpath: do not try to find .gallery2 directory upstream, use
94 specified path (absolute or relavive). Use with causion.
95 --debug: print a lot of debugging info to stdout as you run
96 --asktitle: ask to edit album titles even if there are ".title" files
97 --noasktitle: don't ask to enter album titles even where ".title"
98 files are absent. Use partial directory names as titles.
99 --feed=...: build Atom feed for newly added "albums",
100 enter filename, base URL, and optionally PuSH hub url,
101 separated by commas. (Note: PuSH obviously does not work
102 "out of the box" for static tree! You need a separate
103 "watcher" script to do the publishing for you.)
119 -root=>$parent->{-root},
120 -toppath=>$parent->{-toppath},
121 -depth=>$parent->{-depth}+1,
123 -fullpath=>$parent->{-fullpath}.'/'.$name,
124 -relpath=>$parent->{-relpath}.$name.'/',
125 -inc=>'../'.$parent->{-inc},
134 # fill in -inc, -feed, -relpath
135 initpaths($self); # we are not blessed yet, so cheat.
139 print "new $class:\n";
140 foreach my $k(keys %$self) {
141 print "\t$k\t=\t$self->{$k}\n";
148 my $self=shift; # this is not a method but we cheat
149 my $depth=20; # arbitrary max depth
150 my $fullpath=$self->{-fullpath};
156 $inc .= '/' unless ($inc =~ m%/$%);
159 while ( ! -d $fullpath."/".$inc."/".$incdir ) {
161 last unless ($depth-- > 0);
165 $self->{-inc} = $inc;
168 for ($pos=index($inc,'/');$pos>=0;
169 $pos=index($inc,'/',$pos+1)) {
172 $self->{-depth} = $dp;
173 for ($pos=length($fullpath);$dp>0 && $pos>0;
174 $pos=rindex($fullpath,'/',$pos-1)) {
177 my $relpath = substr($fullpath,$pos);
179 $relpath .= '/' if ($relpath);
180 $self->{-relpath} = $relpath;
181 $self->{-toppath} = substr($fullpath,0,$pos);
182 #print "rel=$relpath, top=$self->{-toppath}, inc=$inc\n";
185 $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
187 $self->{-relpath} = '';
193 my $self=shift; # this is not a method but we cheat
194 my $fullpath=$self->{-fullpath};
195 my $toppath=$self->{-toppath};
196 my $inc=$self->{-inc}.$incdir.'/';
197 my $conffile=$toppath.'/'.$incdir.'/feed.conf';
202 if (open($CONF,">".$conffile)) {
203 print $CONF $feed,"\n";
206 print STDERR "could not open $conffile: $!\n";
209 if (open($CONF,$conffile)) {
217 return unless ($feed);
219 my ($feedfile, $feedbase, $feedhub) = split(',', $feed);
220 $feedbase .= '/' unless ($feedbase =~ /\/$/);
221 print "($feedfile, $feedbase, $feedhub)\n";
223 $feedobj->{-savepath} = $self->{-toppath}.'/'.$feedfile;
224 $feedobj->{-file} = $feedfile;
225 $feedobj->{-base} = $feedbase;
226 $feedobj->{-hub} = $feedhub;
227 $feedobj->{-feed} = XML::FeedPP::Atom::Atom10->new;
228 if ( -f $feedobj->{-file} ) {
229 $feedobj->{-feed}->load($feedobj->{-file});
230 $feedobj->{-feed}->limit_item(15);
232 $feedobj->{-feed}->title("Gallery");
233 $feedobj->{-feed}->description("generated by ".
234 "<a href=\"http://www.average.org/mkgallery/\">".
236 $feedobj->{-feed}->link($feedbase);
237 #$feedobj->{-feed}->copyright("");
238 #$feedobj->{-feed}->language("en");
239 #$feedobj->{-feed}->image($url, $tit, $link, $desc, $w, $h);
241 $feedobj->{-feed}->pubDate(time);
242 $feedobj->{-feed}->to_file($feedobj->{-savepath});
243 $self->{-feed} = $feedobj->{-feed};
248 my $fullpath .= $self->{-fullpath};
249 print "iterate in dir $fullpath\n" if ($debug);
255 unless (opendir($D,$fullpath)) {
256 warn "cannot opendir $fullpath: $!";
259 while (my $de = readdir($D)) {
260 next if ($de =~ /^\./);
261 my $child = $self->new($de);
262 my @stat = stat($child->{-fullpath});
263 $youngest = $stat[9] if ($youngest < $stat[9]);
265 push(@rdirlist,$child);
266 } elsif ($child->isimg) {
267 push(@rimglist,$child);
271 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
272 undef @rdirlist; # inplace sorting would be handy here
273 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
274 undef @rimglist; # optimize away unsorted versions
275 $self->{-firstimg} = $imglist[0];
277 print "Dir: $self->{-fullpath}\n" if ($debug);
279 # 1. first of all, fill title for this directory and create hidden subdirs
283 # 2. recurse into subdirectories to get their titles filled
284 # before we start writing out subalbum list
286 foreach my $dir(@dirlist) {
290 # 3. iterate through images to build cross-links,
293 foreach my $img(@imglist) {
294 # list-linking must be done before generating
295 # aux html because aux pages rely on prev/next refs
297 $previmg->{-nextimg} = $img;
298 $img->{-previmg} = $previmg;
303 # 4. create scaled versions and aux html pages
305 foreach my $img(@imglist) {
306 # scaled versions must be generated before aux html
307 # and main image index because they both rely on
308 # refs to scaled images and they may be just original
309 # images, this is not known before we try scaling.
311 # finally, make aux html pages
315 # no need to go beyond this point if the directory timestamp did not
316 # change since we built index.html file last time.
318 my @istat = stat($self->{-fullpath}.'/index.html');
319 return unless ($youngest > $istat[9]);
321 # 5. start building index.html for the directory
325 # 6. iterate through subdirectories to build subalbums list
329 foreach my $dir(@dirlist) {
335 # 7. iterate through images to build thumb list
339 foreach my $img(@imglist) {
340 print "Img: $img->{-fullpath}\n" if ($debug);
346 # 8. comlplete building index.html for the directory
353 return ( -d $self->{-fullpath} );
358 my $fullpath = $self->{-fullpath};
359 return 0 unless ( -f $fullpath );
362 my $exif = new Image::ExifTool;
363 $exif->ExtractInfo($fullpath);
364 my ($la,$lo) = $exif->GetLocation();
366 $self->{-geoloc} = [$la,$lo];
370 my $info = image_info($fullpath);
371 if (my $error = $info->{error}) {
372 if (($error !~ "Unrecognized file format") &&
373 ($error !~ "Can't read head")) {
374 warn "File \"$fullpath\": $error\n";
379 tryapp12($info) unless ($info->{'ExifVersion'});
382 $self->{-info} = $info;
387 my $info = shift; # this is not a method
389 # dirty hack to take care of Image::Info parser strangeness
390 foreach my $k(keys %$info) {
391 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
393 return unless ($app12); # bad luck
395 foreach my $ln(split /[\r\n]+/,$app12) {
396 $ln =~ s/[[:^print:]\000]/ /g;
397 unless ($seenfirstline) {
402 my ($k,$v)=split /=/,$ln,2;
403 if ($k eq 'TimeDate') {
404 $info->{'DateTime'} =
405 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
407 } elsif ($k eq 'Shutter') {
408 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
409 } elsif ($k eq 'Flash') {
410 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
411 } elsif ($k eq 'Type') {
412 $info->{'Model'} = $v;
413 } elsif ($k eq 'Version') {
414 $info->{'Software'} = $v;
415 } elsif ($k eq 'Fnumber') {
416 $info->{'FNumber'} = $v;
423 my $fullpath = $self->{-fullpath};
424 for my $subdir(@sizes, 'html') {
425 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
426 mkdir($tdir,0755) unless ( -d $tdir );
433 my $fullpath = $self->{-fullpath};
436 if (open($T,'<'.$fullpath.'/.title')) {
437 binmode($T, ":utf8");
439 $title =~ s/[\r\n]*$//;
442 if ($asktitle || (!$title && !$noasktitle)) {
443 my $prompt = $self->{-relpath};
444 $prompt = '/' unless ($prompt);
445 my $OUT = $term->OUT || \*STDOUT;
446 print $OUT "Enter title for $fullpath\n";
447 $title = $term->readline($prompt.' >',$title);
448 $term->addhistory($title) if ($title);
449 if (open($T,'>'.$fullpath.'/.title')) {
450 print $T $title,"\n";
455 $title=$self->{-relpath};
457 $self->{-title}=$title;
458 print "title in $fullpath is $title\n" if ($debug);
463 my $fn = $self->{-fullpath};
464 my $name = $self->{-base};
465 my $dn = $self->{-parent}->{-fullpath};
466 my ($w, $h) = dim($self->{-info});
467 my $max = ($w > $h)?$w:$h;
469 foreach my $size(@sizes) {
470 my $nref = '.'.$size.'/'.$name;
471 my $nfn = $dn.'/'.$nref;
472 my $factor=$size/$max;
474 $self->{$size}->{'url'} = $name; # unscaled version
475 $self->{$size}->{'dim'} = [$w, $h];
477 $self->{$size}->{'url'} = $nref;
478 $self->{$size}->{'dim'} = [int($w*$factor+.5),
480 if (isnewer($fn,$nfn)) {
481 doscaling($fn,$nfn,$factor,$w,$h);
488 my ($fn1,$fn2) = @_; # this is not a method
489 my @stat1=stat($fn1);
490 my @stat2=stat($fn2);
491 return (!@stat2 || ($stat1[9] > $stat2[9]));
492 # true if $fn2 is absent or is older than $fn1
496 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
500 my $im = new Image::Magick;
501 print "doscaling $src -> $dest by $factor\n" if ($debug);
502 if ($err = $im->Read($src)) {
503 warn "ImageMagick: read \"$src\": $err";
505 $im->Scale(width=>$w*$factor,height=>$h*$factor);
506 $err=$im->Write($dest);
507 warn "ImageMagick: write \"$dest\": $err" if ($err);
511 if ($err) { # fallback to command-line tools
512 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
518 my $name = $self->{-base};
519 my $dn = $self->{-parent}->{-fullpath};
520 my $pref = $self->{-previmg}->{-base};
521 my $nref = $self->{-nextimg}->{-base};
522 my $inc = $self->{-inc}.$incdir.'/';
523 my $title = $self->{-info}->{'Comment'};
524 $title = $name unless ($title);
526 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
529 for my $refresh('static', 'slide') {
530 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
531 if (isnewer($self->{-fullpath},$fn)) {
532 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
536 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
538 $fwdref = '../index.html';
541 $bakref = sprintf("%s-%s.html",$pref,$refresh);
543 $bakref = '../index.html';
547 if ($refresh eq 'slide') {
548 $toggleref=sprintf("%s-static.html",$name);
549 $toggletext = 'Stop!';
551 $toggleref=sprintf("%s-slide.html",$name);
552 $toggletext = 'Play->';
555 unless (open($F,'>'.$fn)) {
556 warn "cannot open \"$fn\": $!";
559 binmode($F, ":utf8");
560 if ($refresh eq 'slide') {
565 -head=>meta({-http_equiv=>'Refresh',
566 -content=>"3; url=$fwdref"}),
567 -style=>{-src=>$inc."gallery.css"},
569 comment("Created by ".$version),"\n";
572 print $F start_html(-title=>$title,
575 -style=>{-src=>$inc."gallery.css"},
577 comment("Created by ".$version),"\n";
579 print $F start_table({-class=>'navi'}),start_Tr,"\n",
580 td(a({-href=>"../index.html"},"Index")),"\n",
581 td(a({-href=>$bakref},"<<Prev")),"\n",
582 td(a({-href=>$toggleref},$toggletext)),"\n",
583 td(a({-href=>$fwdref},"Next>>")),"\n",
584 td({-class=>'title'},$title),"\n",
587 center(table({-class=>'picframe'},
588 Tr(td(img({-src=>$imgsrc,
589 -class=>'standalone',
590 -alt=>$title}))))),"\n",
597 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
598 if (isnewer($self->{-fullpath},$fn)) {
600 unless (open($F,'>'.$fn)) {
601 warn "cannot open \"$fn\": $!";
604 binmode($F, ":utf8");
605 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
606 print $F start_html(-title=>$title,
608 -style=>{-src=>$inc."gallery.css"},
610 {-src=>$inc."mootools.js"},
611 {-src=>$inc."urlparser.js"},
612 {-src=>$inc."infopage.js"},
614 comment("Created by ".$version),"\n",
617 table({-class=>'ipage'},
618 Tr(td(img({-src=>$imgsrc,
621 td($self->infotable))),
622 a({-href=>'../index.html',-class=>'conceal'},
632 my $fn = $self->{-fullpath}.'/index.html';
633 my $block = $self->{-fullpath}.'/.noindex';
634 $fn = '/dev/null' if ( -f $block );
636 unless (open($IND,'>'.$fn)) {
637 warn "cannot open $fn: $!";
640 binmode($IND, ":utf8");
641 $self->{-IND} = $IND;
643 my $inc = $self->{-inc}.$incdir.'/';
644 my $title = $self->{-title};
647 $feedlink=Link({-rel=>'alternate',
648 -type=>'application/atom+xml',
649 -title=>'Gallery Feed',
650 -href=>$feedobj->{-base}.$feedobj->{-file}});
652 print $IND start_html(-title => $title,
656 {-src=>$inc."gallery.css"},
657 {-src=>$inc."custom.css"},
660 {-src=>$inc."mootools.js"},
661 {-src=>$inc."overlay.js"},
662 {-src=>$inc."urlparser.js"},
663 {-src=>$inc."multibox.js"},
664 {-src=>$inc."showwin.js"},
665 {-src=>$inc."controls.js"},
666 {-src=>$inc."show.js"},
667 {-src=>$inc."gallery.js"},
669 comment("Created by ".$version),"\n",
670 start_div({-class => 'indexContainer',
671 -id => 'indexContainer'}),
674 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/header.pl')) {
681 -version => $version,
682 -depth => $self->{-depth},
684 -path => $self->{-fullpath},
685 -breadcrumbs => "breadcrumbs unimplemented",
687 print $IND eval $prm,"\n";
689 print STDERR "could not open ",
690 $self->{-toppath}.'/'.$incdir.'/header.pl',
691 " ($!), reverting to default header";
692 print $IND a({-href=>"../index.html"},"UP"),"\n",
693 h1({-class=>'title'},$title),"\n",
699 my $IND = $self->{-IND};
703 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/footer.pl')) {
710 -version => $version,
711 -depth => $self->{-depth},
712 -title => $self->{-title},
713 -breadcrumbs => "breadcrumbs unimplemented",
715 print $IND eval $prm,"\n";
717 print STDERR "could not open ",
718 $self->{-toppath}.'/'.$incdir.'/footer.pl',
719 " ($!), reverting to default empty footer";
721 print $IND end_html,"\n";
723 close($IND) if ($IND);
726 my $feedtitle=sprintf "%s [%d images, %d subalbums]",
730 my $feedlink=$feedobj->{-feed}->link.
731 $self->{-relpath}."index.html";
735 UUID::unparse($uu, $us);
736 $feedobj->{-feed}->add_item(
737 title => $self->{-title},
739 description => $feedtitle,
748 my $IND = $self->{-IND};
750 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
755 my $IND = $self->{-parent}->{-IND};
756 my $name = $self->{-base};
757 my $title = $self->{-title};
759 $self->{-parent}->{-numofsubs}++;
760 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
761 td(a({-href=>$name.'/index.html'},$title))),"\n";
766 my $IND = $self->{-IND};
768 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
773 my $IND = $self->{-IND};
774 my $first = $self->{-firstimg}->{-base};
775 my $slideref = sprintf(".html/%s-slide.html",$first);
777 print $IND h2({-class=>"ititle"},"Images ",
778 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
779 '> slideshow')),"\n";
784 my $IND = $self->{-parent}->{-IND};
785 my $name = $self->{-base};
786 my $title = $self->{-info}->{'Comment'};
787 $title = $name unless ($title);
788 my $thumb = $self->{$sizes[0]}->{'url'};
789 my $info = $self->{-info};
790 my ($w, $h) = dim($info);
792 my $i=0+$self->{-parent}->{-numofimgs};
793 $self->{-parent}->{-numofimgs}++;
795 print $IND a({-name=>$name}),"\n",
796 start_table({-class=>'slide'}),start_Tr,start_td,"\n";
797 print $IND div({-class=>'slidetitle'},
798 "\n ",a({-href=>".html/$name-info.html",
799 -title=>'Image Info: '.$name,
802 start_div({-class=>'slideimage'});
803 if ($self->{-geoloc}) {
804 my ($la,$lo) = @{$self->{-geoloc}};
805 print $IND a({-href=>"http://maps.google.com/".
806 "?q=$la,$lo&ll=$la,$lo",
809 div({-class=>'geoloc'},"")),"\n";
811 print $IND a({-href=>".html/$name-static.html",
817 -alt=>$title})),"\n",end_div,
818 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
819 foreach my $sz(@sizes) {
820 my $src=$self->{$sz}->{'url'};
821 my $w=$self->{$sz}->{'dim'}->[0];
822 my $h=$self->{$sz}->{'dim'}->[1];
823 print $IND " ",a({-href=>$src,
826 -title=>"Reduced to ".$w."x".$h},
829 print $IND " ",a({-href=>$name,
831 -title=>'Original'},$w."x".$h),
833 end_td,end_Tr,end_table,"\n";
838 my $IND = $self->{-IND};
840 print $IND br({-clear=>'all'}),hr,"\n\n";
845 my $info = $self->{-info};
862 $msg.=start_table({-class=>'infotable'})."\n";
863 foreach my $k(@infokeys) {
864 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
866 $msg.=end_table."\n";