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 if ( -f $feedobj->{-file} ) {
228 $feedobj->{-feed} = XML::FeedPP::Atom::Atom10->new(
230 $feedobj->{-feed}->limit_item(15);
232 $feedobj->{-feed} = XML::FeedPP::Atom::Atom10->new;
233 $feedobj->{-feed}->title("Gallery");
234 $feedobj->{-feed}->description("generated by ".
235 "<a href=\"http://www.average.org/mkgallery/\">".
237 $feedobj->{-feed}->link($feedbase, 'type' => 'text/html', 'rel' => 'alternate');
238 $feedobj->{-feed}->xmlns("xmlns:ostatus" =>
239 "http://ostatus.org/schema/1.0");
240 #$feedobj->{-feed}->copyright("");
241 #$feedobj->{-feed}->language("en");
242 #$feedobj->{-feed}->image($url, $tit, $link, $desc, $w, $h);
244 $self->{-feed} = $feedobj->{-feed};
249 my $fullpath .= $self->{-fullpath};
250 print "iterate in dir $fullpath\n" if ($debug);
256 unless (opendir($D,$fullpath)) {
257 warn "cannot opendir $fullpath: $!";
260 while (my $de = readdir($D)) {
261 next if ($de =~ /^\./);
262 my $child = $self->new($de);
263 my @stat = stat($child->{-fullpath});
264 $youngest = $stat[9] if ($youngest < $stat[9]);
266 push(@rdirlist,$child);
267 } elsif ($child->isimg) {
268 push(@rimglist,$child);
272 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
273 undef @rdirlist; # inplace sorting would be handy here
274 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
275 undef @rimglist; # optimize away unsorted versions
276 $self->{-firstimg} = $imglist[0];
278 print "Dir: $self->{-fullpath}\n" if ($debug);
280 # 1. first of all, fill title for this directory and create hidden subdirs
284 # 2. recurse into subdirectories to get their titles filled
285 # before we start writing out subalbum list
287 foreach my $dir(@dirlist) {
291 # 3. iterate through images to build cross-links,
294 foreach my $img(@imglist) {
295 # list-linking must be done before generating
296 # aux html because aux pages rely on prev/next refs
298 $previmg->{-nextimg} = $img;
299 $img->{-previmg} = $previmg;
304 # 4. create scaled versions and aux html pages
306 foreach my $img(@imglist) {
307 # scaled versions must be generated before aux html
308 # and main image index because they both rely on
309 # refs to scaled images and they may be just original
310 # images, this is not known before we try scaling.
312 # finally, make aux html pages
316 # no need to go beyond this point if the directory timestamp did not
317 # change since we built index.html file last time.
319 my @istat = stat($self->{-fullpath}.'/index.html');
320 return unless ($youngest > $istat[9]);
322 # 5. start building index.html for the directory
326 # 6. iterate through subdirectories to build subalbums list
330 foreach my $dir(@dirlist) {
336 # 7. iterate through images to build thumb list
340 foreach my $img(@imglist) {
341 print "Img: $img->{-fullpath}\n" if ($debug);
347 # 8. comlplete building index.html for the directory
354 return ( -d $self->{-fullpath} );
359 my $fullpath = $self->{-fullpath};
360 return 0 unless ( -f $fullpath );
363 my $exif = new Image::ExifTool;
364 $exif->ExtractInfo($fullpath);
365 my ($la,$lo) = $exif->GetLocation();
367 $self->{-geoloc} = [$la,$lo];
371 my $info = image_info($fullpath);
372 if (my $error = $info->{error}) {
373 if (($error !~ "Unrecognized file format") &&
374 ($error !~ "Can't read head")) {
375 warn "File \"$fullpath\": $error\n";
380 tryapp12($info) unless ($info->{'ExifVersion'});
383 $self->{-info} = $info;
388 my $info = shift; # this is not a method
390 # dirty hack to take care of Image::Info parser strangeness
391 foreach my $k(keys %$info) {
392 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
394 return unless ($app12); # bad luck
396 foreach my $ln(split /[\r\n]+/,$app12) {
397 $ln =~ s/[[:^print:]\000]/ /g;
398 unless ($seenfirstline) {
403 my ($k,$v)=split /=/,$ln,2;
404 if ($k eq 'TimeDate') {
405 $info->{'DateTime'} =
406 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
408 } elsif ($k eq 'Shutter') {
409 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
410 } elsif ($k eq 'Flash') {
411 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
412 } elsif ($k eq 'Type') {
413 $info->{'Model'} = $v;
414 } elsif ($k eq 'Version') {
415 $info->{'Software'} = $v;
416 } elsif ($k eq 'Fnumber') {
417 $info->{'FNumber'} = $v;
424 my $fullpath = $self->{-fullpath};
425 for my $subdir(@sizes, 'html') {
426 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
427 mkdir($tdir,0755) unless ( -d $tdir );
434 my $fullpath = $self->{-fullpath};
437 if (open($T,'<'.$fullpath.'/.title')) {
438 binmode($T, ":utf8");
440 $title =~ s/[\r\n]*$//;
443 if ($asktitle || (!$title && !$noasktitle)) {
444 my $prompt = $self->{-relpath};
445 $prompt = '/' unless ($prompt);
446 my $OUT = $term->OUT || \*STDOUT;
447 print $OUT "Enter title for $fullpath\n";
448 $title = $term->readline($prompt.' >',$title);
449 $term->addhistory($title) if ($title);
450 if (open($T,'>'.$fullpath.'/.title')) {
451 print $T $title,"\n";
456 $title=$self->{-relpath};
458 $self->{-title}=$title;
459 print "title in $fullpath is $title\n" if ($debug);
464 my $fn = $self->{-fullpath};
465 my $name = $self->{-base};
466 my $dn = $self->{-parent}->{-fullpath};
467 my ($w, $h) = dim($self->{-info});
468 my $max = ($w > $h)?$w:$h;
470 foreach my $size(@sizes) {
471 my $nref = '.'.$size.'/'.$name;
472 my $nfn = $dn.'/'.$nref;
473 my $factor=$size/$max;
475 $self->{$size}->{'url'} = $name; # unscaled version
476 $self->{$size}->{'dim'} = [$w, $h];
478 $self->{$size}->{'url'} = $nref;
479 $self->{$size}->{'dim'} = [int($w*$factor+.5),
481 if (isnewer($fn,$nfn)) {
482 doscaling($fn,$nfn,$factor,$w,$h);
489 my ($fn1,$fn2) = @_; # this is not a method
490 my @stat1=stat($fn1);
491 my @stat2=stat($fn2);
492 return (!@stat2 || ($stat1[9] > $stat2[9]));
493 # true if $fn2 is absent or is older than $fn1
497 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
501 my $im = new Image::Magick;
502 print "doscaling $src -> $dest by $factor\n" if ($debug);
503 if ($err = $im->Read($src)) {
504 warn "ImageMagick: read \"$src\": $err";
506 $im->Scale(width=>$w*$factor,height=>$h*$factor);
507 $err=$im->Write($dest);
508 warn "ImageMagick: write \"$dest\": $err" if ($err);
512 if ($err) { # fallback to command-line tools
513 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
519 my $name = $self->{-base};
520 my $dn = $self->{-parent}->{-fullpath};
521 my $pref = $self->{-previmg}->{-base};
522 my $nref = $self->{-nextimg}->{-base};
523 my $inc = $self->{-inc}.$incdir.'/';
524 my $title = $self->{-info}->{'Comment'};
525 $title = $name unless ($title);
527 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
530 for my $refresh('static', 'slide') {
531 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
532 if (isnewer($self->{-fullpath},$fn)) {
533 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
537 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
539 $fwdref = '../index.html';
542 $bakref = sprintf("%s-%s.html",$pref,$refresh);
544 $bakref = '../index.html';
548 if ($refresh eq 'slide') {
549 $toggleref=sprintf("%s-static.html",$name);
550 $toggletext = 'Stop!';
552 $toggleref=sprintf("%s-slide.html",$name);
553 $toggletext = 'Play->';
556 unless (open($F,'>'.$fn)) {
557 warn "cannot open \"$fn\": $!";
560 binmode($F, ":utf8");
561 if ($refresh eq 'slide') {
566 -head=>meta({-http_equiv=>'Refresh',
567 -content=>"3; url=$fwdref"}),
568 -style=>{-src=>$inc."gallery.css"},
570 comment("Created by ".$version),"\n";
573 print $F start_html(-title=>$title,
576 -style=>{-src=>$inc."gallery.css"},
578 comment("Created by ".$version),"\n";
580 print $F start_table({-class=>'navi'}),start_Tr,"\n",
581 td(a({-href=>"../index.html"},"Index")),"\n",
582 td(a({-href=>$bakref},"<<Prev")),"\n",
583 td(a({-href=>$toggleref},$toggletext)),"\n",
584 td(a({-href=>$fwdref},"Next>>")),"\n",
585 td({-class=>'title'},$title),"\n",
588 center(table({-class=>'picframe'},
589 Tr(td(img({-src=>$imgsrc,
590 -class=>'standalone',
591 -alt=>$title}))))),"\n",
598 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
599 if (isnewer($self->{-fullpath},$fn)) {
601 unless (open($F,'>'.$fn)) {
602 warn "cannot open \"$fn\": $!";
605 binmode($F, ":utf8");
606 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
607 print $F start_html(-title=>$title,
609 -style=>{-src=>$inc."gallery.css"},
611 {-src=>$inc."mootools.js"},
612 {-src=>$inc."urlparser.js"},
613 {-src=>$inc."infopage.js"},
615 comment("Created by ".$version),"\n",
618 table({-class=>'ipage'},
619 Tr(td(img({-src=>$imgsrc,
622 td($self->infotable))),
623 a({-href=>'../index.html',-class=>'conceal'},
633 my $fn = $self->{-fullpath}.'/index.html';
634 my $block = $self->{-fullpath}.'/.noindex';
635 $fn = '/dev/null' if ( -f $block );
637 unless (open($IND,'>'.$fn)) {
638 warn "cannot open $fn: $!";
641 binmode($IND, ":utf8");
642 $self->{-IND} = $IND;
644 my $inc = $self->{-inc}.$incdir.'/';
645 my $title = $self->{-title};
648 $feedlink=Link({-rel=>'alternate',
649 -type=>'application/atom+xml',
650 -title=>'Gallery Feed',
651 -href=>$feedobj->{-base}.$feedobj->{-file}});
653 print $IND start_html(-title => $title,
657 {-src=>$inc."gallery.css"},
658 {-src=>$inc."custom.css"},
661 {-src=>$inc."mootools.js"},
662 {-src=>$inc."overlay.js"},
663 {-src=>$inc."urlparser.js"},
664 {-src=>$inc."multibox.js"},
665 {-src=>$inc."showwin.js"},
666 {-src=>$inc."controls.js"},
667 {-src=>$inc."show.js"},
668 {-src=>$inc."gallery.js"},
670 comment("Created by ".$version),"\n",
671 start_div({-class => 'indexContainer',
672 -id => 'indexContainer'}),
675 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/header.pl')) {
682 -version => $version,
683 -depth => $self->{-depth},
685 -path => $self->{-fullpath},
686 -breadcrumbs => "breadcrumbs unimplemented",
688 print $IND eval $prm,"\n";
690 print STDERR "could not open ",
691 $self->{-toppath}.'/'.$incdir.'/header.pl',
692 " ($!), reverting to default header";
693 print $IND a({-href=>"../index.html"},"UP"),"\n",
694 h1({-class=>'title'},$title),"\n",
700 my $IND = $self->{-IND};
704 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/footer.pl')) {
711 -version => $version,
712 -depth => $self->{-depth},
713 -title => $self->{-title},
714 -breadcrumbs => "breadcrumbs unimplemented",
716 print $IND eval $prm,"\n";
718 print STDERR "could not open ",
719 $self->{-toppath}.'/'.$incdir.'/footer.pl',
720 " ($!), reverting to default empty footer";
722 print $IND end_html,"\n";
724 close($IND) if ($IND);
727 my $feedtitle=sprintf "%s [%d images, %d subalbums]",
731 my $feedlink=$feedobj->{-feed}->link.
732 $self->{-relpath}."index.html";
736 UUID::unparse($uu, $us);
737 $feedobj->{-feed}->add_item(
738 title => $self->{-title},
740 description => $feedtitle,
749 my $IND = $self->{-IND};
751 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
756 my $IND = $self->{-parent}->{-IND};
757 my $name = $self->{-base};
758 my $title = $self->{-title};
760 $self->{-parent}->{-numofsubs}++;
761 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
762 td(a({-href=>$name.'/index.html'},$title))),"\n";
767 my $IND = $self->{-IND};
769 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
774 my $IND = $self->{-IND};
775 my $first = $self->{-firstimg}->{-base};
776 my $slideref = sprintf(".html/%s-slide.html",$first);
778 print $IND h2({-class=>"ititle"},"Images ",
779 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
780 '> slideshow')),"\n";
785 my $IND = $self->{-parent}->{-IND};
786 my $name = $self->{-base};
787 my $title = $self->{-info}->{'Comment'};
788 $title = $name unless ($title);
789 my $thumb = $self->{$sizes[0]}->{'url'};
790 my $info = $self->{-info};
791 my ($w, $h) = dim($info);
793 my $i=0+$self->{-parent}->{-numofimgs};
794 $self->{-parent}->{-numofimgs}++;
796 print $IND a({-name=>$name}),"\n",
797 start_table({-class=>'slide'}),start_Tr,start_td,"\n";
798 print $IND div({-class=>'slidetitle'},
799 "\n ",a({-href=>".html/$name-info.html",
800 -title=>'Image Info: '.$name,
803 start_div({-class=>'slideimage'});
804 if ($self->{-geoloc}) {
805 my ($la,$lo) = @{$self->{-geoloc}};
806 print $IND a({-href=>"http://maps.google.com/".
807 "?q=$la,$lo&ll=$la,$lo",
810 div({-class=>'geoloc'},"")),"\n";
812 print $IND a({-href=>".html/$name-static.html",
818 -alt=>$title})),"\n",end_div,
819 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
820 foreach my $sz(@sizes) {
821 my $src=$self->{$sz}->{'url'};
822 my $w=$self->{$sz}->{'dim'}->[0];
823 my $h=$self->{$sz}->{'dim'}->[1];
824 print $IND " ",a({-href=>$src,
827 -title=>"Reduced to ".$w."x".$h},
830 print $IND " ",a({-href=>$name,
832 -title=>'Original'},$w."x".$h),
834 end_td,end_Tr,end_table,"\n";
839 my $IND = $self->{-IND};
841 print $IND br({-clear=>'all'}),hr,"\n\n";
846 my $info = $self->{-info};
863 $msg.=start_table({-class=>'infotable'})."\n";
864 foreach my $k(@infokeys) {
865 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
867 $msg.=end_table."\n";