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/;
38 #use encoding 'utf-8';
39 binmode(STDOUT, ":utf8");
41 my $haveimagick = eval { require Image::Magick; };
42 { package Image::Magick; } # to make perl compiler happy
44 my $haverss = eval { require XML::RSS; };
45 { package XML::RSS; } # to make perl compiler happy
47 my $haveatom = eval { require XML::Atom; };
48 { package XML::Atom; } # to make perl compiler happy
50 my $havegeoloc = eval { require Image::ExifTool::Location; };
51 { package Image::ExifTool::Location; } # to make perl compiler happy
53 my @sizes = (160, 640, 1600);
54 my $incdir = ".gallery2";
56 ######################################################################
70 'asktitle'=>\$asktitle,
71 'noasktitle'=>\$noasktitle,
77 if ($feed && ! ($haverss || $haveatom)) {
78 print STDERR "You need to get XML::RSS and/or XML::Atom to use --feed\n";
82 my $term = new Term::ReadLine "Edit Title";
84 FsObj->new(getcwd)->iterate;
85 if ($rssobj) { $rssobj->{'rss'}->save($rssobj->{'file'}); }
89 print STDERR <<__END__;
91 --help: print help message and exit
92 --incpath: do not try to find .gallery2 diretory upstream, use
93 specified path (absolute or relavive). Use with causion.
94 --debug: print a lot of debugging info to stdout as you run
95 --asktitle: ask to edit album titles even if there are ".title" files
96 --noasktitle: don't ask to enter album titles even where ".title"
97 files are absent. Use partial directory names as titles.
98 --feed=...: build RSS feed for newly added "albums", give name of rss file
114 -root=>$parent->{-root},
115 -toppath=>$parent->{-toppath},
116 -depth=>$parent->{-depth}+1,
118 -fullpath=>$parent->{-fullpath}.'/'.$name,
119 -relpath=>$parent->{-relpath}.$name.'/',
120 -inc=>'../'.$parent->{-inc},
129 # fill in -inc, -rss, -relpath
130 initpaths($self); # we are not blessed yet, so cheat.
134 print "new $class:\n";
135 foreach my $k(keys %$self) {
136 print "\t$k\t=\t$self->{$k}\n";
143 my $self=shift; # this is not a method but we cheat
144 my $depth=20; # arbitrary max depth
145 my $fullpath=$self->{-fullpath};
151 $inc .= '/' unless ($inc =~ m%/$%);
154 while ( ! -d $fullpath."/".$inc."/".$incdir ) {
156 last unless ($depth-- > 0);
160 $self->{-inc} = $inc;
163 for ($pos=index($inc,'/');$pos>=0;
164 $pos=index($inc,'/',$pos+1)) {
167 $self->{-depth} = $dp;
168 for ($pos=length($fullpath);$dp>0 && $pos>0;
169 $pos=rindex($fullpath,'/',$pos-1)) {
172 my $relpath = substr($fullpath,$pos);
174 $relpath .= '/' if ($relpath);
175 $self->{-relpath} = $relpath;
176 $self->{-toppath} = substr($fullpath,0,$pos);
177 #print "rel=$relpath, top=$self->{-toppath}, inc=$inc\n";
180 $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
182 $self->{-relpath} = '';
188 my $self=shift; # this is not a method but we cheat
189 my $fullpath=$self->{-fullpath};
190 my $toppath=$self->{-toppath};
191 my $inc=$self->{-inc}.$incdir.'/';
192 my $conffile=$toppath.'/'.$incdir.'/rss.conf';
196 if (open($CONF,">".$conffile)) {
197 print $CONF "file: ",$feed,"\n";
200 print STDERR "could not open $conffile: $!\n";
203 if (open($CONF,$conffile)) {
207 my ($k,$v)=split(':', $ln);
218 return unless ($feed);
220 $rssobj->{'file'} = $self->{-toppath}.'/'.$feed;
221 $rssobj->{'rss'} = new XML::RSS (version=>'2.0');
222 if ( -f $rssobj->{'file'} ) {
223 $rssobj->{'rss'}->parsefile($rssobj->{'file'});
224 my $itemstodel = @{$rssobj->{'rss'}->{'items'}} - 15;
225 while ($itemstodel-- > 0) {
226 pop(@{$rssobj->{'rss'}->{'items'}})
228 $rssobj->{'rss'}->save($rssobj->{'file'});
233 for ($p1=0,$p2=length($toppath);
234 substr($feed,$p1,3) eq '../' && $p2>0;
235 $p1+=3,$p2=rindex($toppath,'/',$p2-1)) {;}
236 $link=substr($toppath,$p2);
238 $link .= '/' if ($link);
239 while (($p1=index($feed,'/',$p1+1)) >= 0) {
243 $rssobj->{'rss'}->channel(
246 description=>'Gallery Feed',
247 #language=>$language,
249 #copyright=>$copyright,
251 #lastBuildDate=>$lastBuild,
253 #managingEditor=>$editor,
254 #webMaster=>$webMaster
256 $rssobj->{'rss'}->save($rssobj->{'file'});
258 $self->{-rss} = $rssobj->{'rss'};
263 my $fullpath .= $self->{-fullpath};
264 print "iterate in dir $fullpath\n" if ($debug);
270 unless (opendir($D,$fullpath)) {
271 warn "cannot opendir $fullpath: $!";
274 while (my $de = readdir($D)) {
275 next if ($de =~ /^\./);
276 my $child = $self->new($de);
277 my @stat = stat($child->{-fullpath});
278 $youngest = $stat[9] if ($youngest < $stat[9]);
280 push(@rdirlist,$child);
281 } elsif ($child->isimg) {
282 push(@rimglist,$child);
286 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
287 undef @rdirlist; # inplace sorting would be handy here
288 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
289 undef @rimglist; # optimize away unsorted versions
290 $self->{-firstimg} = $imglist[0];
292 print "Dir: $self->{-fullpath}\n" if ($debug);
294 # 1. first of all, fill title for this directory and create hidden subdirs
298 # 2. recurse into subdirectories to get their titles filled
299 # before we start writing out subalbum list
301 foreach my $dir(@dirlist) {
305 # 3. iterate through images to build cross-links,
308 foreach my $img(@imglist) {
309 # list-linking must be done before generating
310 # aux html because aux pages rely on prev/next refs
312 $previmg->{-nextimg} = $img;
313 $img->{-previmg} = $previmg;
318 # 4. create scaled versions and aux html pages
320 foreach my $img(@imglist) {
321 # scaled versions must be generated before aux html
322 # and main image index because they both rely on
323 # refs to scaled images and they may be just original
324 # images, this is not known before we try scaling.
326 # finally, make aux html pages
330 # no need to go beyond this point if the directory timestamp did not
331 # change since we built index.html file last time.
333 my @istat = stat($self->{-fullpath}.'/index.html');
334 return unless ($youngest > $istat[9]);
336 # 5. start building index.html for the directory
340 # 6. iterate through subdirectories to build subalbums list
344 foreach my $dir(@dirlist) {
350 # 7. iterate through images to build thumb list
354 foreach my $img(@imglist) {
355 print "Img: $img->{-fullpath}\n" if ($debug);
361 # 8. comlplete building index.html for the directory
368 return ( -d $self->{-fullpath} );
373 my $fullpath = $self->{-fullpath};
374 return 0 unless ( -f $fullpath );
377 my $exif = new Image::ExifTool;
378 $exif->ExtractInfo($fullpath);
379 my ($la,$lo) = $exif->GetLocation();
381 $self->{-geoloc} = [$la,$lo];
385 my $info = image_info($fullpath);
386 if (my $error = $info->{error}) {
387 if (($error !~ "Unrecognized file format") &&
388 ($error !~ "Can't read head")) {
389 warn "File \"$fullpath\": $error\n";
394 tryapp12($info) unless ($info->{'ExifVersion'});
397 $self->{-info} = $info;
402 my $info = shift; # this is not a method
404 # dirty hack to take care of Image::Info parser strangeness
405 foreach my $k(keys %$info) {
406 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
408 return unless ($app12); # bad luck
410 foreach my $ln(split /[\r\n]+/,$app12) {
411 $ln =~ s/[[:^print:]\000]/ /g;
412 unless ($seenfirstline) {
417 my ($k,$v)=split /=/,$ln,2;
418 if ($k eq 'TimeDate') {
419 $info->{'DateTime'} =
420 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
422 } elsif ($k eq 'Shutter') {
423 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
424 } elsif ($k eq 'Flash') {
425 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
426 } elsif ($k eq 'Type') {
427 $info->{'Model'} = $v;
428 } elsif ($k eq 'Version') {
429 $info->{'Software'} = $v;
430 } elsif ($k eq 'Fnumber') {
431 $info->{'FNumber'} = $v;
438 my $fullpath = $self->{-fullpath};
439 for my $subdir(@sizes, 'html') {
440 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
441 mkdir($tdir,0755) unless ( -d $tdir );
448 my $fullpath = $self->{-fullpath};
451 if (open($T,'<'.$fullpath.'/.title')) {
452 binmode($T, ":utf8");
454 $title =~ s/[\r\n]*$//;
457 if ($asktitle || (!$title && !$noasktitle)) {
458 my $prompt = $self->{-relpath};
459 $prompt = '/' unless ($prompt);
460 my $OUT = $term->OUT || \*STDOUT;
461 print $OUT "Enter title for $fullpath\n";
462 $title = $term->readline($prompt.' >',$title);
463 $term->addhistory($title) if ($title);
464 if (open($T,'>'.$fullpath.'/.title')) {
465 print $T $title,"\n";
470 $title=$self->{-relpath};
472 $self->{-title}=$title;
473 print "title in $fullpath is $title\n" if ($debug);
478 my $fn = $self->{-fullpath};
479 my $name = $self->{-base};
480 my $dn = $self->{-parent}->{-fullpath};
481 my ($w, $h) = dim($self->{-info});
482 my $max = ($w > $h)?$w:$h;
484 foreach my $size(@sizes) {
485 my $nref = '.'.$size.'/'.$name;
486 my $nfn = $dn.'/'.$nref;
487 my $factor=$size/$max;
489 $self->{$size}->{'url'} = $name; # unscaled version
490 $self->{$size}->{'dim'} = [$w, $h];
492 $self->{$size}->{'url'} = $nref;
493 $self->{$size}->{'dim'} = [int($w*$factor+.5),
495 if (isnewer($fn,$nfn)) {
496 doscaling($fn,$nfn,$factor,$w,$h);
503 my ($fn1,$fn2) = @_; # this is not a method
504 my @stat1=stat($fn1);
505 my @stat2=stat($fn2);
506 return (!@stat2 || ($stat1[9] > $stat2[9]));
507 # true if $fn2 is absent or is older than $fn1
511 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
515 my $im = new Image::Magick;
516 print "doscaling $src -> $dest by $factor\n" if ($debug);
517 if ($err = $im->Read($src)) {
518 warn "ImageMagick: read \"$src\": $err";
520 $im->Scale(width=>$w*$factor,height=>$h*$factor);
521 $err=$im->Write($dest);
522 warn "ImageMagick: write \"$dest\": $err" if ($err);
526 if ($err) { # fallback to command-line tools
527 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
533 my $name = $self->{-base};
534 my $dn = $self->{-parent}->{-fullpath};
535 my $pref = $self->{-previmg}->{-base};
536 my $nref = $self->{-nextimg}->{-base};
537 my $inc = $self->{-inc}.$incdir.'/';
538 my $title = $self->{-info}->{'Comment'};
539 $title = $name unless ($title);
541 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
544 for my $refresh('static', 'slide') {
545 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
546 if (isnewer($self->{-fullpath},$fn)) {
547 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
551 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
553 $fwdref = '../index.html';
556 $bakref = sprintf("%s-%s.html",$pref,$refresh);
558 $bakref = '../index.html';
562 if ($refresh eq 'slide') {
563 $toggleref=sprintf("%s-static.html",$name);
564 $toggletext = 'Stop!';
566 $toggleref=sprintf("%s-slide.html",$name);
567 $toggletext = 'Play->';
570 unless (open($F,'>'.$fn)) {
571 warn "cannot open \"$fn\": $!";
574 binmode($F, ":utf8");
575 if ($refresh eq 'slide') {
580 -head=>meta({-http_equiv=>'Refresh',
581 -content=>"3; url=$fwdref"}),
582 -style=>{-src=>$inc."gallery.css"},
584 comment("Created by ".$version),"\n";
587 print $F start_html(-title=>$title,
590 -style=>{-src=>$inc."gallery.css"},
592 comment("Created by ".$version),"\n";
594 print $F start_table({-class=>'navi'}),start_Tr,"\n",
595 td(a({-href=>"../index.html"},"Index")),"\n",
596 td(a({-href=>$bakref},"<<Prev")),"\n",
597 td(a({-href=>$toggleref},$toggletext)),"\n",
598 td(a({-href=>$fwdref},"Next>>")),"\n",
599 td({-class=>'title'},$title),"\n",
602 center(table({-class=>'picframe'},
603 Tr(td(img({-src=>$imgsrc,
604 -class=>'standalone',
605 -alt=>$title}))))),"\n",
612 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
613 if (isnewer($self->{-fullpath},$fn)) {
615 unless (open($F,'>'.$fn)) {
616 warn "cannot open \"$fn\": $!";
619 binmode($F, ":utf8");
620 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
621 print $F start_html(-title=>$title,
623 -style=>{-src=>$inc."gallery.css"},
625 {-src=>$inc."mootools.js"},
626 {-src=>$inc."urlparser.js"},
627 {-src=>$inc."infopage.js"},
629 comment("Created by ".$version),"\n",
632 table({-class=>'ipage'},
633 Tr(td(img({-src=>$imgsrc,
636 td($self->infotable))),
637 a({-href=>'../index.html',-class=>'conceal'},
647 my $fn = $self->{-fullpath}.'/index.html';
648 my $block = $self->{-fullpath}.'/.noindex';
649 $fn = '/dev/null' if ( -f $block );
651 unless (open($IND,'>'.$fn)) {
652 warn "cannot open $fn: $!";
655 binmode($IND, ":utf8");
656 $self->{-IND} = $IND;
658 my $inc = $self->{-inc}.$incdir.'/';
659 my $title = $self->{-title};
662 $rsslink=Link({-rel=>'alternate',
663 -type=>'application/rss+xml',
665 -href=>$self->{-inc}.$feed});
667 print $IND start_html(-title => $title,
671 {-src=>$inc."gallery.css"},
672 {-src=>$inc."custom.css"},
675 {-src=>$inc."mootools.js"},
676 {-src=>$inc."overlay.js"},
677 {-src=>$inc."urlparser.js"},
678 {-src=>$inc."multibox.js"},
679 {-src=>$inc."showwin.js"},
680 {-src=>$inc."controls.js"},
681 {-src=>$inc."show.js"},
682 {-src=>$inc."gallery.js"},
684 comment("Created by ".$version),"\n",
685 start_div({-class => 'indexContainer',
686 -id => 'indexContainer'}),
689 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/header.pl')) {
696 -version => $version,
697 -depth => $self->{-depth},
699 -breadcrumbs => "breadcrumbs unimplemented",
701 print $IND eval $prm,"\n";
703 print STDERR "could not open ",
704 $self->{-toppath}.'/'.$incdir.'/header.pl',
705 " ($!), reverting to default header";
706 print $IND a({-href=>"../index.html"},"UP"),"\n",
707 h1({-class=>'title'},$title),"\n",
713 my $IND = $self->{-IND};
717 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/footer.pl')) {
724 -version => $version,
725 -depth => $self->{-depth},
726 -title => $self->{-title},
727 -breadcrumbs => "breadcrumbs unimplemented",
729 print $IND eval $prm,"\n";
731 print STDERR "could not open ",
732 $self->{-toppath}.'/'.$incdir.'/footer.pl',
733 " ($!), reverting to default empty footer";
735 print $IND end_html,"\n";
737 close($IND) if ($IND);
740 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
744 my $rsslink=$rssobj->{'rss'}->channel('link').
745 $self->{-relpath}."index.html";
746 $rssobj->{'rss'}->add_item(
747 title => $self->{-title},
749 description => $rsstitle,
750 pubDate => time2str(time),
757 my $IND = $self->{-IND};
759 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
764 my $IND = $self->{-parent}->{-IND};
765 my $name = $self->{-base};
766 my $title = $self->{-title};
768 $self->{-parent}->{-numofsubs}++;
769 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
770 td(a({-href=>$name.'/index.html'},$title))),"\n";
775 my $IND = $self->{-IND};
777 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
782 my $IND = $self->{-IND};
783 my $first = $self->{-firstimg}->{-base};
784 my $slideref = sprintf(".html/%s-slide.html",$first);
786 print $IND h2({-class=>"ititle"},"Images ",
787 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
788 '> slideshow')),"\n";
793 my $IND = $self->{-parent}->{-IND};
794 my $name = $self->{-base};
795 my $title = $self->{-info}->{'Comment'};
796 $title = $name unless ($title);
797 my $thumb = $self->{$sizes[0]}->{'url'};
798 my $info = $self->{-info};
799 my ($w, $h) = dim($info);
801 my $i=0+$self->{-parent}->{-numofimgs};
802 $self->{-parent}->{-numofimgs}++;
804 print $IND a({-name=>$name}),"\n",
805 start_table({-class=>'slide'}),start_Tr,start_td,"\n";
806 print $IND div({-class=>'slidetitle'},
807 "\n ",a({-href=>".html/$name-info.html",
808 -title=>'Image Info: '.$name,
811 start_div({-class=>'slideimage'});
812 if ($self->{-geoloc}) {
813 my ($la,$lo) = @{$self->{-geoloc}};
814 print $IND a({-href=>"http://maps.google.com/".
815 "?q=$la,$lo&ll=$la,$lo",
818 div({-class=>'geoloc'},"")),"\n";
820 print $IND a({-href=>".html/$name-static.html",
826 -alt=>$title})),"\n",end_div,
827 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
828 foreach my $sz(@sizes) {
829 my $src=$self->{$sz}->{'url'};
830 my $w=$self->{$sz}->{'dim'}->[0];
831 my $h=$self->{$sz}->{'dim'}->[1];
832 print $IND " ",a({-href=>$src,
835 -title=>"Reduced to ".$w."x".$h},
838 print $IND " ",a({-href=>$name,
840 -title=>'Original'},$w."x".$h),
842 end_td,end_Tr,end_table,"\n";
847 my $IND = $self->{-IND};
849 print $IND br({-clear=>'all'}),hr,"\n\n";
854 my $info = $self->{-info};
871 $msg.=start_table({-class=>'infotable'})."\n";
872 foreach my $k(@infokeys) {
873 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
875 $msg.=end_table."\n";