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},
124 # fill in -inc, -rss, -relpath
125 initpaths($self); # we are not blessed yet, so cheat.
129 print "new $class:\n";
130 foreach my $k(keys %$self) {
131 print "\t$k\t=\t$self->{$k}\n";
138 my $self=shift; # this is not a method but we cheat
139 my $depth=20; # arbitrary max depth
140 my $fullpath=$self->{-fullpath};
146 $inc .= '/' unless ($inc =~ m%/$%);
149 while ( ! -d $fullpath."/".$inc."/".$incdir ) {
151 last unless ($depth-- > 0);
155 $self->{-inc} = $inc;
158 for ($pos=index($inc,'/');$pos>=0;
159 $pos=index($inc,'/',$pos+1)) {
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} = '';
181 my $self=shift; # this is not a method but we cheat
182 my $fullpath=$self->{-fullpath};
183 my $toppath=$self->{-toppath};
184 my $inc=$self->{-inc}.$incdir.'/';
185 my $conffile=$toppath.'/'.$incdir.'/rss.conf';
189 if (open($CONF,">".$conffile)) {
190 print $CONF "file: ",$rssfile,"\n";
193 print STDERR "could not open $conffile: $!\n";
196 if (open($CONF,$conffile)) {
200 my ($k,$v)=split(':', $ln);
211 return unless ($rssfile);
213 $rssobj->{'file'} = $self->{-toppath}.'/'.$rssfile;
214 $rssobj->{'rss'} = new XML::RSS (version=>'2.0');
215 if ( -f $rssobj->{'file'} ) {
216 $rssobj->{'rss'}->parsefile($rssobj->{'file'});
217 my $itemstodel = @{$rssobj->{'rss'}->{'items'}} - 15;
218 while ($itemstodel-- > 0) {
219 pop(@{$rssobj->{'rss'}->{'items'}})
221 $rssobj->{'rss'}->save($rssobj->{'file'});
226 for ($p1=0,$p2=length($toppath);
227 substr($rssfile,$p1,3) eq '../' && $p2>0;
228 $p1+=3,$p2=rindex($toppath,'/',$p2-1)) {;}
229 $link=substr($toppath,$p2);
231 $link .= '/' if ($link);
232 while (($p1=index($rssfile,'/',$p1+1)) >= 0) {
236 $rssobj->{'rss'}->channel(
239 description=>'Gallery Feed',
240 #language=>$language,
242 #copyright=>$copyright,
244 #lastBuildDate=>$lastBuild,
246 #managingEditor=>$editor,
247 #webMaster=>$webMaster
249 $rssobj->{'rss'}->save($rssobj->{'file'});
251 $self->{-rss} = $rssobj->{'rss'};
256 my $fullpath .= $self->{-fullpath};
257 print "iterate in dir $fullpath\n" if ($debug);
263 unless (opendir($D,$fullpath)) {
264 warn "cannot opendir $fullpath: $!";
267 while (my $de = readdir($D)) {
268 next if ($de =~ /^\./);
269 my $child = $self->new($de);
270 my @stat = stat($child->{-fullpath});
271 $youngest = $stat[9] if ($youngest < $stat[9]);
273 push(@rdirlist,$child);
274 } elsif ($child->isimg) {
275 push(@rimglist,$child);
279 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
280 undef @rdirlist; # inplace sorting would be handy here
281 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
282 undef @rimglist; # optimize away unsorted versions
283 $self->{-firstimg} = $imglist[0];
285 print "Dir: $self->{-fullpath}\n" if ($debug);
287 # 1. first of all, fill title for this directory and create hidden subdirs
291 # 2. recurse into subdirectories to get their titles filled
292 # before we start writing out subalbum list
294 foreach my $dir(@dirlist) {
298 # 3. iterate through images to build cross-links,
301 foreach my $img(@imglist) {
302 # list-linking must be done before generating
303 # aux html because aux pages rely on prev/next refs
305 $previmg->{-nextimg} = $img;
306 $img->{-previmg} = $previmg;
311 # 4. create scaled versions and aux html pages
313 foreach my $img(@imglist) {
314 # scaled versions must be generated before aux html
315 # and main image index because they both rely on
316 # refs to scaled images and they may be just original
317 # images, this is not known before we try scaling.
319 # finally, make aux html pages
323 # no need to go beyond this point if the directory timestamp did not
324 # change since we built index.html file last time.
326 my @istat = stat($self->{-fullpath}.'/index.html');
327 return unless ($youngest > $istat[9]);
329 # 5. start building index.html for the directory
333 # 6. iterate through subdirectories to build subalbums list
337 foreach my $dir(@dirlist) {
343 # 7. iterate through images to build thumb list
347 foreach my $img(@imglist) {
348 print "Img: $img->{-fullpath}\n" if ($debug);
354 # 8. comlplete building index.html for the directory
361 return ( -d $self->{-fullpath} );
366 my $fullpath = $self->{-fullpath};
367 return 0 unless ( -f $fullpath );
368 my $info = image_info($fullpath);
369 if (my $error = $info->{error}) {
370 if (($error !~ "Unrecognized file format") &&
371 ($error !~ "Can't read head")) {
372 warn "File \"$fullpath\": $error\n";
377 tryapp12($info) unless ($info->{'ExifVersion'});
380 $self->{-info} = $info;
385 my $info = shift; # this is not a method
387 # dirty hack to take care of Image::Info parser strangeness
388 foreach my $k(keys %$info) {
389 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
391 return unless ($app12); # bad luck
393 foreach my $ln(split /[\r\n]+/,$app12) {
394 $ln =~ s/[[:^print:]\000]/ /g;
395 unless ($seenfirstline) {
400 my ($k,$v)=split /=/,$ln,2;
401 if ($k eq 'TimeDate') {
402 $info->{'DateTime'} =
403 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
405 } elsif ($k eq 'Shutter') {
406 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
407 } elsif ($k eq 'Flash') {
408 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
409 } elsif ($k eq 'Type') {
410 $info->{'Model'} = $v;
411 } elsif ($k eq 'Version') {
412 $info->{'Software'} = $v;
413 } elsif ($k eq 'Fnumber') {
414 $info->{'FNumber'} = $v;
421 my $fullpath = $self->{-fullpath};
422 for my $subdir(@sizes, 'html') {
423 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
424 mkdir($tdir,0755) unless ( -d $tdir );
431 my $fullpath = $self->{-fullpath};
434 if (open($T,'<'.$fullpath.'/.title')) {
436 $title =~ s/[\r\n]*$//;
439 if ($asktitle || (!$title && !$noasktitle)) {
440 my $prompt = $self->{-relpath};
441 $prompt = '/' unless ($prompt);
442 my $OUT = $term->OUT || \*STDOUT;
443 print $OUT "Enter title for $fullpath\n";
444 $title = $term->readline($prompt.' >',$title);
445 $term->addhistory($title) if ($title);
446 if (open($T,'>'.$fullpath.'/.title')) {
447 print $T $title,"\n";
452 $title=$self->{-relpath};
454 $self->{-title}=$title;
455 print "title in $fullpath is $title\n" if ($debug);
460 my $fn = $self->{-fullpath};
461 my $name = $self->{-base};
462 my $dn = $self->{-parent}->{-fullpath};
463 my ($w, $h) = dim($self->{-info});
464 my $max = ($w > $h)?$w:$h;
466 foreach my $size(@sizes) {
467 my $nref = '.'.$size.'/'.$name;
468 my $nfn = $dn.'/'.$nref;
469 my $factor=$size/$max;
471 $self->{$size}->{'url'} = $name; # unscaled version
472 $self->{$size}->{'dim'} = [$w, $h];
474 $self->{$size}->{'url'} = $nref;
475 $self->{$size}->{'dim'} = [int($w*$factor+.5),
477 if (isnewer($fn,$nfn)) {
478 doscaling($fn,$nfn,$factor,$w,$h);
485 my ($fn1,$fn2) = @_; # this is not a method
486 my @stat1=stat($fn1);
487 my @stat2=stat($fn2);
488 return (!@stat2 || ($stat1[9] > $stat2[9]));
489 # true if $fn2 is absent or is older than $fn1
493 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
497 my $im = new Image::Magick;
498 print "doscaling $src -> $dest by $factor\n" if ($debug);
499 if ($err = $im->Read($src)) {
500 warn "ImageMagick: read \"$src\": $err";
502 $im->Scale(width=>$w*$factor,height=>$h*$factor);
503 $err=$im->Write($dest);
504 warn "ImageMagick: write \"$dest\": $err" if ($err);
508 if ($err) { # fallback to command-line tools
509 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
515 my $name = $self->{-base};
516 my $dn = $self->{-parent}->{-fullpath};
517 my $pref = $self->{-previmg}->{-base};
518 my $nref = $self->{-nextimg}->{-base};
519 my $inc = $self->{-inc}.$incdir.'/';
520 my $title = $self->{-info}->{'Comment'};
521 $title = $name unless ($title);
523 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
526 for my $refresh('static', 'slide') {
527 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
528 if (isnewer($self->{-fullpath},$fn)) {
529 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
533 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
535 $fwdref = '../index.html';
538 $bakref = sprintf("%s-%s.html",$pref,$refresh);
540 $bakref = '../index.html';
544 if ($refresh eq 'slide') {
545 $toggleref=sprintf("%s-static.html",$name);
546 $toggletext = 'Stop!';
548 $toggleref=sprintf("%s-slide.html",$name);
549 $toggletext = 'Play->';
552 unless (open($F,'>'.$fn)) {
553 warn "cannot open \"$fn\": $!";
556 binmode($F, ":utf8");
557 if ($refresh eq 'slide') {
562 -head=>meta({-http_equiv=>'Refresh',
563 -content=>"3; url=$fwdref"}),
564 -style=>{-src=>$inc."gallery.css"},
566 comment("Created by ".$version),"\n";
569 print $F start_html(-title=>$title,
572 -style=>{-src=>$inc."gallery.css"},
574 comment("Created by ".$version),"\n";
576 print $F start_table({-class=>'navi'}),start_Tr,"\n",
577 td(a({-href=>"../index.html"},"Index")),"\n",
578 td(a({-href=>$bakref},"<<Prev")),"\n",
579 td(a({-href=>$toggleref},$toggletext)),"\n",
580 td(a({-href=>$fwdref},"Next>>")),"\n",
581 td({-class=>'title'},$title),"\n",
584 center(table({-class=>'picframe'},
585 Tr(td(img({-src=>$imgsrc,
586 -class=>'standalone',
587 -alt=>$title}))))),"\n",
594 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
595 if (isnewer($self->{-fullpath},$fn)) {
597 unless (open($F,'>'.$fn)) {
598 warn "cannot open \"$fn\": $!";
601 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
602 print $F start_html(-title=>$title,
604 -style=>{-src=>$inc."gallery.css"},
606 {-src=>$inc."mootools.js"},
607 {-src=>$inc."urlparser.js"},
608 {-src=>$inc."infopage.js"},
610 comment("Created by ".$version),"\n",
613 table({-class=>'ipage'},
614 Tr(td(img({-src=>$imgsrc,
617 td($self->infotable))),
618 a({-href=>'../index.html',-class=>'conceal'},
628 my $fn = $self->{-fullpath}.'/index.html';
629 my $block = $self->{-fullpath}.'/.noindex';
630 $fn = '/dev/null' if ( -f $block );
632 unless (open($IND,'>'.$fn)) {
633 warn "cannot open $fn: $!";
636 binmode($IND, ":utf8");
637 $self->{-IND} = $IND;
639 my $inc = $self->{-inc}.$incdir.'/';
640 my $title = $self->{-title};
643 $rsslink=Link({-rel=>'alternate',
644 -type=>'application/rss+xml',
646 -href=>$self->{-inc}.$rssfile});
648 print $IND start_html(-title => $title,
652 {-src=>$inc."gallery.css"},
653 {-src=>$inc."custom.css"},
656 {-src=>$inc."mootools.js"},
657 {-src=>$inc."overlay.js"},
658 {-src=>$inc."urlparser.js"},
659 {-src=>$inc."multibox.js"},
660 {-src=>$inc."showwin.js"},
661 {-src=>$inc."controls.js"},
662 {-src=>$inc."show.js"},
663 {-src=>$inc."gallery.js"},
665 comment("Created by ".$version),"\n",
666 start_div({-class => 'indexContainer',
667 -id => 'indexContainer'}),
670 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/header.pl')) {
677 -version => $version,
678 -depth => $self->{-depth},
680 -breadcrumbs => "breadcrumbs unimplemented",
682 print $IND eval $prm,"\n";
684 print STDERR "could not open ",
685 $self->{-toppath}.'/'.$incdir.'/header.pl',
686 " ($!), reverting to default header";
687 print $IND a({-href=>"../index.html"},"UP"),"\n",
688 h1({-class=>'title'},$title),"\n",
694 my $IND = $self->{-IND};
698 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/footer.pl')) {
705 -version => $version,
706 -depth => $self->{-depth},
707 -title => $self->{-title},
708 -breadcrumbs => "breadcrumbs unimplemented",
710 print $IND eval $prm,"\n";
712 print STDERR "could not open ",
713 $self->{-toppath}.'/'.$incdir.'/footer.pl',
714 " ($!), reverting to default empty footer";
716 print $IND end_html,"\n";
718 close($IND) if ($IND);
721 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
725 my $rsslink=$rssobj->{'rss'}->channel('link').
726 $self->{-relpath}."index.html";
727 $rssobj->{'rss'}->add_item(
728 title => $self->{-title},
730 description => $rsstitle,
731 pubDate => time2str(time),
738 my $IND = $self->{-IND};
740 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
745 my $IND = $self->{-parent}->{-IND};
746 my $name = $self->{-base};
747 my $title = $self->{-title};
749 $self->{-parent}->{-numofsubs}++;
750 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
751 td(a({-href=>$name.'/index.html'},$title))),"\n";
756 my $IND = $self->{-IND};
758 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
763 my $IND = $self->{-IND};
764 my $first = $self->{-firstimg}->{-base};
765 my $slideref = sprintf(".html/%s-slide.html",$first);
767 print $IND h2({-class=>"ititle"},"Images ",
768 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
769 '> slideshow')),"\n";
774 my $IND = $self->{-parent}->{-IND};
775 my $name = $self->{-base};
776 my $title = $self->{-info}->{'Comment'};
777 $title = $name unless ($title);
778 my $thumb = $self->{$sizes[0]}->{'url'};
779 my $info = $self->{-info};
780 my ($w, $h) = dim($info);
782 my $i=0+$self->{-parent}->{-numofimgs};
783 $self->{-parent}->{-numofimgs}++;
785 print $IND a({-name=>$name}),"\n",
786 start_table({-class=>'slide'}),start_Tr,start_td,"\n",
787 div({-class=>'slidetitle'},
788 "\n ",a({-href=>".html/$name-info.html",
789 -title=>'Image Info: '.$name,
792 div({-class=>'slideimage'},
793 "\n ",a({-href=>".html/$name-static.html",
799 -alt=>$title})),"\n"),"\n",
800 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
801 foreach my $sz(@sizes) {
802 my $src=$self->{$sz}->{'url'};
803 my $w=$self->{$sz}->{'dim'}->[0];
804 my $h=$self->{$sz}->{'dim'}->[1];
805 print $IND " ",a({-href=>$src,
808 -title=>"Reduced to ".$w."x".$h},
811 print $IND " ",a({-href=>$name,
813 -title=>'Original'},$w."x".$h),
815 end_td,end_Tr,end_table,"\n";
820 my $IND = $self->{-IND};
822 print $IND br({-clear=>'all'}),hr,"\n\n";
827 my $info = $self->{-info};
844 $msg.=start_table({-class=>'infotable'})."\n";
845 foreach my $k(@infokeys) {
846 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
848 $msg.=end_table."\n";