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);
47 my $incdir = ".gallery2";
49 ######################################################################
63 'asktitle'=>\$asktitle,
64 'noasktitle'=>\$noasktitle,
65 'rssfile=s'=>\$rssfile,
70 if ($rssfile && ! $haverssxml) {
71 print STDERR "You need to get XML::RSS from CPAN to use --rssfile\n";
75 my $term = new Term::ReadLine "Edit Title";
77 FsObj->new(getcwd)->iterate;
78 if ($rssobj) { $rssobj->{'rss'}->save($rssobj->{'file'}); }
82 print STDERR <<__END__;
84 --help: print help message and exit
85 --incpath: do not try to find .gallery2 diretory upstream, use
86 specified path (absolute or relavive). Use with causion.
87 --debug: print a lot of debugging info to stdout as you run
88 --asktitle: ask to edit album titles even if there are ".title" files
89 --noasktitle: don't ask to enter album titles even where ".title"
90 files are absent. Use partial directory names as titles.
91 --rssfile=...: build RSS feed for newly added "albums", give name of rss file
107 -root=>$parent->{-root},
108 -toppath=>$parent->{-toppath},
109 -depth=>$parent->{-depth}+1,
111 -fullpath=>$parent->{-fullpath}.'/'.$name,
112 -relpath=>$parent->{-relpath}.$name.'/',
113 -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 for ($pos=length($fullpath);$dp>0 && $pos>0;
162 $pos=rindex($fullpath,'/',$pos-1)) {
165 my $relpath = substr($fullpath,$pos);
167 $relpath .= '/' if ($relpath);
168 $self->{-relpath} = $relpath;
169 $self->{-toppath} = substr($fullpath,0,$pos);
170 #print "rel=$relpath, top=$self->{-toppath}, inc=$inc\n";
173 $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
175 $self->{-relpath} = '';
180 my $self=shift; # this is not a method but we cheat
181 my $fullpath=$self->{-fullpath};
182 my $inc=$self->{-inc}.$incdir.'/';
183 my $conffile=$self->{-toppath}.'/'.$incdir.'/rss.conf';
187 if (open($CONF,">".$conffile)) {
188 print $CONF "file: ",$rssfile,"\n";
191 print STDERR "could not open $conffile: $!\n";
194 if (open($CONF,$conffile)) {
198 my ($k,$v)=split(':', $ln);
209 return unless ($rssfile);
211 $rssobj->{'file'} = $self->{-toppath}.'/'.$rssfile;
212 $rssobj->{'rss'} = new XML::RSS (version=>'2.0');
213 if ( -f $rssobj->{'file'} ) {
214 $rssobj->{'rss'}->parsefile($rssobj->{'file'});
215 my $itemstodel = @{$rssobj->{'rss'}->{'items'}} - 15;
216 while ($itemstodel-- > 0) {
217 pop(@{$rssobj->{'rss'}->{'items'}})
219 $rssobj->{'rss'}->save($rssobj->{'file'});
222 for (my $pos=index($rssfile,'/');$pos>=0;
223 $pos=index($rssfile,'/',$pos+1)) {
227 $rssobj->{'rss'}->channel(
230 description=>'Gallery Feed',
231 #language=>$language,
233 #copyright=>$copyright,
235 #lastBuildDate=>$lastBuild,
237 #managingEditor=>$editor,
238 #webMaster=>$webMaster
240 $rssobj->{'rss'}->save($rssobj->{'file'});
242 $self->{-rss} = $rssobj->{'rss'};
247 my $fullpath .= $self->{-fullpath};
248 print "iterate in dir $fullpath\n" if ($debug);
254 unless (opendir($D,$fullpath)) {
255 warn "cannot opendir $fullpath: $!";
258 while (my $de = readdir($D)) {
259 next if ($de =~ /^\./);
260 my $child = $self->new($de);
261 my @stat = stat($child->{-fullpath});
262 $youngest = $stat[9] if ($youngest < $stat[9]);
264 push(@rdirlist,$child);
265 } elsif ($child->isimg) {
266 push(@rimglist,$child);
270 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
271 undef @rdirlist; # inplace sorting would be handy here
272 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
273 undef @rimglist; # optimize away unsorted versions
274 $self->{-firstimg} = $imglist[0];
276 print "Dir: $self->{-fullpath}\n" if ($debug);
278 # 1. first of all, fill title for this directory and create hidden subdirs
282 # 2. recurse into subdirectories to get their titles filled
283 # before we start writing out subalbum list
285 foreach my $dir(@dirlist) {
289 # 3. iterate through images to build cross-links,
292 foreach my $img(@imglist) {
293 # list-linking must be done before generating
294 # aux html because aux pages rely on prev/next refs
296 $previmg->{-nextimg} = $img;
297 $img->{-previmg} = $previmg;
302 # 4. create scaled versions and aux html pages
304 foreach my $img(@imglist) {
305 # scaled versions must be generated before aux html
306 # and main image index because they both rely on
307 # refs to scaled images and they may be just original
308 # images, this is not known before we try scaling.
310 # finally, make aux html pages
314 # no need to go beyond this point if the directory timestamp did not
315 # change since we built index.html file last time.
317 my @istat = stat($self->{-fullpath}.'/index.html');
318 return unless ($youngest > $istat[9]);
320 # 5. start building index.html for the directory
324 # 6. iterate through subdirectories to build subalbums list
328 foreach my $dir(@dirlist) {
334 # 7. iterate through images to build thumb list
338 foreach my $img(@imglist) {
339 print "Img: $img->{-fullpath}\n" if ($debug);
345 # 8. comlplete building index.html for the directory
352 return ( -d $self->{-fullpath} );
357 my $fullpath = $self->{-fullpath};
358 return 0 unless ( -f $fullpath );
359 my $info = image_info($fullpath);
360 if (my $error = $info->{error}) {
361 if (($error !~ "Unrecognized file format") &&
362 ($error !~ "Can't read head")) {
363 warn "File \"$fullpath\": $error\n";
368 tryapp12($info) unless ($info->{'ExifVersion'});
371 $self->{-info} = $info;
376 my $info = shift; # this is not a method
378 # dirty hack to take care of Image::Info parser strangeness
379 foreach my $k(keys %$info) {
380 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
382 return unless ($app12); # bad luck
384 foreach my $ln(split /[\r\n]+/,$app12) {
385 $ln =~ s/[[:^print:]\000]/ /g;
386 unless ($seenfirstline) {
391 my ($k,$v)=split /=/,$ln,2;
392 if ($k eq 'TimeDate') {
393 $info->{'DateTime'} =
394 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
396 } elsif ($k eq 'Shutter') {
397 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
398 } elsif ($k eq 'Flash') {
399 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
400 } elsif ($k eq 'Type') {
401 $info->{'Model'} = $v;
402 } elsif ($k eq 'Version') {
403 $info->{'Software'} = $v;
404 } elsif ($k eq 'Fnumber') {
405 $info->{'FNumber'} = $v;
412 my $fullpath = $self->{-fullpath};
413 for my $subdir(@sizes, 'html') {
414 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
415 mkdir($tdir,0755) unless ( -d $tdir );
422 my $fullpath = $self->{-fullpath};
425 if (open($T,'<'.$fullpath.'/.title')) {
427 $title =~ s/[\r\n]*$//;
430 if ($asktitle || (!$title && !$noasktitle)) {
431 my $prompt = $self->{-relpath};
432 $prompt = '/' unless ($prompt);
433 my $OUT = $term->OUT || \*STDOUT;
434 print $OUT "Enter title for $fullpath\n";
435 $title = $term->readline($prompt.' >',$title);
436 $term->addhistory($title) if ($title);
437 if (open($T,'>'.$fullpath.'/.title')) {
438 print $T $title,"\n";
443 $title=$self->{-relpath};
445 $self->{-title}=$title;
446 print "title in $fullpath is $title\n" if ($debug);
451 my $fn = $self->{-fullpath};
452 my $name = $self->{-base};
453 my $dn = $self->{-parent}->{-fullpath};
454 my ($w, $h) = dim($self->{-info});
455 my $max = ($w > $h)?$w:$h;
457 foreach my $size(@sizes) {
458 my $nref = '.'.$size.'/'.$name;
459 my $nfn = $dn.'/'.$nref;
460 my $factor=$size/$max;
462 $self->{$size}->{'url'} = $name; # unscaled version
463 $self->{$size}->{'dim'} = [$w, $h];
465 $self->{$size}->{'url'} = $nref;
466 $self->{$size}->{'dim'} = [int($w*$factor+.5),
468 if (isnewer($fn,$nfn)) {
469 doscaling($fn,$nfn,$factor,$w,$h);
476 my ($fn1,$fn2) = @_; # this is not a method
477 my @stat1=stat($fn1);
478 my @stat2=stat($fn2);
479 return (!@stat2 || ($stat1[9] > $stat2[9]));
480 # true if $fn2 is absent or is older than $fn1
484 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
488 my $im = new Image::Magick;
489 print "doscaling $src -> $dest by $factor\n" if ($debug);
490 if ($err = $im->Read($src)) {
491 warn "ImageMagick: read \"$src\": $err";
493 $im->Scale(width=>$w*$factor,height=>$h*$factor);
494 $err=$im->Write($dest);
495 warn "ImageMagick: write \"$dest\": $err" if ($err);
499 if ($err) { # fallback to command-line tools
500 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
506 my $name = $self->{-base};
507 my $dn = $self->{-parent}->{-fullpath};
508 my $pref = $self->{-previmg}->{-base};
509 my $nref = $self->{-nextimg}->{-base};
510 my $inc = $self->{-inc}.$incdir.'/';
511 my $title = $self->{-info}->{'Comment'};
512 $title = $name unless ($title);
514 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
517 for my $refresh('static', 'slide') {
518 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
519 if (isnewer($self->{-fullpath},$fn)) {
520 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
524 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
526 $fwdref = '../index.html';
529 $bakref = sprintf("%s-%s.html",$pref,$refresh);
531 $bakref = '../index.html';
535 if ($refresh eq 'slide') {
536 $toggleref=sprintf("%s-static.html",$name);
537 $toggletext = 'Stop!';
539 $toggleref=sprintf("%s-slide.html",$name);
540 $toggletext = 'Play->';
543 unless (open($F,'>'.$fn)) {
544 warn "cannot open \"$fn\": $!";
547 binmode($F, ":utf8");
548 if ($refresh eq 'slide') {
553 -head=>meta({-http_equiv=>'Refresh',
554 -content=>"3; url=$fwdref"}),
555 -style=>{-src=>$inc."gallery.css"},
557 comment("Created by ".$version),"\n";
560 print $F start_html(-title=>$title,
563 -style=>{-src=>$inc."gallery.css"},
565 comment("Created by ".$version),"\n";
567 print $F start_table({-class=>'navi'}),start_Tr,"\n",
568 td(a({-href=>"../index.html"},"Index")),"\n",
569 td(a({-href=>$bakref},"<<Prev")),"\n",
570 td(a({-href=>$toggleref},$toggletext)),"\n",
571 td(a({-href=>$fwdref},"Next>>")),"\n",
572 td({-class=>'title'},$title),"\n",
575 center(table({-class=>'picframe'},
576 Tr(td(img({-src=>$imgsrc,
577 -class=>'standalone',
578 -alt=>$title}))))),"\n",
585 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
586 if (isnewer($self->{-fullpath},$fn)) {
588 unless (open($F,'>'.$fn)) {
589 warn "cannot open \"$fn\": $!";
592 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
593 print $F start_html(-title=>$title,
595 -style=>{-src=>$inc."gallery.css"},
597 {-src=>$inc."mootools.js"},
598 {-src=>$inc."urlparser.js"},
599 {-src=>$inc."infopage.js"},
601 comment("Created by ".$version),"\n",
604 table({-class=>'ipage'},
605 Tr(td(img({-src=>$imgsrc,
608 td($self->infotable))),
609 a({-href=>'../index.html',-class=>'conceal'},
619 my $fn = $self->{-fullpath}.'/index.html';
620 my $block = $self->{-fullpath}.'/.noindex';
621 $fn = '/dev/null' if ( -f $block );
623 unless (open($IND,'>'.$fn)) {
624 warn "cannot open $fn: $!";
627 binmode($IND, ":utf8");
628 $self->{-IND} = $IND;
630 my $inc = $self->{-inc}.$incdir.'/';
631 my $title = $self->{-title};
634 $rsslink=Link({-rel=>'alternate',
635 -type=>'application/rss+xml',
637 -href=>$self->{-inc}.$rssfile});
639 print $IND start_html(-title => $title,
643 {-src=>$inc."gallery.css"},
644 {-src=>$inc."custom.css"},
647 {-src=>$inc."mootools.js"},
648 {-src=>$inc."overlay.js"},
649 {-src=>$inc."urlparser.js"},
650 {-src=>$inc."multibox.js"},
651 {-src=>$inc."showwin.js"},
652 {-src=>$inc."controls.js"},
653 {-src=>$inc."show.js"},
654 {-src=>$inc."gallery.js"},
656 comment("Created by ".$version),"\n",
657 start_div({-class => 'indexContainer',
658 -id => 'indexContainer'}),
661 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/header.pl')) {
668 -version => $version,
669 -depth => $self->{-depth},
671 -breadcrumbs => "breadcrumbs unimplemented",
673 print $IND eval $prm,"\n";
675 print $IND a({-href=>"../index.html"},"UP"),"\n",
676 h1({-class=>'title'},$title),"\n",
682 my $IND = $self->{-IND};
686 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/footer.pl')) {
693 -version => $version,
694 -depth => $self->{-depth},
695 -title => $self->{-title},
696 -breadcrumbs => "breadcrumbs unimplemented",
698 print $IND eval $prm,"\n";
700 print $IND end_html,"\n";
702 close($IND) if ($IND);
705 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
709 my $rsslink=$rssobj->{'rss'}->channel('link').
710 $self->{-relpath}."index.html";
711 $rssobj->{'rss'}->add_item(
712 title => $self->{-title},
714 description => $rsstitle,
721 my $IND = $self->{-IND};
723 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
728 my $IND = $self->{-parent}->{-IND};
729 my $name = $self->{-base};
730 my $title = $self->{-title};
732 $self->{-parent}->{-numofsubs}++;
733 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
734 td(a({-href=>$name.'/index.html'},$title))),"\n";
739 my $IND = $self->{-IND};
741 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
746 my $IND = $self->{-IND};
747 my $first = $self->{-firstimg}->{-base};
748 my $slideref = sprintf(".html/%s-slide.html",$first);
750 print $IND h2({-class=>"ititle"},"Images ",
751 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
752 '> slideshow')),"\n";
757 my $IND = $self->{-parent}->{-IND};
758 my $name = $self->{-base};
759 my $title = $self->{-info}->{'Comment'};
760 $title = $name unless ($title);
761 my $thumb = $self->{$sizes[0]}->{'url'};
762 my $info = $self->{-info};
763 my ($w, $h) = dim($info);
765 my $i=0+$self->{-parent}->{-numofimgs};
766 $self->{-parent}->{-numofimgs}++;
768 print $IND a({-name=>$name}),"\n",
769 start_table({-class=>'slide'}),start_Tr,start_td,"\n",
770 div({-class=>'slidetitle'},
771 "\n ",a({-href=>".html/$name-info.html",
772 -title=>'Image Info: '.$name,
775 div({-class=>'slideimage'},
776 "\n ",a({-href=>".html/$name-static.html",
782 -alt=>$title})),"\n"),"\n",
783 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
784 foreach my $sz(@sizes) {
785 my $src=$self->{$sz}->{'url'};
786 my $w=$self->{$sz}->{'dim'}->[0];
787 my $h=$self->{$sz}->{'dim'}->[1];
788 print $IND " ",a({-href=>$src,
791 -title=>"Reduced to ".$w."x".$h},
794 print $IND " ",a({-href=>$name,
796 -title=>'Original'},$w."x".$h),
798 end_td,end_Tr,end_table,"\n";
803 my $IND = $self->{-IND};
805 print $IND br({-clear=>'all'}),hr,"\n\n";
810 my $info = $self->{-info};
827 $msg.=start_table({-class=>'infotable'})."\n";
828 foreach my $k(@infokeys) {
829 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
831 $msg.=end_table."\n";