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
104 my $fullpath = $parent->{-fullpath}.'/'.$name;
107 -root=>$parent->{-root},
108 -depth=>$parent->{-depth}+1,
110 -fullpath=>$fullpath,
111 -inc=>'../'.$parent->{-inc},
112 -rss=>'../'.$parent->{-rss},
127 print "new $class:\n";
128 foreach my $k(keys %$self) {
129 print "\t$k\t=\t$self->{$k}\n";
136 my $fullpath=shift; # this is not a method
137 my $depth=20; # arbitrary max depth
140 return $incpath."/.gallery2";
144 while ( ! -d $fullpath."/".$inc ) {
146 last unless ($depth-- > 0);
149 return $inc.'/'; # prefix with trailing slash
151 return 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
156 my $fullpath=shift; # this is not a method
157 my $depth=20; # arbitrary max depth
159 return "" unless $rssfile;
162 while ( ! -f $fullpath."/".$rss ) {
164 last unless ($depth-- > 0);
167 $rssobj->{'file'} = $rss;
168 $rssobj->{'rss'} = new XML::RSS (version=>2);
169 $rssobj->{'rss'}->parsefile($rss);
170 my $itemstodel = @{$rssobj->{'rss'}->{'items'}} - 15;
171 while ($itemstodel-- > 0) {
172 pop(@{$rssobj->{'rss'}->{'items'}})
174 $rssobj->{'rss'}->save($rssobj->{'file'});
177 print STDERR "There is no $rssfile in this or parent ".
178 "directories, you must create one with mkgalrss.pl\n";
185 my $fullpath .= $self->{-fullpath};
186 print "iterate in dir $fullpath\n" if ($debug);
192 unless (opendir($D,$fullpath)) {
193 warn "cannot opendir $fullpath: $!";
196 while (my $de = readdir($D)) {
197 next if ($de =~ /^\./);
198 my $child = $self->new($de);
199 my @stat = stat($child->{-fullpath});
200 $youngest = $stat[9] if ($youngest < $stat[9]);
202 push(@rdirlist,$child);
203 } elsif ($child->isimg) {
204 push(@rimglist,$child);
208 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
209 undef @rdirlist; # inplace sorting would be handy here
210 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
211 undef @rimglist; # optimize away unsorted versions
212 $self->{-firstimg} = $imglist[0];
214 print "Dir: $self->{-fullpath}\n" if ($debug);
216 # 1. first of all, fill title for this directory and create hidden subdirs
220 # 2. recurse into subdirectories to get their titles filled
221 # before we start writing out subalbum list
223 foreach my $dir(@dirlist) {
227 # 3. iterate through images to build cross-links,
230 foreach my $img(@imglist) {
231 # list-linking must be done before generating
232 # aux html because aux pages rely on prev/next refs
234 $previmg->{-nextimg} = $img;
235 $img->{-previmg} = $previmg;
240 # 4. create scaled versions and aux html pages
242 foreach my $img(@imglist) {
243 # scaled versions must be generated before aux html
244 # and main image index because they both rely on
245 # refs to scaled images and they may be just original
246 # images, this is not known before we try scaling.
248 # finally, make aux html pages
252 # no need to go beyond this point if the directory timestamp did not
253 # change since we built index.html file last time.
255 my @istat = stat($self->{-fullpath}.'/index.html');
256 return unless ($youngest > $istat[9]);
258 # 5. start building index.html for the directory
262 # 6. iterate through subdirectories to build subalbums list
266 foreach my $dir(@dirlist) {
272 # 7. iterate through images to build thumb list
276 foreach my $img(@imglist) {
277 print "Img: $img->{-fullpath}\n" if ($debug);
283 # 8. comlplete building index.html for the directory
290 return ( -d $self->{-fullpath} );
295 my $fullpath = $self->{-fullpath};
296 return 0 unless ( -f $fullpath );
297 my $info = image_info($fullpath);
298 if (my $error = $info->{error}) {
299 if (($error !~ "Unrecognized file format") &&
300 ($error !~ "Can't read head")) {
301 warn "File \"$fullpath\": $error\n";
306 tryapp12($info) unless ($info->{'ExifVersion'});
309 $self->{-info} = $info;
314 my $info = shift; # this is not a method
316 # dirty hack to take care of Image::Info parser strangeness
317 foreach my $k(keys %$info) {
318 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
320 return unless ($app12); # bad luck
322 foreach my $ln(split /[\r\n]+/,$app12) {
323 $ln =~ s/[[:^print:]\000]/ /g;
324 unless ($seenfirstline) {
329 my ($k,$v)=split /=/,$ln,2;
330 if ($k eq 'TimeDate') {
331 $info->{'DateTime'} =
332 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
334 } elsif ($k eq 'Shutter') {
335 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
336 } elsif ($k eq 'Flash') {
337 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
338 } elsif ($k eq 'Type') {
339 $info->{'Model'} = $v;
340 } elsif ($k eq 'Version') {
341 $info->{'Software'} = $v;
342 } elsif ($k eq 'Fnumber') {
343 $info->{'FNumber'} = $v;
350 my $fullpath = $self->{-fullpath};
351 for my $subdir(@sizes, 'html') {
352 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
353 mkdir($tdir,0755) unless ( -d $tdir );
360 my $fullpath = $self->{-fullpath};
363 if (open($T,'<'.$fullpath.'/.title')) {
365 $title =~ s/[\r\n]*$//;
368 if ($asktitle || (!$title && !$noasktitle)) {
369 my $prompt = $self->{-base};
370 $prompt = '/' unless ($prompt);
371 my $OUT = $term->OUT || \*STDOUT;
372 print $OUT "Enter title for $fullpath\n";
373 $title = $term->readline($prompt.' >',$title);
374 $term->addhistory($title) if ($title);
375 if (open($T,'>'.$fullpath.'/.title')) {
376 print $T $title,"\n";
381 $title=substr($fullpath,length($self->{-root}));
383 $self->{-title}=$title;
384 print "title in $fullpath is $title\n" if ($debug);
389 my $fn = $self->{-fullpath};
390 my $name = $self->{-base};
391 my $dn = $self->{-parent}->{-fullpath};
392 my ($w, $h) = dim($self->{-info});
393 my $max = ($w > $h)?$w:$h;
395 foreach my $size(@sizes) {
396 my $nref = '.'.$size.'/'.$name;
397 my $nfn = $dn.'/'.$nref;
398 my $factor=$size/$max;
400 $self->{$size}->{'url'} = $name; # unscaled version
401 $self->{$size}->{'dim'} = [$w, $h];
403 $self->{$size}->{'url'} = $nref;
404 $self->{$size}->{'dim'} = [int($w*$factor+.5),
406 if (isnewer($fn,$nfn)) {
407 doscaling($fn,$nfn,$factor,$w,$h);
414 my ($fn1,$fn2) = @_; # this is not a method
415 my @stat1=stat($fn1);
416 my @stat2=stat($fn2);
417 return (!@stat2 || ($stat1[9] > $stat2[9]));
418 # true if $fn2 is absent or is older than $fn1
422 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
426 my $im = new Image::Magick;
427 print "doscaling $src -> $dest by $factor\n" if ($debug);
428 if ($err = $im->Read($src)) {
429 warn "ImageMagick: read \"$src\": $err";
431 $im->Scale(width=>$w*$factor,height=>$h*$factor);
432 $err=$im->Write($dest);
433 warn "ImageMagick: write \"$dest\": $err" if ($err);
437 if ($err) { # fallback to command-line tools
438 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
444 my $name = $self->{-base};
445 my $dn = $self->{-parent}->{-fullpath};
446 my $pref = $self->{-previmg}->{-base};
447 my $nref = $self->{-nextimg}->{-base};
448 my $inc = $self->{-inc};
449 my $title = $self->{-info}->{'Comment'};
450 $title = $name unless ($title);
452 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
455 for my $refresh('static', 'slide') {
456 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
457 if (isnewer($self->{-fullpath},$fn)) {
458 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
462 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
464 $fwdref = '../index.html';
467 $bakref = sprintf("%s-%s.html",$pref,$refresh);
469 $bakref = '../index.html';
473 if ($refresh eq 'slide') {
474 $toggleref=sprintf("%s-static.html",$name);
475 $toggletext = 'Stop!';
477 $toggleref=sprintf("%s-slide.html",$name);
478 $toggletext = 'Play->';
481 unless (open($F,'>'.$fn)) {
482 warn "cannot open \"$fn\": $!";
485 binmode($F, ":utf8");
486 if ($refresh eq 'slide') {
491 -head=>meta({-http_equiv=>'Refresh',
492 -content=>"3; url=$fwdref"}),
493 -style=>{-src=>$inc."gallery.css"},
495 comment("Created by ".$version),"\n";
498 print $F start_html(-title=>$title,
501 -style=>{-src=>$inc."gallery.css"},
503 comment("Created by ".$version),"\n";
505 print $F start_table({-class=>'navi'}),start_Tr,"\n",
506 td(a({-href=>"../index.html"},"Index")),"\n",
507 td(a({-href=>$bakref},"<<Prev")),"\n",
508 td(a({-href=>$toggleref},$toggletext)),"\n",
509 td(a({-href=>$fwdref},"Next>>")),"\n",
510 td({-class=>'title'},$title),"\n",
513 center(table({-class=>'picframe'},
514 Tr(td(img({-src=>$imgsrc,
515 -class=>'standalone',
516 -alt=>$title}))))),"\n",
523 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
524 if (isnewer($self->{-fullpath},$fn)) {
526 unless (open($F,'>'.$fn)) {
527 warn "cannot open \"$fn\": $!";
530 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
531 print $F start_html(-title=>$title,
533 -style=>{-src=>$inc."gallery.css"},
535 {-src=>$inc."mootools.js"},
536 {-src=>$inc."urlparser.js"},
537 {-src=>$inc."infopage.js"},
539 comment("Created by ".$version),"\n",
542 table({-class=>'ipage'},
543 Tr(td(img({-src=>$imgsrc,
546 td($self->infotable))),
547 a({-href=>'../index.html',-class=>'conceal'},
557 my $fn = $self->{-fullpath}.'/index.html';
558 my $block = $self->{-fullpath}.'/.noindex';
559 $fn = '/dev/null' if ( -f $block );
561 unless (open($IND,'>'.$fn)) {
562 warn "cannot open $fn: $!";
565 binmode($IND, ":utf8");
566 $self->{-IND} = $IND;
568 my $inc = $self->{-inc};
569 my $title = $self->{-title};
572 $rsslink=Link({-rel=>'alternate',
573 -type=>'application/rss+xml',
575 -href=>$self->{-rss}});
577 print $IND start_html(-title => $title,
581 {-src=>$inc."gallery.css"},
582 {-src=>$inc."custom.css"},
585 {-src=>$inc."mootools.js"},
586 {-src=>$inc."overlay.js"},
587 {-src=>$inc."urlparser.js"},
588 {-src=>$inc."multibox.js"},
589 {-src=>$inc."showwin.js"},
590 {-src=>$inc."controls.js"},
591 {-src=>$inc."show.js"},
592 {-src=>$inc."gallery.js"},
594 comment("Created by ".$version),"\n",
595 start_div({-class => 'indexContainer',
596 -id => 'indexContainer'}),
599 if (open($EVL,$inc.'header.pl')) {
606 -version => $version,
607 -depth => $self->{-depth},
609 -breadcrumbs => "breadcrumbs unimplemented",
611 print $IND eval $prm,"\n";
613 print $IND a({-href=>"../index.html"},"UP"),"\n",
614 h1({-class=>'title'},$title),"\n",
620 my $IND = $self->{-IND};
624 if (open($EVL,$self->{-inc}.'footer.pl')) {
631 -version => $version,
632 -depth => $self->{-depth},
633 -title => $self->{-title},
634 -breadcrumbs => "breadcrumbs unimplemented",
636 print $IND eval $prm,"\n";
638 print $IND end_html,"\n";
640 close($IND) if ($IND);
643 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
647 my $rsslink=$rssobj->{'rss'}->channel('link')."index.html";
648 $rssobj->{'rss'}->add_item(
649 title => $self->{-title},
651 description => $rsstitle,
658 my $IND = $self->{-IND};
660 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
665 my $IND = $self->{-parent}->{-IND};
666 my $name = $self->{-base};
667 my $title = $self->{-title};
669 $self->{-parent}->{-numofsubs}++;
670 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
671 td(a({-href=>$name.'/index.html'},$title))),"\n";
676 my $IND = $self->{-IND};
678 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
683 my $IND = $self->{-IND};
684 my $first = $self->{-firstimg}->{-base};
685 my $slideref = sprintf(".html/%s-slide.html",$first);
687 print $IND h2({-class=>"ititle"},"Images ",
688 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
689 '> slideshow')),"\n";
694 my $IND = $self->{-parent}->{-IND};
695 my $name = $self->{-base};
696 my $title = $self->{-info}->{'Comment'};
697 $title = $name unless ($title);
698 my $thumb = $self->{$sizes[0]}->{'url'};
699 my $info = $self->{-info};
700 my ($w, $h) = dim($info);
702 my $i=0+$self->{-parent}->{-numofimgs};
703 $self->{-parent}->{-numofimgs}++;
705 print $IND a({-name=>$name}),"\n",
706 start_table({-class=>'slide'}),start_Tr,start_td,"\n",
707 div({-class=>'slidetitle'},
708 "\n ",a({-href=>".html/$name-info.html",
709 -title=>'Image Info: '.$name,
712 div({-class=>'slideimage'},
713 "\n ",a({-href=>".html/$name-static.html",
719 -alt=>$title})),"\n"),"\n",
720 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
721 foreach my $sz(@sizes) {
722 my $src=$self->{$sz}->{'url'};
723 my $w=$self->{$sz}->{'dim'}->[0];
724 my $h=$self->{$sz}->{'dim'}->[1];
725 print $IND " ",a({-href=>$src,
728 -title=>"Reduced to ".$w."x".$h},
731 print $IND " ",a({-href=>$name,
733 -title=>'Original'},$w."x".$h),
735 end_td,end_Tr,end_table,"\n";
740 my $IND = $self->{-IND};
742 print $IND br({-clear=>'all'}),hr,"\n\n";
747 my $info = $self->{-info};
764 $msg.=start_table({-class=>'infotable'})."\n";
765 foreach my $k(@infokeys) {
766 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
768 $msg.=end_table."\n";