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/;
39 #use encoding 'utf-8';
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";
71 FsObj->new(getcwd)->iterate;
75 print STDERR <<__END__;
77 --help: print help message and exit
78 --incpath: do not try to find .gallery2 directory upstream, use
79 specified path (absolute or relavive). Use with causion.
80 --debug: print a lot of debugging info to stdout as you run
81 --asktitle: ask to edit album titles even if there are ".title" files
82 --noasktitle: don't ask to enter album titles even where ".title"
83 files are absent. Use partial directory names as titles.
99 -root=>$parent->{-root},
100 -toppath=>$parent->{-toppath},
101 -depth=>$parent->{-depth}+1,
103 -fullpath=>$parent->{-fullpath}.'/'.$name,
104 -relpath=>$parent->{-relpath}.$name.'/',
105 -inc=>'../'.$parent->{-inc},
114 # fill in -inc, -relpath
115 initpaths($self); # we are not blessed yet, so cheat.
119 print "new $class:\n";
120 foreach my $k(keys %$self) {
121 print "\t$k\t=\t$self->{$k}\n";
128 my $self=shift; # this is not a method but we cheat
129 my $depth=20; # arbitrary max depth
130 my $fullpath=$self->{-fullpath};
136 $inc .= '/' unless ($inc =~ m%/$%);
139 while ( ! -d $fullpath."/".$inc."/".$incdir ) {
141 last unless ($depth-- > 0);
145 $self->{-inc} = $inc;
148 for ($pos=index($inc,'/');$pos>=0;
149 $pos=index($inc,'/',$pos+1)) {
152 $self->{-depth} = $dp;
153 for ($pos=length($fullpath);$dp>0 && $pos>0;
154 $pos=rindex($fullpath,'/',$pos-1)) {
157 my $relpath = substr($fullpath,$pos);
159 $relpath .= '/' if ($relpath);
160 $self->{-relpath} = $relpath;
161 $self->{-toppath} = substr($fullpath,0,$pos);
162 #print "rel=$relpath, top=$self->{-toppath}, inc=$inc\n";
164 $self->{-inc} = 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
165 $self->{-relpath} = '';
172 my $fullpath .= $self->{-fullpath};
173 print "iterate in dir $fullpath\n" if ($debug);
179 unless (opendir($D,$fullpath)) {
180 warn "cannot opendir $fullpath: $!";
183 while (my $de = readdir($D)) {
184 next if ($de =~ /^\./);
185 my $child = $self->new($de);
186 my @stat = stat($child->{-fullpath});
187 $youngest = $stat[9] if ($youngest < $stat[9]);
189 push(@rdirlist,$child);
190 } elsif ($child->isimg) {
191 push(@rimglist,$child);
195 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
196 undef @rdirlist; # inplace sorting would be handy here
197 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
198 undef @rimglist; # optimize away unsorted versions
199 $self->{-firstimg} = $imglist[0];
201 print "Dir: $self->{-fullpath}\n" if ($debug);
203 # 1. first of all, fill title for this directory and create hidden subdirs
207 # 2. recurse into subdirectories to get their titles filled
208 # before we start writing out subalbum list
210 foreach my $dir(@dirlist) {
214 # 3. iterate through images to build cross-links,
217 foreach my $img(@imglist) {
218 # list-linking must be done before generating
219 # aux html because aux pages rely on prev/next refs
221 $previmg->{-nextimg} = $img;
222 $img->{-previmg} = $previmg;
227 # 4. create scaled versions and aux html pages
229 foreach my $img(@imglist) {
230 # scaled versions must be generated before aux html
231 # and main image index because they both rely on
232 # refs to scaled images and they may be just original
233 # images, this is not known before we try scaling.
235 # finally, make aux html pages
239 # no need to go beyond this point if the directory timestamp did not
240 # change since we built index.html file last time.
242 my @istat = stat($self->{-fullpath}.'/index.html');
243 return unless ($youngest > $istat[9]);
245 # 5. start building index.html for the directory
249 # 6. iterate through subdirectories to build subalbums list
253 foreach my $dir(@dirlist) {
259 # 7. iterate through images to build thumb list
263 foreach my $img(@imglist) {
264 print "Img: $img->{-fullpath}\n" if ($debug);
270 # 8. comlplete building index.html for the directory
277 return ( -d $self->{-fullpath} );
282 my $fullpath = $self->{-fullpath};
283 return 0 unless ( -f $fullpath );
286 my $exif = new Image::ExifTool;
287 $exif->ExtractInfo($fullpath);
288 my ($la,$lo) = $exif->GetLocation();
290 $self->{-geoloc} = [$la,$lo];
294 my $info = image_info($fullpath);
295 if (my $error = $info->{error}) {
296 if (($error !~ "Unrecognized file format") &&
297 ($error !~ "Can't read head")) {
298 warn "File \"$fullpath\": $error\n";
303 tryapp12($info) unless ($info->{'ExifVersion'});
306 $self->{-info} = $info;
311 my $info = shift; # this is not a method
313 # dirty hack to take care of Image::Info parser strangeness
314 foreach my $k(keys %$info) {
315 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
317 return unless ($app12); # bad luck
319 foreach my $ln(split /[\r\n]+/,$app12) {
320 $ln =~ s/[[:^print:]\000]/ /g;
321 unless ($seenfirstline) {
326 my ($k,$v)=split /=/,$ln,2;
327 if ($k eq 'TimeDate') {
328 $info->{'DateTime'} =
329 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
331 } elsif ($k eq 'Shutter') {
332 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
333 } elsif ($k eq 'Flash') {
334 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
335 } elsif ($k eq 'Type') {
336 $info->{'Model'} = $v;
337 } elsif ($k eq 'Version') {
338 $info->{'Software'} = $v;
339 } elsif ($k eq 'Fnumber') {
340 $info->{'FNumber'} = $v;
347 my $fullpath = $self->{-fullpath};
348 for my $subdir(@sizes, 'html') {
349 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
350 mkdir($tdir,0755) unless ( -d $tdir );
357 my $fullpath = $self->{-fullpath};
360 if (open($T,'<'.$fullpath.'/.title')) {
361 binmode($T, ":utf8");
363 $title =~ s/[\r\n]*$//;
366 if ($asktitle || (!$title && !$noasktitle)) {
367 my $prompt = $self->{-relpath};
368 $prompt = '/' unless ($prompt);
369 my $OUT = $term->OUT || \*STDOUT;
370 print $OUT "Enter title for $fullpath\n";
371 $title = $term->readline($prompt.' >',$title);
372 $term->addhistory($title) if ($title);
373 if (open($T,'>'.$fullpath.'/.title')) {
374 print $T $title,"\n";
379 $title=$self->{-relpath};
381 $self->{-title}=$title;
382 print "title in $fullpath is $title\n" if ($debug);
387 my $fn = $self->{-fullpath};
388 my $name = $self->{-base};
389 my $dn = $self->{-parent}->{-fullpath};
390 my ($w, $h) = dim($self->{-info});
391 my $max = ($w > $h)?$w:$h;
393 foreach my $size(@sizes) {
394 my $nref = '.'.$size.'/'.$name;
395 my $nfn = $dn.'/'.$nref;
396 my $factor=$size/$max;
398 $self->{$size}->{'url'} = $name; # unscaled version
399 $self->{$size}->{'dim'} = [$w, $h];
401 $self->{$size}->{'url'} = $nref;
402 $self->{$size}->{'dim'} = [int($w*$factor+.5),
404 if (isnewer($fn,$nfn)) {
405 doscaling($fn,$nfn,$factor,$w,$h);
412 my ($fn1,$fn2) = @_; # this is not a method
413 my @stat1=stat($fn1);
414 my @stat2=stat($fn2);
415 return (!@stat2 || ($stat1[9] > $stat2[9]));
416 # true if $fn2 is absent or is older than $fn1
420 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
424 my $im = new Image::Magick;
425 print "doscaling $src -> $dest by $factor\n" if ($debug);
426 if ($err = $im->Read($src)) {
427 warn "ImageMagick: read \"$src\": $err";
429 $im->Scale(width=>$w*$factor,height=>$h*$factor);
430 $err=$im->Write($dest);
431 warn "ImageMagick: write \"$dest\": $err" if ($err);
435 if ($err) { # fallback to command-line tools
436 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
442 my $name = $self->{-base};
443 my $dn = $self->{-parent}->{-fullpath};
444 my $pref = $self->{-previmg}->{-base};
445 my $nref = $self->{-nextimg}->{-base};
446 my $inc = $self->{-inc}.$incdir.'/';
447 my $title = $self->{-info}->{'Comment'};
448 $title = $name unless ($title);
450 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
453 for my $refresh('static', 'slide') {
454 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
455 if (isnewer($self->{-fullpath},$fn)) {
456 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
460 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
462 $fwdref = '../index.html';
465 $bakref = sprintf("%s-%s.html",$pref,$refresh);
467 $bakref = '../index.html';
471 if ($refresh eq 'slide') {
472 $toggleref=sprintf("%s-static.html",$name);
473 $toggletext = 'Stop!';
475 $toggleref=sprintf("%s-slide.html",$name);
476 $toggletext = 'Play->';
479 unless (open($F,'>'.$fn)) {
480 warn "cannot open \"$fn\": $!";
483 binmode($F, ":utf8");
484 if ($refresh eq 'slide') {
489 -head=>meta({-http_equiv=>'Refresh',
490 -content=>"3; url=$fwdref"}),
491 -style=>{-src=>$inc."gallery.css"},
493 comment("Created by ".$version),"\n";
496 print $F start_html(-title=>$title,
499 -style=>{-src=>$inc."gallery.css"},
501 comment("Created by ".$version),"\n";
503 print $F start_table({-class=>'navi'}),start_Tr,"\n",
504 td(a({-href=>"../index.html"},"Index")),"\n",
505 td(a({-href=>$bakref},"<<Prev")),"\n",
506 td(a({-href=>$toggleref},$toggletext)),"\n",
507 td(a({-href=>$fwdref},"Next>>")),"\n",
508 td({-class=>'title'},$title),"\n",
511 center(table({-class=>'picframe'},
512 Tr(td(img({-src=>$imgsrc,
513 -class=>'standalone',
514 -alt=>$title}))))),"\n",
521 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
522 if (isnewer($self->{-fullpath},$fn)) {
524 unless (open($F,'>'.$fn)) {
525 warn "cannot open \"$fn\": $!";
528 binmode($F, ":utf8");
529 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
530 print $F start_html(-title=>$title,
532 -style=>{-src=>$inc."gallery.css"},
534 {-src=>$inc."mootools.js"},
535 {-src=>$inc."urlparser.js"},
536 {-src=>$inc."infopage.js"},
538 comment("Created by ".$version),"\n",
541 table({-class=>'ipage'},
542 Tr(td(img({-src=>$imgsrc,
545 td($self->infotable))),
546 a({-href=>'../index.html',-class=>'conceal'},
556 my $fn = $self->{-fullpath}.'/index.html';
557 my $block = $self->{-fullpath}.'/.noindex';
558 $fn = '/dev/null' if ( -f $block );
560 unless (open($IND,'>'.$fn)) {
561 warn "cannot open $fn: $!";
564 binmode($IND, ":utf8");
565 $self->{-IND} = $IND;
567 my $inc = $self->{-inc}.$incdir.'/';
568 my $title = $self->{-title};
569 print $IND start_html(-title => $title,
572 {-src=>$inc."gallery.css"},
573 {-src=>$inc."custom.css"},
576 {-src=>$inc."mootools.js"},
577 {-src=>$inc."overlay.js"},
578 {-src=>$inc."urlparser.js"},
579 {-src=>$inc."multibox.js"},
580 {-src=>$inc."showwin.js"},
581 {-src=>$inc."controls.js"},
582 {-src=>$inc."show.js"},
583 {-src=>$inc."gallery.js"},
585 comment("Created by ".$version),"\n",
586 start_div({-class => 'indexContainer',
587 -id => 'indexContainer'}),
590 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/header.pl')) {
597 -version => $version,
598 -depth => $self->{-depth},
600 -path => $self->{-fullpath},
601 -breadcrumbs => "breadcrumbs unimplemented",
603 print $IND eval $prm,"\n";
605 print STDERR "could not open ",
606 $self->{-toppath}.'/'.$incdir.'/header.pl',
607 " ($!), reverting to default header";
608 print $IND a({-href=>"../index.html"},"UP"),"\n",
609 h1({-class=>'title'},$title),"\n",
615 my $IND = $self->{-IND};
619 if (open($EVL,$self->{-toppath}.'/'.$incdir.'/footer.pl')) {
626 -version => $version,
627 -depth => $self->{-depth},
628 -title => $self->{-title},
629 -breadcrumbs => "breadcrumbs unimplemented",
631 print $IND eval $prm,"\n";
633 print STDERR "could not open ",
634 $self->{-toppath}.'/'.$incdir.'/footer.pl',
635 " ($!), reverting to default empty footer";
637 print $IND end_html,"\n";
639 close($IND) if ($IND);
645 my $IND = $self->{-IND};
647 print $IND h2({-class=>"atitle"},"Albums"),"\n",start_table,"\n";
652 my $IND = $self->{-parent}->{-IND};
653 my $name = $self->{-base};
654 my $title = $self->{-title};
656 $self->{-parent}->{-numofsubs}++;
657 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
658 td(a({-href=>$name.'/index.html'},$title))),"\n";
663 my $IND = $self->{-IND};
665 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
670 my $IND = $self->{-IND};
671 my $first = $self->{-firstimg}->{-base};
672 my $slideref = sprintf(".html/%s-slide.html",$first);
674 print $IND h2({-class=>"ititle"},"Images ",
675 a({-href=>$slideref,-class=>'showStart',-rel=>'i'.$first},
676 '> slideshow')),"\n";
681 my $IND = $self->{-parent}->{-IND};
682 my $name = $self->{-base};
683 my $title = $self->{-info}->{'Comment'};
684 $title = $name unless ($title);
685 my $thumb = $self->{$sizes[0]}->{'url'};
686 my $info = $self->{-info};
687 my ($w, $h) = dim($info);
689 my $i=0+$self->{-parent}->{-numofimgs};
690 $self->{-parent}->{-numofimgs}++;
692 print $IND a({-name=>$name}),"\n",
693 start_table({-class=>'slide'}),start_Tr,start_td,"\n";
694 print $IND div({-class=>'slidetitle'},
695 "\n ",a({-href=>".html/$name-info.html",
696 -title=>'Image Info: '.$name,
699 start_div({-class=>'slideimage'});
700 if ($self->{-geoloc}) {
701 my ($la,$lo) = @{$self->{-geoloc}};
702 print $IND a({-href=>"http://maps.google.com/".
703 "?q=$la,$lo&ll=$la,$lo",
706 div({-class=>'geoloc'},"")),"\n";
708 print $IND a({-href=>".html/$name-static.html",
714 -alt=>$title})),"\n",end_div,
715 start_div({-class=>'varimages',-id=>'i'.$name,-title=>$title}),"\n";
716 foreach my $sz(@sizes) {
717 my $src=$self->{$sz}->{'url'};
718 my $w=$self->{$sz}->{'dim'}->[0];
719 my $h=$self->{$sz}->{'dim'}->[1];
720 print $IND " ",a({-href=>$src,
723 -title=>"Reduced to ".$w."x".$h},
726 print $IND " ",a({-href=>$name,
728 -title=>'Original'},$w."x".$h),
730 end_td,end_Tr,end_table,"\n";
735 my $IND = $self->{-IND};
737 print $IND br({-clear=>'all'}),hr,"\n\n";
742 my $info = $self->{-info};
759 $msg.=start_table({-class=>'infotable'})."\n";
760 foreach my $k(@infokeys) {
761 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
763 $msg.=end_table."\n";