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 binmode(STDOUT, ":utf8");
41 my $haveimagick = eval { require Image::Magick; };
42 { package Image::Magick; } # to make perl compiler happy
44 my $haverssxml = eval { require XML::RSS; };
45 { package XML::RSS; } # to make perl compiler happy
47 my @sizes = (160, 640, 1600);
48 my $incdir = ".gallery2";
50 ######################################################################
64 'asktitle'=>\$asktitle,
65 'noasktitle'=>\$noasktitle,
66 'rssfile=s'=>\$rssfile,
71 if ($rssfile && ! $haverssxml) {
72 print STDERR "You need to get XML::RSS from CPAN to use --rssfile\n";
76 my $term = new Term::ReadLine "Edit Title";
78 FsObj->new(getcwd)->iterate;
79 if ($rssobj) { $rssobj->{'rss'}->save($rssobj->{'file'}); }
83 print STDERR <<__END__;
85 --help: print help message and exit
86 --incpath: do not try to find .gallery2 diretory upstream, use
87 specified path (absolute or relavive). Use with causion.
88 --debug: print a lot of debugging info to stdout as you run
89 --asktitle: ask to edit album titles even if there are ".title" files
90 --noasktitle: don't ask to enter album titles even where ".title"
91 files are absent. Use partial directory names as titles.
92 --rssfile=...: build RSS feed for newly added "albums", give name of rss file
108 -root=>$parent->{-root},
109 -toppath=>$parent->{-toppath},
110 -depth=>$parent->{-depth}+1,
112 -fullpath=>$parent->{-fullpath}.'/'.$name,
113 -relpath=>$parent->{-relpath}.$name.'/',
114 -inc=>'../'.$parent->{-inc},
123 # fill in -inc, -rss, -relpath
124 initpaths($self); # we are not blessed yet, so cheat.
128 print "new $class:\n";
129 foreach my $k(keys %$self) {
130 print "\t$k\t=\t$self->{$k}\n";
137 my $self=shift; # this is not a method but we cheat
138 my $depth=20; # arbitrary max depth
139 my $fullpath=$self->{-fullpath};
145 $inc .= '/' unless ($inc =~ m%/$%);
148 while ( ! -d $fullpath."/".$inc."/".$incdir ) {
150 last unless ($depth-- > 0);
154 $self->{-inc} = $inc;
157 for ($pos=index($inc,'/');$pos>=0;
158 $pos=index($inc,'/',$pos+1)) {
161 $self->{-depth} = $dp;
162 for ($pos=length($fullpath);$dp>0 && $pos>0;
163 $pos=rindex($fullpath,'/',$pos-1)) {
166 my $relpath = substr($fullpath,$pos);
168 $relpath .= '/' if ($relpath);
169 $self->{-relpath} = $relpath;
170 $self->{-toppath} = substr($fullpath,0,$pos);
171 #print "rel=$relpath, top=$self->{-toppath}, inc=$inc\n";
174 $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
176 $self->{-relpath} = '';
182 my $self=shift; # this is not a method but we cheat
183 my $fullpath=$self->{-fullpath};
184 my $toppath=$self->{-toppath};
185 my $inc=$self->{-inc}.$incdir.'/';
186 my $conffile=$toppath.'/'.$incdir.'/rss.conf';
190 if (open($CONF,">".$conffile)) {
191 print $CONF "file: ",$rssfile,"\n";
194 print STDERR "could not open $conffile: $!\n";
197 if (open($CONF,$conffile)) {
201 my ($k,$v)=split(':', $ln);
212 return unless ($rssfile);
214 $rssobj->{'file'} = $self->{-toppath}.'/'.$rssfile;
215 $rssobj->{'rss'} = new XML::RSS (version=>'2.0');
216 if ( -f $rssobj->{'file'} ) {
217 $rssobj->{'rss'}->parsefile($rssobj->{'file'});
218 my $itemstodel = @{$rssobj->{'rss'}->{'items'}} - 15;
219 while ($itemstodel-- > 0) {
220 pop(@{$rssobj->{'rss'}->{'items'}})
222 $rssobj->{'rss'}->save($rssobj->{'file'});
227 for ($p1=0,$p2=length($toppath);
228 substr($rssfile,$p1,3) eq '../' && $p2>0;
229 $p1+=3,$p2=rindex($toppath,'/',$p2-1)) {;}
230 $link=substr($toppath,$p2);
232 $link .= '/' if ($link);
233 while (($p1=index($rssfile,'/',$p1+1)) >= 0) {
237 $rssobj->{'rss'}->channel(
240 description=>'Gallery Feed',
241 #language=>$language,
243 #copyright=>$copyright,
245 #lastBuildDate=>$lastBuild,
247 #managingEditor=>$editor,
248 #webMaster=>$webMaster
250 $rssobj->{'rss'}->save($rssobj->{'file'});
252 $self->{-rss} = $rssobj->{'rss'};
257 my $fullpath .= $self->{-fullpath};
258 print "iterate in dir $fullpath\n" if ($debug);
264 unless (opendir($D,$fullpath)) {
265 warn "cannot opendir $fullpath: $!";
268 while (my $de = readdir($D)) {
269 next if ($de =~ /^\./);
270 my $child = $self->new($de);
271 my @stat = stat($child->{-fullpath});
272 $youngest = $stat[9] if ($youngest < $stat[9]);
274 push(@rdirlist,$child);
275 } elsif ($child->isimg) {
276 push(@rimglist,$child);
280 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
281 undef @rdirlist; # inplace sorting would be handy here
282 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
283 undef @rimglist; # optimize away unsorted versions
284 $self->{-firstimg} = $imglist[0];
286 print "Dir: $self->{-fullpath}\n" if ($debug);
288 # 1. first of all, fill title for this directory and create hidden subdirs
292 # 2. recurse into subdirectories to get their titles filled
293 # before we start writing out subalbum list
295 foreach my $dir(@dirlist) {
299 # 3. iterate through images to build cross-links,
302 foreach my $img(@imglist) {
303 # list-linking must be done before generating
304 # aux html because aux pages rely on prev/next refs
306 $previmg->{-nextimg} = $img;
307 $img->{-previmg} = $previmg;
312 # 4. create scaled versions and aux html pages
314 foreach my $img(@imglist) {
315 # scaled versions must be generated before aux html
316 # and main image index because they both rely on
317 # refs to scaled images and they may be just original
318 # images, this is not known before we try scaling.
320 # finally, make aux html pages
324 # no need to go beyond this point if the directory timestamp did not
325 # change since we built index.html file last time.
327 my @istat = stat($self->{-fullpath}.'/index.html');
328 return unless ($youngest > $istat[9]);
330 # 5. start building index.html for the directory
334 # 6. iterate through subdirectories to build subalbums list
338 foreach my $dir(@dirlist) {
344 # 7. iterate through images to build thumb list
348 foreach my $img(@imglist) {
349 print "Img: $img->{-fullpath}\n" if ($debug);
355 # 8. comlplete building index.html for the directory
362 return ( -d $self->{-fullpath} );
367 my $fullpath = $self->{-fullpath};
368 return 0 unless ( -f $fullpath );
369 my $info = image_info($fullpath);
370 if (my $error = $info->{error}) {
371 if (($error !~ "Unrecognized file format") &&
372 ($error !~ "Can't read head")) {
373 warn "File \"$fullpath\": $error\n";
378 tryapp12($info) unless ($info->{'ExifVersion'});
381 $self->{-info} = $info;
386 my $info = shift; # this is not a method
388 # dirty hack to take care of Image::Info parser strangeness
389 foreach my $k(keys %$info) {
390 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
392 return unless ($app12); # bad luck
394 foreach my $ln(split /[\r\n]+/,$app12) {
395 $ln =~ s/[[:^print:]\000]/ /g;
396 unless ($seenfirstline) {
401 my ($k,$v)=split /=/,$ln,2;
402 if ($k eq 'TimeDate') {
403 $info->{'DateTime'} =
404 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
406 } elsif ($k eq 'Shutter') {
407 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
408 } elsif ($k eq 'Flash') {
409 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
410 } elsif ($k eq 'Type') {
411 $info->{'Model'} = $v;
412 } elsif ($k eq 'Version') {
413 $info->{'Software'} = $v;
414 } elsif ($k eq 'Fnumber') {
415 $info->{'FNumber'} = $v;
422 my $fullpath = $self->{-fullpath};
423 for my $subdir(@sizes, 'html') {
424 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
425 mkdir($tdir,0755) unless ( -d $tdir );
432 my $fullpath = $self->{-fullpath};
435 if (open($T,'<'.$fullpath.'/.title')) {
437 $title =~ s/[\r\n]*$//;
440 if ($asktitle || (!$title && !$noasktitle)) {
441 my $prompt = $self->{-relpath};
442 $prompt = '/' unless ($prompt);
443 my $OUT = $term->OUT || \*STDOUT;
444 print $OUT "Enter title for $fullpath\n";
445 $title = $term->readline($prompt.' >',$title);
446 $term->addhistory($title) if ($title);
447 if (open($T,'>'.$fullpath.'/.title')) {
448 print $T $title,"\n";
453 $title=$self->{-relpath};
455 $self->{-title}=$title;
456 print "title in $fullpath is $title\n" if ($debug);
461 my $fn = $self->{-fullpath};
462 my $name = $self->{-base};
463 my $dn = $self->{-parent}->{-fullpath};
464 my ($w, $h) = dim($self->{-info});
465 my $max = ($w > $h)?$w:$h;
467 foreach my $size(@sizes) {
468 my $nref = '.'.$size.'/'.$name;
469 my $nfn = $dn.'/'.$nref;
470 my $factor=$size/$max;
472 $self->{$size}->{'url'} = $name; # unscaled version
473 $self->{$size}->{'dim'} = [$w, $h];
475 $self->{$size}->{'url'} = $nref;
476 $self->{$size}->{'dim'} = [int($w*$factor+.5),
478 if (isnewer($fn,$nfn)) {
479 doscaling($fn,$nfn,$factor,$w,$h);
486 my ($fn1,$fn2) = @_; # this is not a method
487 my @stat1=stat($fn1);
488 my @stat2=stat($fn2);
489 return (!@stat2 || ($stat1[9] > $stat2[9]));
490 # true if $fn2 is absent or is older than $fn1
494 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
498 my $im = new Image::Magick;
499 print "doscaling $src -> $dest by $factor\n" if ($debug);
500 if ($err = $im->Read($src)) {
501 warn "ImageMagick: read \"$src\": $err";
503 $im->Scale(width=>$w*$factor,height=>$h*$factor);
504 $err=$im->Write($dest);
505 warn "ImageMagick: write \"$dest\": $err" if ($err);
509 if ($err) { # fallback to command-line tools
510 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
516 my $name = $self->{-base};
517 my $dn = $self->{-parent}->{-fullpath};
518 my $pref = $self->{-previmg}->{-base};
519 my $nref = $self->{-nextimg}->{-base};
520 my $inc = $self->{-inc}.$incdir.'/';
521 my $title = $self->{-info}->{'Comment'};
522 $title = $name unless ($title);
524 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
527 for my $refresh('static', 'slide') {
528 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
529 if (isnewer($self->{-fullpath},$fn)) {
530 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
534 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
536 $fwdref = '../index.html';
539 $bakref = sprintf("%s-%s.html",$pref,$refresh);
541 $bakref = '../index.html';
545 if ($refresh eq 'slide') {
546 $toggleref=sprintf("%s-static.html",$name);
547 $toggletext = 'Stop!';
549 $toggleref=sprintf("%s-slide.html",$name);
550 $toggletext = 'Play->';
553 unless (open($F,'>'.$fn)) {
554 warn "cannot open \"$fn\": $!";
557 binmode($F, ":utf8");
558 if ($refresh eq 'slide') {
563 -head=>meta({-http_equiv=>'Refresh',
564 -content=>"3; url=$fwdref"}),
565 -style=>{-src=>$inc."gallery.css"},
567 comment("Created by ".$version),"\n";
570 print $F start_html(-title=>$title,
573 -style=>{-src=>$inc."gallery.css"},
575 comment("Created by ".$version),"\n";
577 print $F start_table({-class=>'navi'}),start_Tr,"\n",
578 td(a({-href=>"../index.html"},"Index")),"\n",
579 td(a({-href=>$bakref},"<<Prev")),"\n",
580 td(a({-href=>$toggleref},$toggletext)),"\n",
581 td(a({-href=>$fwdref},"Next>>")),"\n",
582 td({-class=>'title'},$title),"\n",
585 center(table({-class=>'picframe'},
586 Tr(td(img({-src=>$imgsrc,
587 -class=>'standalone',
588 -alt=>$title}))))),"\n",
595 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
596 if (isnewer($self->{-fullpath},$fn)) {
598 unless (open($F,'>'.$fn)) {
599 warn "cannot open \"$fn\": $!";
602 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
603 print $F start_html(-title=>$title,
605 -style=>{-src=>$inc."gallery.css"},
607 {-src=>$inc."mootools.js"},
608 {-src=>$inc."urlparser.js"},
609 {-src=>$inc."infopage.js"},
611 comment("Created by ".$version),"\n",
614 table({-class=>'ipage'},
615 Tr(td(img({-src=>$imgsrc,
618 td($self->infotable))),
619 a({-href=>'../index.html',-class=>'conceal'},
629 my $fn = $self->{-fullpath}.'/index.html';
630 my $block = $self->{-fullpath}.'/.noindex';
631 $fn = '/dev/null' if ( -f $block );
633 unless (open($IND,'>'.$fn)) {
634 warn "cannot open $fn: $!";
637 binmode($IND, ":utf8");
638 $self->{-IND} = $IND;
640 my $inc = $self->{-inc}.$incdir.'/';
641 my $title = $self->{-title};
644 $rsslink=Link({-rel=>'alternate',
645 -type=>'application/rss+xml',
647 -href=>$self->{-inc}.$rssfile});
649 print $IND start_html(-title => $title,
653 {-src=>$inc."gallery.css"},
654 {-src=>$inc."custom.css"},
657 {-src=>$inc."mootools.js"},
658 {-src=>$inc."overlay.js"},
659 {-src=>$inc."urlparser.js"},
660 {-src=>$inc."multibox.js"},
661 {-src=>$inc."showwin.js"},
662 {-src=>$inc."controls.js"},
663 {-src=>$inc."show.js"},
664 {-src=>$inc."gallery.js"},
666 comment("Created by ".$version),"\n",
667 start_div({-class => 'indexContainer',
668 -id => 'indexContainer'}),
671 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/header.pl')) {
678 -version => $version,
679 -depth => $self->{-depth},
681 -breadcrumbs => "breadcrumbs unimplemented",
683 print $IND eval $prm,"\n";
685 print STDERR "could not open ",
686 $self->{-toppath}.'/'.$incdir.'/header.pl',
687 " ($!), reverting to default header";
688 print $IND a({-href=>"../index.html"},"UP"),"\n",
689 h1({-class=>'title'},$title),"\n",
695 my $IND = $self->{-IND};
699 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/footer.pl')) {
706 -version => $version,
707 -depth => $self->{-depth},
708 -title => $self->{-title},
709 -breadcrumbs => "breadcrumbs unimplemented",
711 print $IND eval $prm,"\n";
713 print STDERR "could not open ",
714 $self->{-toppath}.'/'.$incdir.'/footer.pl',
715 " ($!), reverting to default empty footer";
717 print $IND end_html,"\n";
719 close($IND) if ($IND);
722 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
726 my $rsslink=$rssobj->{'rss'}->channel('link').
727 $self->{-relpath}."index.html";
728 $rssobj->{'rss'}->add_item(
729 title => $self->{-title},
731 description => $rsstitle,
732 pubDate => time2str(time),
739 my $IND = $self->{-IND};
741 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
746 my $IND = $self->{-parent}->{-IND};
747 my $name = $self->{-base};
748 my $title = $self->{-title};
750 $self->{-parent}->{-numofsubs}++;
751 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
752 td(a({-href=>$name.'/index.html'},$title))),"\n";
757 my $IND = $self->{-IND};
759 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
764 my $IND = $self->{-IND};
765 my $first = $self->{-firstimg}->{-base};
766 my $slideref = sprintf(".html/%s-slide.html",$first);
768 print $IND h2({-class=>"ititle"},"Images ",
769 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
770 '> slideshow')),"\n";
775 my $IND = $self->{-parent}->{-IND};
776 my $name = $self->{-base};
777 my $title = $self->{-info}->{'Comment'};
778 $title = $name unless ($title);
779 my $thumb = $self->{$sizes[0]}->{'url'};
780 my $info = $self->{-info};
781 my ($w, $h) = dim($info);
783 my $i=0+$self->{-parent}->{-numofimgs};
784 $self->{-parent}->{-numofimgs}++;
786 print $IND a({-name=>$name}),"\n",
787 start_table({-class=>'slide'}),start_Tr,start_td,"\n",
788 div({-class=>'slidetitle'},
789 "\n ",a({-href=>".html/$name-info.html",
790 -title=>'Image Info: '.$name,
793 div({-class=>'slideimage'},
794 "\n ",a({-href=>".html/$name-static.html",
800 -alt=>$title})),"\n"),"\n",
801 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
802 foreach my $sz(@sizes) {
803 my $src=$self->{$sz}->{'url'};
804 my $w=$self->{$sz}->{'dim'}->[0];
805 my $h=$self->{$sz}->{'dim'}->[1];
806 print $IND " ",a({-href=>$src,
809 -title=>"Reduced to ".$w."x".$h},
812 print $IND " ",a({-href=>$name,
814 -title=>'Original'},$w."x".$h),
816 end_td,end_Tr,end_table,"\n";
821 my $IND = $self->{-IND};
823 print $IND br({-clear=>'all'}),hr,"\n\n";
828 my $info = $self->{-info};
845 $msg.=start_table({-class=>'infotable'})."\n";
846 foreach my $k(@infokeys) {
847 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
849 $msg.=end_table."\n";