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-2013 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/;
40 binmode(STDOUT, ":utf8");
42 my $haveimagick = eval { require Image::Magick; };
43 { package Image::Magick; } # to make perl compiler happy
45 my $havegeoloc = eval { require Image::ExifTool::Location; };
46 { package Image::ExifTool::Location; } # to make perl compiler happy
48 my @sizes = (160, 640, 1600);
49 my $incdir = ".gallery2";
51 ######################################################################
63 'asktitle'=>\$asktitle,
64 'noasktitle'=>\$noasktitle,
69 my $term = new Term::ReadLine "Edit Title";
70 binmode($term->IN, ':utf8');
72 FsObj->new(getcwd)->iterate;
76 print STDERR <<__END__;
78 --help: print help message and exit
79 --incpath: do not try to find .gallery2 directory upstream, use
80 specified path (absolute or relavive). Use with causion.
81 --debug: print a lot of debugging info to stdout as you run
82 --asktitle: ask to edit album titles even if there are ".title" files
83 --noasktitle: don't ask to enter album titles even where ".title"
84 files are absent. Use partial directory names as titles.
100 -root=>$parent->{-root},
101 -toppath=>$parent->{-toppath},
102 -depth=>$parent->{-depth}+1,
104 -fullpath=>$parent->{-fullpath}.'/'.$name,
105 -relpath=>$parent->{-relpath}.$name.'/',
106 -inc=>'../'.$parent->{-inc},
115 # fill in -inc, -relpath
116 initpaths($self); # we are not blessed yet, so cheat.
120 print "new $class:\n";
121 foreach my $k(keys %$self) {
122 print "\t$k\t=\t$self->{$k}\n";
129 my $self=shift; # this is not a method but we cheat
130 my $depth=20; # arbitrary max depth
131 my $fullpath=$self->{-fullpath};
137 $inc .= '/' unless ($inc =~ m%/$%);
140 while ( ! -d $fullpath."/".$inc."/".$incdir ) {
142 last unless ($depth-- > 0);
146 $self->{-inc} = $inc;
149 for ($pos=index($inc,'/');$pos>=0;
150 $pos=index($inc,'/',$pos+1)) {
153 $self->{-depth} = $dp;
154 for ($pos=length($fullpath);$dp>0 && $pos>0;
155 $pos=rindex($fullpath,'/',$pos-1)) {
158 my $relpath = substr($fullpath,$pos);
160 $relpath .= '/' if ($relpath);
161 $self->{-relpath} = $relpath;
162 $self->{-toppath} = substr($fullpath,0,$pos);
163 #print "rel=$relpath, top=$self->{-toppath}, inc=$inc\n";
165 $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
166 $self->{-relpath} = '';
173 my $fullpath .= $self->{-fullpath};
174 print "iterate in dir $fullpath\n" if ($debug);
180 unless (opendir($D,$fullpath)) {
181 warn "cannot opendir $fullpath: $!";
184 while (my $de = readdir($D)) {
185 next if ($de =~ /^\./);
186 my $child = $self->new($de);
187 my @stat = stat($child->{-fullpath});
188 $youngest = $stat[9] if ($youngest < $stat[9]);
190 push(@rdirlist,$child);
191 } elsif ($child->isimg) {
192 push(@rimglist,$child);
196 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
197 undef @rdirlist; # inplace sorting would be handy here
198 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
199 undef @rimglist; # optimize away unsorted versions
200 $self->{-firstimg} = $imglist[0];
202 print "Dir: $self->{-fullpath}\n" if ($debug);
204 # 1. first of all, fill title for this directory and create hidden subdirs
208 # 2. recurse into subdirectories to get their titles filled
209 # before we start writing out subalbum list
211 foreach my $dir(@dirlist) {
215 # 3. iterate through images to build cross-links,
218 foreach my $img(@imglist) {
219 # list-linking must be done before generating
220 # aux html because aux pages rely on prev/next refs
222 $previmg->{-nextimg} = $img;
223 $img->{-previmg} = $previmg;
228 # 4. create scaled versions and aux html pages
230 foreach my $img(@imglist) {
231 # scaled versions must be generated before aux html
232 # and main image index because they both rely on
233 # refs to scaled images and they may be just original
234 # images, this is not known before we try scaling.
236 # finally, make aux html pages
240 # no need to go beyond this point if the directory timestamp did not
241 # change since we built index.html file last time.
243 my @istat = stat($self->{-fullpath}.'/index.html');
244 return unless ($youngest > $istat[9]);
246 # 5. start building index.html for the directory
250 # 6. iterate through subdirectories to build subalbums list
254 foreach my $dir(@dirlist) {
260 # 7. iterate through images to build thumb list
264 foreach my $img(@imglist) {
265 print "Img: $img->{-fullpath}\n" if ($debug);
271 # 8. comlplete building index.html for the directory
278 return ( -d $self->{-fullpath} );
283 my $fullpath = $self->{-fullpath};
284 return 0 unless ( -f $fullpath );
287 my $exif = new Image::ExifTool;
288 $exif->ExtractInfo($fullpath);
289 my ($la,$lo) = $exif->GetLocation();
291 $self->{-geoloc} = [$la,$lo];
295 my $info = image_info($fullpath);
296 if (my $error = $info->{error}) {
297 if (($error !~ "Unrecognized file format") &&
298 ($error !~ "Can't read head")) {
299 warn "File \"$fullpath\": $error\n";
304 tryapp12($info) unless ($info->{'ExifVersion'});
307 $self->{-info} = $info;
312 my $info = shift; # this is not a method
314 # dirty hack to take care of Image::Info parser strangeness
315 foreach my $k(keys %$info) {
316 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
318 return unless ($app12); # bad luck
320 foreach my $ln(split /[\r\n]+/,$app12) {
321 $ln =~ s/[[:^print:]\000]/ /g;
322 unless ($seenfirstline) {
327 my ($k,$v)=split /=/,$ln,2;
328 if ($k eq 'TimeDate') {
329 $info->{'DateTime'} =
330 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
332 } elsif ($k eq 'Shutter') {
333 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
334 } elsif ($k eq 'Flash') {
335 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
336 } elsif ($k eq 'Type') {
337 $info->{'Model'} = $v;
338 } elsif ($k eq 'Version') {
339 $info->{'Software'} = $v;
340 } elsif ($k eq 'Fnumber') {
341 $info->{'FNumber'} = $v;
348 my $fullpath = $self->{-fullpath};
349 for my $subdir(@sizes, 'html') {
350 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
351 mkdir($tdir,0755) unless ( -d $tdir );
358 my $fullpath = $self->{-fullpath};
363 if (open($T,'<:encoding(utf8)', $fullpath.'/.title')) {
365 $title =~ s/[\r\n]*$//;
368 if ($asktitle || (!$title && !$noasktitle)) {
369 my $prompt = $self->{-relpath};
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,'>:encoding(utf8)', $fullpath.'/.title')) {
376 print $T $title,"\n";
381 $title=$self->{-relpath};
383 $self->{-title}=$title;
384 if (open($TI,'<:encoding(utf8)', $fullpath.'/.titleimage')) {
386 $titleimage =~ s/[\r\n]*$//;
388 #print STDERR "found title image \"",$titleimage,"\"\n";
389 $self->{-titleimage}=$titleimage;
391 print "title in $fullpath is $title\n" if ($debug);
396 my $fn = $self->{-fullpath};
397 my $name = $self->{-base};
398 my $dn = $self->{-parent}->{-fullpath};
399 my ($w, $h) = dim($self->{-info});
400 my $max = ($w > $h)?$w:$h;
402 foreach my $size(@sizes) {
403 my $nref = '.'.$size.'/'.$name;
404 my $nfn = $dn.'/'.$nref;
405 my $factor=$size/$max;
407 $self->{$size}->{'url'} = $name; # unscaled version
408 $self->{$size}->{'dim'} = [$w, $h];
410 $self->{$size}->{'url'} = $nref;
411 $self->{$size}->{'dim'} = [int($w*$factor+.5),
413 if (isnewer($fn,$nfn)) {
414 doscaling($fn,$nfn,$factor,$w,$h);
421 my ($fn1,$fn2) = @_; # this is not a method
422 my @stat1=stat($fn1);
423 my @stat2=stat($fn2);
424 return (!@stat2 || ($stat1[9] > $stat2[9]));
425 # true if $fn2 is absent or is older than $fn1
429 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
433 my $im = new Image::Magick;
434 print "doscaling $src -> $dest by $factor\n" if ($debug);
435 if ($err = $im->Read($src)) {
436 warn "ImageMagick: read \"$src\": $err";
438 $im->Scale(width=>$w*$factor,height=>$h*$factor);
439 $err=$im->Write($dest);
440 warn "ImageMagick: write \"$dest\": $err" if ($err);
444 if ($err) { # fallback to command-line tools
445 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
451 my $name = $self->{-base};
452 my $dn = $self->{-parent}->{-fullpath};
453 my $pref = $self->{-previmg}->{-base};
454 my $nref = $self->{-nextimg}->{-base};
455 my $inc = $self->{-inc}.$incdir.'/';
456 my $title = $self->{-info}->{'Comment'};
457 $title = $name unless ($title);
459 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
462 for my $refresh('static', 'slide') {
463 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
464 if (isnewer($self->{-fullpath},$fn)) {
465 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
469 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
471 $fwdref = '../index.html';
474 $bakref = sprintf("%s-%s.html",$pref,$refresh);
476 $bakref = '../index.html';
480 if ($refresh eq 'slide') {
481 $toggleref=sprintf("%s-static.html",$name);
482 $toggletext = 'Stop!';
484 $toggleref=sprintf("%s-slide.html",$name);
485 $toggletext = 'Play->';
488 unless (open($F,'>:encoding(utf8)', $fn)) {
489 warn "cannot open \"$fn\": $!";
492 if ($refresh eq 'slide') {
497 -head=>meta({-http_equiv=>'Refresh',
498 -content=>"3; url=$fwdref"}),
499 -style=>{-src=>$inc."gallery.css"},
501 comment("Created by ".$version),"\n";
504 print $F start_html(-title=>$title,
507 -style=>{-src=>$inc."gallery.css"},
509 comment("Created by ".$version),"\n";
511 print $F start_table({-class=>'navi'}),start_Tr,"\n",
512 td(a({-href=>"../index.html"},"Index")),"\n",
513 td(a({-href=>$bakref},"<<Prev")),"\n",
514 td(a({-href=>$toggleref},$toggletext)),"\n",
515 td(a({-href=>$fwdref},"Next>>")),"\n",
516 td({-class=>'title'},$title),"\n",
519 center(table({-class=>'picframe'},
520 Tr(td(img({-src=>$imgsrc,
521 -class=>'standalone',
522 -alt=>$title}))))),"\n",
529 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
530 if (isnewer($self->{-fullpath},$fn)) {
532 unless (open($F,'>:encoding(utf8)', $fn)) {
533 warn "cannot open \"$fn\": $!";
536 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
537 print $F start_html(-title=>$title,
539 -style=>{-src=>$inc."gallery.css"},
541 {-src=>$inc."mootools.js"},
542 {-src=>$inc."urlparser.js"},
543 {-src=>$inc."infopage.js"},
545 comment("Created by ".$version),"\n",
548 table({-class=>'ipage'},
549 Tr(td(img({-src=>$imgsrc,
552 td($self->infotable))),
553 a({-href=>'../index.html',-class=>'conceal'},
563 my $fn = $self->{-fullpath}.'/index.html';
564 my $block = $self->{-fullpath}.'/.noindex';
565 $fn = '/dev/null' if ( -f $block );
567 unless (open($IND,'>:encoding(utf8)', $fn)) {
568 warn "cannot open $fn: $!";
571 $self->{-IND} = $IND;
573 my $inc = $self->{-inc}.$incdir.'/';
574 my $title = $self->{-title};
575 my $titleimage = $self->{-titleimage};
576 print $IND start_html(-title => $title,
579 {-src=>$inc."gallery.css"},
580 {-src=>$inc."custom.css"},
583 {-src=>$inc."mootools.js"},
584 {-src=>$inc."overlay.js"},
585 {-src=>$inc."urlparser.js"},
586 {-src=>$inc."multibox.js"},
587 {-src=>$inc."showwin.js"},
588 {-src=>$inc."controls.js"},
589 {-src=>$inc."show.js"},
590 {-src=>$inc."gallery.js"},
592 comment("Created by ".$version),"\n",
593 start_div({-class => 'indexContainer',
594 -id => 'indexContainer'}),
597 if (open($EVL, '<:encoding(utf8)', $self->{-toppath}.'/'.$incdir.'/header.pl')) {
604 -version => $version,
605 -depth => $self->{-depth},
607 -titleimage => $titleimage,
608 -path => $self->{-fullpath},
609 -breadcrumbs => "breadcrumbs unimplemented",
611 print $IND eval $prm,"\n";
613 print STDERR "could not open ",
614 $self->{-toppath}.'/'.$incdir.'/header.pl',
615 " ($!), reverting to default header";
616 print $IND a({-href=>"../index.html"},"UP"),"\n";
618 print $IND img({-src=>$titleimage,
619 -class=>'titleimage',
620 -alt=>'Title Image'}),"\n";
622 print $IND h1({-class=>'title'},$title),
623 br({-clear=>'all'}),"\n";
629 my $IND = $self->{-IND};
633 if (open($EVL, '<:encoding(utf8)', $self->{-toppath}.'/'.$incdir.'/footer.pl')) {
640 -version => $version,
641 -depth => $self->{-depth},
642 -title => $self->{-title},
643 -titleimage => $self->{-titleimage},
644 -breadcrumbs => "breadcrumbs unimplemented",
646 print $IND eval $prm,"\n";
648 print STDERR "could not open ",
649 $self->{-toppath}.'/'.$incdir.'/footer.pl',
650 " ($!), reverting to default empty footer";
652 print $IND end_html,"\n";
654 close($IND) if ($IND);
660 my $IND = $self->{-IND};
662 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
667 my $IND = $self->{-parent}->{-IND};
668 my $name = $self->{-base};
669 my $title = $self->{-title};
671 $self->{-parent}->{-numofsubs}++;
672 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
673 td(a({-href=>$name.'/index.html'},$title))),"\n";
678 my $IND = $self->{-IND};
680 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
685 my $IND = $self->{-IND};
686 my $first = $self->{-firstimg}->{-base};
687 my $slideref = sprintf(".html/%s-slide.html",$first);
689 print $IND h2({-class=>"ititle"},"Images ",
690 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
691 '> slideshow')),"\n";
696 my $IND = $self->{-parent}->{-IND};
697 my $name = $self->{-base};
698 my $title = $self->{-info}->{'Comment'};
699 $title = $name unless ($title);
700 my $thumb = $self->{$sizes[0]}->{'url'};
701 my $info = $self->{-info};
702 my ($w, $h) = dim($info);
704 my $i=0+$self->{-parent}->{-numofimgs};
705 $self->{-parent}->{-numofimgs}++;
707 print $IND a({-name=>$name}),"\n",
708 start_table({-class=>'slide'}),start_Tr,start_td,"\n";
709 print $IND div({-class=>'slidetitle'},
710 "\n ",a({-href=>".html/$name-info.html",
711 -title=>'Image Info: '.$name,
714 start_div({-class=>'slideimage'});
715 if ($self->{-geoloc}) {
716 my ($la,$lo) = @{$self->{-geoloc}};
717 print $IND a({-href=>"http://maps.google.com/".
718 "?q=$la,$lo&ll=$la,$lo",
721 div({-class=>'geoloc'},"")),"\n";
723 print $IND a({-href=>".html/$name-static.html",
729 -alt=>$title})),"\n",end_div,
730 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
731 foreach my $sz(@sizes) {
732 my $src=$self->{$sz}->{'url'};
733 my $w=$self->{$sz}->{'dim'}->[0];
734 my $h=$self->{$sz}->{'dim'}->[1];
735 print $IND " ",a({-href=>$src,
738 -title=>"Reduced to ".$w."x".$h},
741 print $IND " ",a({-href=>$name,
743 -title=>'Original'},$w."x".$h),
745 end_td,end_Tr,end_table,"\n";
750 my $IND = $self->{-IND};
752 print $IND br({-clear=>'all'}),hr,"\n\n";
757 my $info = $self->{-info};
774 $msg.=start_table({-class=>'infotable'})."\n";
775 foreach my $k(@infokeys) {
776 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
778 $msg.=end_table."\n";