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 $self->{-relpath} = $relpath;
168 $self->{-toppath} = substr($fullpath,0,$pos);
169 #print "rel=$relpath, top=$self->{-toppath}, inc=$inc\n";
172 $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
174 $self->{-relpath} = '';
179 my $self=shift; # this is not a method but we cheat
180 my $fullpath=$self->{-fullpath};
181 my $inc=$self->{-inc}.$incdir.'/';
182 my $conffile=$inc.'rss.conf';
186 if (open($CONF,">".$conffile)) {
187 print $CONF "file: ",$rssfile,"\n";
190 print STDERR "could not open $conffile: $!\n";
193 if (open($CONF,$conffile)) {
197 my ($k,$v)=split(':', $ln);
208 return unless ($rssfile);
210 $rssobj->{'file'} = $self->{-toppath}.'/'.$rssfile;
211 $rssobj->{'rss'} = new XML::RSS (version=>'2.0');
212 if ( -f $rssobj->{'file'} ) {
213 $rssobj->{'rss'}->parsefile($rssobj->{'file'});
214 my $itemstodel = @{$rssobj->{'rss'}->{'items'}} - 15;
215 while ($itemstodel-- > 0) {
216 pop(@{$rssobj->{'rss'}->{'items'}})
218 $rssobj->{'rss'}->save($rssobj->{'file'});
221 for (my $pos=index($rssfile,'/');$pos>=0;
222 $pos=index($rssfile,'/',$pos+1)) {
226 $rssobj->{'rss'}->channel(
229 description=>'Gallery Feed',
230 #language=>$language,
232 #copyright=>$copyright,
234 #lastBuildDate=>$lastBuild,
236 #managingEditor=>$editor,
237 #webMaster=>$webMaster
239 $rssobj->{'rss'}->save($rssobj->{'file'});
241 $self->{-rss} = $rssobj->{'rss'};
246 my $fullpath .= $self->{-fullpath};
247 print "iterate in dir $fullpath\n" if ($debug);
253 unless (opendir($D,$fullpath)) {
254 warn "cannot opendir $fullpath: $!";
257 while (my $de = readdir($D)) {
258 next if ($de =~ /^\./);
259 my $child = $self->new($de);
260 my @stat = stat($child->{-fullpath});
261 $youngest = $stat[9] if ($youngest < $stat[9]);
263 push(@rdirlist,$child);
264 } elsif ($child->isimg) {
265 push(@rimglist,$child);
269 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
270 undef @rdirlist; # inplace sorting would be handy here
271 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
272 undef @rimglist; # optimize away unsorted versions
273 $self->{-firstimg} = $imglist[0];
275 print "Dir: $self->{-fullpath}\n" if ($debug);
277 # 1. first of all, fill title for this directory and create hidden subdirs
281 # 2. recurse into subdirectories to get their titles filled
282 # before we start writing out subalbum list
284 foreach my $dir(@dirlist) {
288 # 3. iterate through images to build cross-links,
291 foreach my $img(@imglist) {
292 # list-linking must be done before generating
293 # aux html because aux pages rely on prev/next refs
295 $previmg->{-nextimg} = $img;
296 $img->{-previmg} = $previmg;
301 # 4. create scaled versions and aux html pages
303 foreach my $img(@imglist) {
304 # scaled versions must be generated before aux html
305 # and main image index because they both rely on
306 # refs to scaled images and they may be just original
307 # images, this is not known before we try scaling.
309 # finally, make aux html pages
313 # no need to go beyond this point if the directory timestamp did not
314 # change since we built index.html file last time.
316 my @istat = stat($self->{-fullpath}.'/index.html');
317 return unless ($youngest > $istat[9]);
319 # 5. start building index.html for the directory
323 # 6. iterate through subdirectories to build subalbums list
327 foreach my $dir(@dirlist) {
333 # 7. iterate through images to build thumb list
337 foreach my $img(@imglist) {
338 print "Img: $img->{-fullpath}\n" if ($debug);
344 # 8. comlplete building index.html for the directory
351 return ( -d $self->{-fullpath} );
356 my $fullpath = $self->{-fullpath};
357 return 0 unless ( -f $fullpath );
358 my $info = image_info($fullpath);
359 if (my $error = $info->{error}) {
360 if (($error !~ "Unrecognized file format") &&
361 ($error !~ "Can't read head")) {
362 warn "File \"$fullpath\": $error\n";
367 tryapp12($info) unless ($info->{'ExifVersion'});
370 $self->{-info} = $info;
375 my $info = shift; # this is not a method
377 # dirty hack to take care of Image::Info parser strangeness
378 foreach my $k(keys %$info) {
379 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
381 return unless ($app12); # bad luck
383 foreach my $ln(split /[\r\n]+/,$app12) {
384 $ln =~ s/[[:^print:]\000]/ /g;
385 unless ($seenfirstline) {
390 my ($k,$v)=split /=/,$ln,2;
391 if ($k eq 'TimeDate') {
392 $info->{'DateTime'} =
393 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
395 } elsif ($k eq 'Shutter') {
396 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
397 } elsif ($k eq 'Flash') {
398 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
399 } elsif ($k eq 'Type') {
400 $info->{'Model'} = $v;
401 } elsif ($k eq 'Version') {
402 $info->{'Software'} = $v;
403 } elsif ($k eq 'Fnumber') {
404 $info->{'FNumber'} = $v;
411 my $fullpath = $self->{-fullpath};
412 for my $subdir(@sizes, 'html') {
413 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
414 mkdir($tdir,0755) unless ( -d $tdir );
421 my $fullpath = $self->{-fullpath};
424 if (open($T,'<'.$fullpath.'/.title')) {
426 $title =~ s/[\r\n]*$//;
429 if ($asktitle || (!$title && !$noasktitle)) {
430 my $prompt = $self->{-relpath};
431 $prompt = '/' unless ($prompt);
432 my $OUT = $term->OUT || \*STDOUT;
433 print $OUT "Enter title for $fullpath\n";
434 $title = $term->readline($prompt.' >',$title);
435 $term->addhistory($title) if ($title);
436 if (open($T,'>'.$fullpath.'/.title')) {
437 print $T $title,"\n";
442 $title=$self->{-relpath};
444 $self->{-title}=$title;
445 print "title in $fullpath is $title\n" if ($debug);
450 my $fn = $self->{-fullpath};
451 my $name = $self->{-base};
452 my $dn = $self->{-parent}->{-fullpath};
453 my ($w, $h) = dim($self->{-info});
454 my $max = ($w > $h)?$w:$h;
456 foreach my $size(@sizes) {
457 my $nref = '.'.$size.'/'.$name;
458 my $nfn = $dn.'/'.$nref;
459 my $factor=$size/$max;
461 $self->{$size}->{'url'} = $name; # unscaled version
462 $self->{$size}->{'dim'} = [$w, $h];
464 $self->{$size}->{'url'} = $nref;
465 $self->{$size}->{'dim'} = [int($w*$factor+.5),
467 if (isnewer($fn,$nfn)) {
468 doscaling($fn,$nfn,$factor,$w,$h);
475 my ($fn1,$fn2) = @_; # this is not a method
476 my @stat1=stat($fn1);
477 my @stat2=stat($fn2);
478 return (!@stat2 || ($stat1[9] > $stat2[9]));
479 # true if $fn2 is absent or is older than $fn1
483 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
487 my $im = new Image::Magick;
488 print "doscaling $src -> $dest by $factor\n" if ($debug);
489 if ($err = $im->Read($src)) {
490 warn "ImageMagick: read \"$src\": $err";
492 $im->Scale(width=>$w*$factor,height=>$h*$factor);
493 $err=$im->Write($dest);
494 warn "ImageMagick: write \"$dest\": $err" if ($err);
498 if ($err) { # fallback to command-line tools
499 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
505 my $name = $self->{-base};
506 my $dn = $self->{-parent}->{-fullpath};
507 my $pref = $self->{-previmg}->{-base};
508 my $nref = $self->{-nextimg}->{-base};
509 my $inc = $self->{-inc}.$incdir.'/';
510 my $title = $self->{-info}->{'Comment'};
511 $title = $name unless ($title);
513 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
516 for my $refresh('static', 'slide') {
517 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
518 if (isnewer($self->{-fullpath},$fn)) {
519 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
523 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
525 $fwdref = '../index.html';
528 $bakref = sprintf("%s-%s.html",$pref,$refresh);
530 $bakref = '../index.html';
534 if ($refresh eq 'slide') {
535 $toggleref=sprintf("%s-static.html",$name);
536 $toggletext = 'Stop!';
538 $toggleref=sprintf("%s-slide.html",$name);
539 $toggletext = 'Play->';
542 unless (open($F,'>'.$fn)) {
543 warn "cannot open \"$fn\": $!";
546 binmode($F, ":utf8");
547 if ($refresh eq 'slide') {
552 -head=>meta({-http_equiv=>'Refresh',
553 -content=>"3; url=$fwdref"}),
554 -style=>{-src=>$inc."gallery.css"},
556 comment("Created by ".$version),"\n";
559 print $F start_html(-title=>$title,
562 -style=>{-src=>$inc."gallery.css"},
564 comment("Created by ".$version),"\n";
566 print $F start_table({-class=>'navi'}),start_Tr,"\n",
567 td(a({-href=>"../index.html"},"Index")),"\n",
568 td(a({-href=>$bakref},"<<Prev")),"\n",
569 td(a({-href=>$toggleref},$toggletext)),"\n",
570 td(a({-href=>$fwdref},"Next>>")),"\n",
571 td({-class=>'title'},$title),"\n",
574 center(table({-class=>'picframe'},
575 Tr(td(img({-src=>$imgsrc,
576 -class=>'standalone',
577 -alt=>$title}))))),"\n",
584 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
585 if (isnewer($self->{-fullpath},$fn)) {
587 unless (open($F,'>'.$fn)) {
588 warn "cannot open \"$fn\": $!";
591 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
592 print $F start_html(-title=>$title,
594 -style=>{-src=>$inc."gallery.css"},
596 {-src=>$inc."mootools.js"},
597 {-src=>$inc."urlparser.js"},
598 {-src=>$inc."infopage.js"},
600 comment("Created by ".$version),"\n",
603 table({-class=>'ipage'},
604 Tr(td(img({-src=>$imgsrc,
607 td($self->infotable))),
608 a({-href=>'../index.html',-class=>'conceal'},
618 my $fn = $self->{-fullpath}.'/index.html';
619 my $block = $self->{-fullpath}.'/.noindex';
620 $fn = '/dev/null' if ( -f $block );
622 unless (open($IND,'>'.$fn)) {
623 warn "cannot open $fn: $!";
626 binmode($IND, ":utf8");
627 $self->{-IND} = $IND;
629 my $inc = $self->{-inc}.$incdir.'/';
630 my $title = $self->{-title};
633 $rsslink=Link({-rel=>'alternate',
634 -type=>'application/rss+xml',
636 -href=>$self->{-rss}});
638 print $IND start_html(-title => $title,
642 {-src=>$inc."gallery.css"},
643 {-src=>$inc."custom.css"},
646 {-src=>$inc."mootools.js"},
647 {-src=>$inc."overlay.js"},
648 {-src=>$inc."urlparser.js"},
649 {-src=>$inc."multibox.js"},
650 {-src=>$inc."showwin.js"},
651 {-src=>$inc."controls.js"},
652 {-src=>$inc."show.js"},
653 {-src=>$inc."gallery.js"},
655 comment("Created by ".$version),"\n",
656 start_div({-class => 'indexContainer',
657 -id => 'indexContainer'}),
660 if (open($EVL,$inc.'header.pl')) {
667 -version => $version,
668 -depth => $self->{-depth},
670 -breadcrumbs => "breadcrumbs unimplemented",
672 print $IND eval $prm,"\n";
674 print $IND a({-href=>"../index.html"},"UP"),"\n",
675 h1({-class=>'title'},$title),"\n",
681 my $IND = $self->{-IND};
685 if (open($EVL,$self->{-inc}.$incdir.'/footer.pl')) {
692 -version => $version,
693 -depth => $self->{-depth},
694 -title => $self->{-title},
695 -breadcrumbs => "breadcrumbs unimplemented",
697 print $IND eval $prm,"\n";
699 print $IND end_html,"\n";
701 close($IND) if ($IND);
704 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
708 my $rsslink=$rssobj->{'rss'}->channel('link').
709 $self->{-relpath}."/index.html";
710 $rssobj->{'rss'}->add_item(
711 title => $self->{-title},
713 description => $rsstitle,
720 my $IND = $self->{-IND};
722 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
727 my $IND = $self->{-parent}->{-IND};
728 my $name = $self->{-base};
729 my $title = $self->{-title};
731 $self->{-parent}->{-numofsubs}++;
732 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
733 td(a({-href=>$name.'/index.html'},$title))),"\n";
738 my $IND = $self->{-IND};
740 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
745 my $IND = $self->{-IND};
746 my $first = $self->{-firstimg}->{-base};
747 my $slideref = sprintf(".html/%s-slide.html",$first);
749 print $IND h2({-class=>"ititle"},"Images ",
750 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
751 '> slideshow')),"\n";
756 my $IND = $self->{-parent}->{-IND};
757 my $name = $self->{-base};
758 my $title = $self->{-info}->{'Comment'};
759 $title = $name unless ($title);
760 my $thumb = $self->{$sizes[0]}->{'url'};
761 my $info = $self->{-info};
762 my ($w, $h) = dim($info);
764 my $i=0+$self->{-parent}->{-numofimgs};
765 $self->{-parent}->{-numofimgs}++;
767 print $IND a({-name=>$name}),"\n",
768 start_table({-class=>'slide'}),start_Tr,start_td,"\n",
769 div({-class=>'slidetitle'},
770 "\n ",a({-href=>".html/$name-info.html",
771 -title=>'Image Info: '.$name,
774 div({-class=>'slideimage'},
775 "\n ",a({-href=>".html/$name-static.html",
781 -alt=>$title})),"\n"),"\n",
782 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
783 foreach my $sz(@sizes) {
784 my $src=$self->{$sz}->{'url'};
785 my $w=$self->{$sz}->{'dim'}->[0];
786 my $h=$self->{$sz}->{'dim'}->[1];
787 print $IND " ",a({-href=>$src,
790 -title=>"Reduced to ".$w."x".$h},
793 print $IND " ",a({-href=>$name,
795 -title=>'Original'},$w."x".$h),
797 end_td,end_Tr,end_table,"\n";
802 my $IND = $self->{-IND};
804 print $IND br({-clear=>'all'}),hr,"\n\n";
809 my $info = $self->{-info};
826 $msg.=start_table({-class=>'infotable'})."\n";
827 foreach my $k(@infokeys) {
828 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
830 $msg.=end_table."\n";