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/;
32 use CGI qw/:html *table *Tr *td *center *div *Link/;
33 use Image::Info qw/image_info dim/;
38 binmode(STDOUT, ":utf8");
40 my $haveimagick = eval { require Image::Magick; };
41 { package Image::Magick; } # to make perl compiler happy
43 my $haverssxml = eval { require XML::RSS; };
44 { package XML::RSS; } # to make perl compiler happy
46 my @sizes = (160, 640, 1600);
48 ######################################################################
62 'asktitle'=>\$asktitle,
63 'noasktitle'=>\$noasktitle,
64 'rssfile=s'=>\$rssfile,
69 if ($rssfile && ! $haverssxml) {
70 print STDERR "You need to get XML::RSS from CPAN to use --rssfile\n";
74 my $term = new Term::ReadLine "Edit Title";
76 FsObj->new(getcwd)->iterate;
77 if ($rssobj) { $rssobj->{'rss'}->save($rssobj->{'file'}); }
81 print STDERR <<__END__;
83 --help: print help message and exit
84 --incpath: do not try to find .gallery2 diretory upstream, use
85 specified path (absolute or relavive). Use with causion.
86 --debug: print a lot of debugging info to stdout as you run
87 --asktitle: ask to edit album titles even if there are ".title" files
88 --noasktitle: don't ask to enter album titles even where ".title"
89 files are absent. Use partial directory names as titles.
90 --rssfile=...: build RSS feed for newly added "albums", give name of rss file
106 -root=>$parent->{-root},
107 -toppath=>$parent->{-toppath},
108 -depth=>$parent->{-depth}+1,
110 -fullpath=>$parent->{-fullpath}.'/'.$name,
111 -relpath=>$parent->{-relpath}.'/'.$name,
112 -inc=>'../'.$parent->{-inc},
122 # fill in -inc, -rss, -relpath
123 initpaths($self); # we are not blessed yet, so cheat.
127 print "new $class:\n";
128 foreach my $k(keys %$self) {
129 print "\t$k\t=\t$self->{$k}\n";
136 my $self=shift; # this is not a method but we cheat
137 my $depth=20; # arbitrary max depth
138 my $fullpath=$self->{-fullpath};
144 $inc = $incpath."/.gallery2";
147 while ( ! -d $fullpath."/".$inc ) {
149 last unless ($depth-- > 0);
153 $self->{-inc} = $inc.'/';
156 for ($pos=index($inc,'/');$pos>=0;
157 $pos=index($inc,'/',$pos+1)) {
160 for ($pos=length($fullpath);$dp-->0 && $pos>0;
161 $pos=rindex($fullpath,'/',$pos-1)) {;}
162 my $relpath = substr($fullpath,$pos);
164 $self->{-relpath} = $relpath;
165 $self->{-toppath} = substr($fullpath,0,$pos);
168 $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
170 $self->{-relpath} = '';
175 my $self=shift; # this is not a method but we cheat
176 my $fullpath=$self->{-fullpath};
180 return "" unless $rssfile;
183 while ( ! -f $fullpath."/".$rss ) {
185 last unless ($depth-- > 0);
188 $rssobj->{'file'} = $rss;
189 $rssobj->{'rss'} = new XML::RSS (version=>2);
190 $rssobj->{'rss'}->parsefile($rss);
191 my $itemstodel = @{$rssobj->{'rss'}->{'items'}} - 15;
192 while ($itemstodel-- > 0) {
193 pop(@{$rssobj->{'rss'}->{'items'}})
195 $rssobj->{'rss'}->save($rssobj->{'file'});
198 print STDERR "There is no $rssfile in this or parent ".
199 "directories, you must create one with mkgalrss.pl\n";
206 my $fullpath .= $self->{-fullpath};
207 print "iterate in dir $fullpath\n" if ($debug);
213 unless (opendir($D,$fullpath)) {
214 warn "cannot opendir $fullpath: $!";
217 while (my $de = readdir($D)) {
218 next if ($de =~ /^\./);
219 my $child = $self->new($de);
220 my @stat = stat($child->{-fullpath});
221 $youngest = $stat[9] if ($youngest < $stat[9]);
223 push(@rdirlist,$child);
224 } elsif ($child->isimg) {
225 push(@rimglist,$child);
229 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
230 undef @rdirlist; # inplace sorting would be handy here
231 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
232 undef @rimglist; # optimize away unsorted versions
233 $self->{-firstimg} = $imglist[0];
235 print "Dir: $self->{-fullpath}\n" if ($debug);
237 # 1. first of all, fill title for this directory and create hidden subdirs
241 # 2. recurse into subdirectories to get their titles filled
242 # before we start writing out subalbum list
244 foreach my $dir(@dirlist) {
248 # 3. iterate through images to build cross-links,
251 foreach my $img(@imglist) {
252 # list-linking must be done before generating
253 # aux html because aux pages rely on prev/next refs
255 $previmg->{-nextimg} = $img;
256 $img->{-previmg} = $previmg;
261 # 4. create scaled versions and aux html pages
263 foreach my $img(@imglist) {
264 # scaled versions must be generated before aux html
265 # and main image index because they both rely on
266 # refs to scaled images and they may be just original
267 # images, this is not known before we try scaling.
269 # finally, make aux html pages
273 # no need to go beyond this point if the directory timestamp did not
274 # change since we built index.html file last time.
276 my @istat = stat($self->{-fullpath}.'/index.html');
277 return unless ($youngest > $istat[9]);
279 # 5. start building index.html for the directory
283 # 6. iterate through subdirectories to build subalbums list
287 foreach my $dir(@dirlist) {
293 # 7. iterate through images to build thumb list
297 foreach my $img(@imglist) {
298 print "Img: $img->{-fullpath}\n" if ($debug);
304 # 8. comlplete building index.html for the directory
311 return ( -d $self->{-fullpath} );
316 my $fullpath = $self->{-fullpath};
317 return 0 unless ( -f $fullpath );
318 my $info = image_info($fullpath);
319 if (my $error = $info->{error}) {
320 if (($error !~ "Unrecognized file format") &&
321 ($error !~ "Can't read head")) {
322 warn "File \"$fullpath\": $error\n";
327 tryapp12($info) unless ($info->{'ExifVersion'});
330 $self->{-info} = $info;
335 my $info = shift; # this is not a method
337 # dirty hack to take care of Image::Info parser strangeness
338 foreach my $k(keys %$info) {
339 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
341 return unless ($app12); # bad luck
343 foreach my $ln(split /[\r\n]+/,$app12) {
344 $ln =~ s/[[:^print:]\000]/ /g;
345 unless ($seenfirstline) {
350 my ($k,$v)=split /=/,$ln,2;
351 if ($k eq 'TimeDate') {
352 $info->{'DateTime'} =
353 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
355 } elsif ($k eq 'Shutter') {
356 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
357 } elsif ($k eq 'Flash') {
358 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
359 } elsif ($k eq 'Type') {
360 $info->{'Model'} = $v;
361 } elsif ($k eq 'Version') {
362 $info->{'Software'} = $v;
363 } elsif ($k eq 'Fnumber') {
364 $info->{'FNumber'} = $v;
371 my $fullpath = $self->{-fullpath};
372 for my $subdir(@sizes, 'html') {
373 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
374 mkdir($tdir,0755) unless ( -d $tdir );
381 my $fullpath = $self->{-fullpath};
384 if (open($T,'<'.$fullpath.'/.title')) {
386 $title =~ s/[\r\n]*$//;
389 if ($asktitle || (!$title && !$noasktitle)) {
390 my $prompt = $self->{-relpath};
391 $prompt = '/' unless ($prompt);
392 my $OUT = $term->OUT || \*STDOUT;
393 print $OUT "Enter title for $fullpath\n";
394 $title = $term->readline($prompt.' >',$title);
395 $term->addhistory($title) if ($title);
396 if (open($T,'>'.$fullpath.'/.title')) {
397 print $T $title,"\n";
402 $title=$self->{-relpath};
404 $self->{-title}=$title;
405 print "title in $fullpath is $title\n" if ($debug);
410 my $fn = $self->{-fullpath};
411 my $name = $self->{-base};
412 my $dn = $self->{-parent}->{-fullpath};
413 my ($w, $h) = dim($self->{-info});
414 my $max = ($w > $h)?$w:$h;
416 foreach my $size(@sizes) {
417 my $nref = '.'.$size.'/'.$name;
418 my $nfn = $dn.'/'.$nref;
419 my $factor=$size/$max;
421 $self->{$size}->{'url'} = $name; # unscaled version
422 $self->{$size}->{'dim'} = [$w, $h];
424 $self->{$size}->{'url'} = $nref;
425 $self->{$size}->{'dim'} = [int($w*$factor+.5),
427 if (isnewer($fn,$nfn)) {
428 doscaling($fn,$nfn,$factor,$w,$h);
435 my ($fn1,$fn2) = @_; # this is not a method
436 my @stat1=stat($fn1);
437 my @stat2=stat($fn2);
438 return (!@stat2 || ($stat1[9] > $stat2[9]));
439 # true if $fn2 is absent or is older than $fn1
443 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
447 my $im = new Image::Magick;
448 print "doscaling $src -> $dest by $factor\n" if ($debug);
449 if ($err = $im->Read($src)) {
450 warn "ImageMagick: read \"$src\": $err";
452 $im->Scale(width=>$w*$factor,height=>$h*$factor);
453 $err=$im->Write($dest);
454 warn "ImageMagick: write \"$dest\": $err" if ($err);
458 if ($err) { # fallback to command-line tools
459 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
465 my $name = $self->{-base};
466 my $dn = $self->{-parent}->{-fullpath};
467 my $pref = $self->{-previmg}->{-base};
468 my $nref = $self->{-nextimg}->{-base};
469 my $inc = $self->{-inc};
470 my $title = $self->{-info}->{'Comment'};
471 $title = $name unless ($title);
473 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
476 for my $refresh('static', 'slide') {
477 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
478 if (isnewer($self->{-fullpath},$fn)) {
479 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
483 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
485 $fwdref = '../index.html';
488 $bakref = sprintf("%s-%s.html",$pref,$refresh);
490 $bakref = '../index.html';
494 if ($refresh eq 'slide') {
495 $toggleref=sprintf("%s-static.html",$name);
496 $toggletext = 'Stop!';
498 $toggleref=sprintf("%s-slide.html",$name);
499 $toggletext = 'Play->';
502 unless (open($F,'>'.$fn)) {
503 warn "cannot open \"$fn\": $!";
506 binmode($F, ":utf8");
507 if ($refresh eq 'slide') {
512 -head=>meta({-http_equiv=>'Refresh',
513 -content=>"3; url=$fwdref"}),
514 -style=>{-src=>$inc."gallery.css"},
516 comment("Created by ".$version),"\n";
519 print $F start_html(-title=>$title,
522 -style=>{-src=>$inc."gallery.css"},
524 comment("Created by ".$version),"\n";
526 print $F start_table({-class=>'navi'}),start_Tr,"\n",
527 td(a({-href=>"../index.html"},"Index")),"\n",
528 td(a({-href=>$bakref},"<<Prev")),"\n",
529 td(a({-href=>$toggleref},$toggletext)),"\n",
530 td(a({-href=>$fwdref},"Next>>")),"\n",
531 td({-class=>'title'},$title),"\n",
534 center(table({-class=>'picframe'},
535 Tr(td(img({-src=>$imgsrc,
536 -class=>'standalone',
537 -alt=>$title}))))),"\n",
544 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
545 if (isnewer($self->{-fullpath},$fn)) {
547 unless (open($F,'>'.$fn)) {
548 warn "cannot open \"$fn\": $!";
551 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
552 print $F start_html(-title=>$title,
554 -style=>{-src=>$inc."gallery.css"},
556 {-src=>$inc."mootools.js"},
557 {-src=>$inc."urlparser.js"},
558 {-src=>$inc."infopage.js"},
560 comment("Created by ".$version),"\n",
563 table({-class=>'ipage'},
564 Tr(td(img({-src=>$imgsrc,
567 td($self->infotable))),
568 a({-href=>'../index.html',-class=>'conceal'},
578 my $fn = $self->{-fullpath}.'/index.html';
579 my $block = $self->{-fullpath}.'/.noindex';
580 $fn = '/dev/null' if ( -f $block );
582 unless (open($IND,'>'.$fn)) {
583 warn "cannot open $fn: $!";
586 binmode($IND, ":utf8");
587 $self->{-IND} = $IND;
589 my $inc = $self->{-inc};
590 my $title = $self->{-title};
593 $rsslink=Link({-rel=>'alternate',
594 -type=>'application/rss+xml',
596 -href=>$self->{-rss}});
598 print $IND start_html(-title => $title,
602 {-src=>$inc."gallery.css"},
603 {-src=>$inc."custom.css"},
606 {-src=>$inc."mootools.js"},
607 {-src=>$inc."overlay.js"},
608 {-src=>$inc."urlparser.js"},
609 {-src=>$inc."multibox.js"},
610 {-src=>$inc."showwin.js"},
611 {-src=>$inc."controls.js"},
612 {-src=>$inc."show.js"},
613 {-src=>$inc."gallery.js"},
615 comment("Created by ".$version),"\n",
616 start_div({-class => 'indexContainer',
617 -id => 'indexContainer'}),
620 if (open($EVL,$inc.'header.pl')) {
627 -version => $version,
628 -depth => $self->{-depth},
630 -breadcrumbs => "breadcrumbs unimplemented",
632 print $IND eval $prm,"\n";
634 print $IND a({-href=>"../index.html"},"UP"),"\n",
635 h1({-class=>'title'},$title),"\n",
641 my $IND = $self->{-IND};
645 if (open($EVL,$self->{-inc}.'footer.pl')) {
652 -version => $version,
653 -depth => $self->{-depth},
654 -title => $self->{-title},
655 -breadcrumbs => "breadcrumbs unimplemented",
657 print $IND eval $prm,"\n";
659 print $IND end_html,"\n";
661 close($IND) if ($IND);
664 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
668 my $rsslink=$rssobj->{'rss'}->channel('link')."index.html";
669 $rssobj->{'rss'}->add_item(
670 title => $self->{-title},
672 description => $rsstitle,
679 my $IND = $self->{-IND};
681 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
686 my $IND = $self->{-parent}->{-IND};
687 my $name = $self->{-base};
688 my $title = $self->{-title};
690 $self->{-parent}->{-numofsubs}++;
691 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
692 td(a({-href=>$name.'/index.html'},$title))),"\n";
697 my $IND = $self->{-IND};
699 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
704 my $IND = $self->{-IND};
705 my $first = $self->{-firstimg}->{-base};
706 my $slideref = sprintf(".html/%s-slide.html",$first);
708 print $IND h2({-class=>"ititle"},"Images ",
709 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
710 '> slideshow')),"\n";
715 my $IND = $self->{-parent}->{-IND};
716 my $name = $self->{-base};
717 my $title = $self->{-info}->{'Comment'};
718 $title = $name unless ($title);
719 my $thumb = $self->{$sizes[0]}->{'url'};
720 my $info = $self->{-info};
721 my ($w, $h) = dim($info);
723 my $i=0+$self->{-parent}->{-numofimgs};
724 $self->{-parent}->{-numofimgs}++;
726 print $IND a({-name=>$name}),"\n",
727 start_table({-class=>'slide'}),start_Tr,start_td,"\n",
728 div({-class=>'slidetitle'},
729 "\n ",a({-href=>".html/$name-info.html",
730 -title=>'Image Info: '.$name,
733 div({-class=>'slideimage'},
734 "\n ",a({-href=>".html/$name-static.html",
740 -alt=>$title})),"\n"),"\n",
741 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
742 foreach my $sz(@sizes) {
743 my $src=$self->{$sz}->{'url'};
744 my $w=$self->{$sz}->{'dim'}->[0];
745 my $h=$self->{$sz}->{'dim'}->[1];
746 print $IND " ",a({-href=>$src,
749 -title=>"Reduced to ".$w."x".$h},
752 print $IND " ",a({-href=>$name,
754 -title=>'Original'},$w."x".$h),
756 end_td,end_Tr,end_table,"\n";
761 my $IND = $self->{-IND};
763 print $IND br({-clear=>'all'}),hr,"\n\n";
768 my $info = $self->{-info};
785 $msg.=start_table({-class=>'infotable'})."\n";
786 foreach my $k(@infokeys) {
787 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
789 $msg.=end_table."\n";