5 # Recursively create image gallery index and slideshow wrappings.
6 # Makes use of (slightly modified) "lightbox" Javascript/CSS as published
7 # at http://www.huddletogether.com/projects/lightbox/
9 # Copyright (c) 2006 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 *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);
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;
79 $rssobj->save($rssfile);
84 print STDERR <<__END__;
86 --help: print help message and exit
87 --incpath: do not try to find .include diretory upstream, use
88 specified path (absolute or relavive). Use with causion.
89 --debug: print a lot of debugging info to stdout as you run
90 --asktitle: ask to edit album titles even if there are ".title" files
91 --noasktitle: don't ask to enter album titles even where ".title"
92 files are absent. Use partial directory names as titles.
93 --rssfile=...: build RSS feed for newly added "albums", give name of rss file
107 my $fullpath = $parent->{-fullpath}.'/'.$name;
110 -root=>$parent->{-root},
112 -fullpath=>$fullpath,
113 -inc=>'../'.$parent->{-inc},
114 -rss=>'../'.$parent->{-rss},
128 print "new $class:\n";
129 foreach my $k(keys %$self) {
130 print "\t$k\t=\t$self->{$k}\n";
137 my $fullpath=shift; # this is not a method
138 my $depth=20; # arbitrary max depth
141 return $incpath."/.include";
145 while ( ! -d $fullpath."/".$inc ) {
147 last unless ($depth-- > 0);
150 return $inc.'/'; # prefix with trailing slash
152 return 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
157 my $fullpath=shift; # this is not a method
158 my $depth=20; # arbitrary max depth
160 return "" unless $rssfile;
163 while ( ! -f $fullpath."/".$rss ) {
165 last unless ($depth-- > 0);
168 $rssobj = new XML::RSS (version=>2);
169 $rssobj->parsefile($rss);
172 print STDERR "There is no $rssfile in this or parent ".
173 "directories, you must create one with mkgalrss.pl\n";
180 my $fullpath .= $self->{-fullpath};
181 print "iterate in dir $fullpath\n" if ($debug);
187 unless (opendir($D,$fullpath)) {
188 warn "cannot opendir $fullpath: $!";
191 while (my $de = readdir($D)) {
192 next if ($de =~ /^\./);
193 my $child = $self->new($de);
194 my @stat = stat($child->{-fullpath});
195 $youngest = $stat[9] if ($youngest < $stat[9]);
197 push(@rdirlist,$child);
198 } elsif ($child->isimg) {
199 push(@rimglist,$child);
203 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
204 undef @rdirlist; # inplace sorting would be handy here
205 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
206 undef @rimglist; # optimize away unsorted versions
207 $self->{-firstimg} = $imglist[0];
209 print "Dir: $self->{-fullpath}\n" if ($debug);
211 # 1. first of all, fill title for this directory and create hidden subdirs
215 # 2. recurse into subdirectories to get their titles filled
216 # before we start writing out subalbum list
218 foreach my $dir(@dirlist) {
222 # 3. iterate through images to build cross-links,
225 foreach my $img(@imglist) {
226 # list-linking must be done before generating
227 # aux html because aux pages rely on prev/next refs
229 $previmg->{-nextimg} = $img;
230 $img->{-previmg} = $previmg;
235 # 4. create scaled versions and aux html pages
237 foreach my $img(@imglist) {
238 # scaled versions must be generated before aux html
239 # and main image index because they both rely on
240 # refs to scaled images and they may be just original
241 # images, this is not known before we try scaling.
243 # finally, make aux html pages
247 # no need to go beyond this point if the directory timestamp did not
248 # change since we built index.html file last time.
250 my @istat = stat($self->{-fullpath}.'/index.html');
251 return unless ($youngest > $istat[9]);
253 # 5. start building index.html for the directory
257 # 6. iterate through subdirectories to build subalbums list
261 foreach my $dir(@dirlist) {
267 # 7. iterate through images to build thumb list
271 foreach my $img(@imglist) {
272 print "Img: $img->{-fullpath}\n" if ($debug);
278 # 8. comlplete building index.html for the directory
285 return ( -d $self->{-fullpath} );
290 my $fullpath = $self->{-fullpath};
291 return 0 unless ( -f $fullpath );
292 my $info = image_info($fullpath);
293 if (my $error = $info->{error}) {
294 if (($error !~ "Unrecognized file format") &&
295 ($error !~ "Can't read head")) {
296 warn "File \"$fullpath\": $error\n";
301 tryapp12($info) unless ($info->{'ExifVersion'});
304 $self->{-info} = $info;
309 my $info = shift; # this is not a method
311 # dirty hack to take care of Image::Info parser strangeness
312 foreach my $k(keys %$info) {
313 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
315 return unless ($app12); # bad luck
317 foreach my $ln(split /[\r\n]+/,$app12) {
318 $ln =~ s/[[:^print:]\000]/ /g;
319 unless ($seenfirstline) {
324 my ($k,$v)=split /=/,$ln,2;
325 if ($k eq 'TimeDate') {
326 $info->{'DateTime'} =
327 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
329 } elsif ($k eq 'Shutter') {
330 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
331 } elsif ($k eq 'Flash') {
332 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
333 } elsif ($k eq 'Type') {
334 $info->{'Model'} = $v;
335 } elsif ($k eq 'Version') {
336 $info->{'Software'} = $v;
337 } elsif ($k eq 'Fnumber') {
338 $info->{'FNumber'} = $v;
345 my $fullpath = $self->{-fullpath};
346 for my $subdir(@sizes, 'html') {
347 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
348 mkdir($tdir,0755) unless ( -d $tdir );
355 my $fullpath = $self->{-fullpath};
358 if (open($T,'<'.$fullpath.'/.title')) {
360 $title =~ s/[\r\n]*$//;
363 if ($asktitle || (!$title && !$noasktitle)) {
364 my $prompt = $self->{-base};
365 $prompt = '/' unless ($prompt);
366 my $OUT = $term->OUT || \*STDOUT;
367 print $OUT "Enter title for $fullpath\n";
368 $title = $term->readline($prompt.' >',$title);
369 $term->addhistory($title) if ($title);
370 if (open($T,'>'.$fullpath.'/.title')) {
371 print $T $title,"\n";
376 $title=substr($fullpath,length($self->{-root}));
378 $self->{-title}=$title;
379 print "title in $fullpath is $title\n" if ($debug);
384 my $fn = $self->{-fullpath};
385 my $name = $self->{-base};
386 my $dn = $self->{-parent}->{-fullpath};
387 my ($w, $h) = dim($self->{-info});
388 my $max = ($w > $h)?$w:$h;
390 foreach my $size(@sizes) {
391 my $nref = '.'.$size.'/'.$name;
392 my $nfn = $dn.'/'.$nref;
393 my $factor=$size/$max;
395 $self->{$size} = $name; # unscaled version will do
397 $self->{$size} = $nref;
398 if (isnewer($fn,$nfn)) {
399 doscaling($fn,$nfn,$factor,$w,$h);
406 my ($fn1,$fn2) = @_; # this is not a method
407 my @stat1=stat($fn1);
408 my @stat2=stat($fn2);
409 return (!@stat2 || ($stat1[9] > $stat2[9]));
410 # true if $fn2 is absent or is older than $fn1
414 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
418 my $im = new Image::Magick;
419 print "doscaling $src -> $dest by $factor\n" if ($debug);
420 if ($err = $im->Read($src)) {
421 warn "ImageMagick: read \"$src\": $err";
423 $im->Scale(width=>$w*$factor,height=>$h*$factor);
424 $err=$im->Write($dest);
425 warn "ImageMagick: write \"$dest\": $err" if ($err);
429 if ($err) { # fallback to command-line tools
430 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
436 my $name = $self->{-base};
437 my $dn = $self->{-parent}->{-fullpath};
438 my $pref = $self->{-previmg}->{-base};
439 my $nref = $self->{-nextimg}->{-base};
440 my $inc = $self->{-inc};
441 my $title = $self->{-info}->{'Comment'};
442 $title = $name unless ($title);
444 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
447 for my $refresh('static', 'slide') {
448 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
449 if (isnewer($self->{-fullpath},$fn)) {
450 my $imgsrc = '../'.$self->{$sizes[1]};
454 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
456 $fwdref = '../index.html';
459 $bakref = sprintf("%s-%s.html",$pref,$refresh);
461 $bakref = '../index.html';
465 if ($refresh eq 'slide') {
466 $toggleref=sprintf("%s-static.html",$name);
467 $toggletext = 'Stop!';
469 $toggleref=sprintf("%s-slide.html",$name);
470 $toggletext = 'Play->';
473 unless (open($F,'>'.$fn)) {
474 warn "cannot open \"$fn\": $!";
477 binmode($F, ":utf8");
478 if ($refresh eq 'slide') {
483 -head=>meta({-http_equiv=>'Refresh',
484 -content=>"3; url=$fwdref"}),
485 -style=>{-src=>$inc."gallery.css"},
489 print $F start_html(-title=>$title,
492 -style=>{-src=>$inc."gallery.css"},
495 print $F start_center,"\n",
497 start_table({-class=>'navi'}),start_Tr,"\n",
498 td(a({-href=>"../index.html"},"Index")),"\n",
499 td(a({-href=>$bakref},"<<Prev")),"\n",
500 td(a({-href=>$toggleref},$toggletext)),"\n",
501 td(a({-href=>$fwdref},"Next>>")),"\n",
504 table({-class=>'picframe'},
505 Tr(td(img({-src=>$imgsrc})))),"\n",
513 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
514 if (isnewer($self->{-fullpath},$fn)) {
516 unless (open($F,'>'.$fn)) {
517 warn "cannot open \"$fn\": $!";
520 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
521 print $F start_html(-title=>$title,
523 -style=>{-src=>$inc."gallery.css"},),"\n",
526 table({-class=>'ipage'},
527 Tr(td(img({-src=>$imgsrc})),
528 td($self->infotable))),
529 a({-href=>'../index.html'},'Index'),"\n",
538 my $fn = $self->{-fullpath}.'/index.html';
539 my $block = $self->{-fullpath}.'/.noindex';
540 $fn = '/dev/null' if ( -f $block );
542 unless (open($IND,'>'.$fn)) {
543 warn "cannot open $fn: $!";
546 binmode($IND, ":utf8");
547 $self->{-IND} = $IND;
549 my $inc = $self->{-inc};
550 my $title = $self->{-title};
553 $rsslink=Link({-rel=>'alternate',
554 -type=>'application/rss+xml',
556 -href=>$self->{-rss}});
558 print $IND start_html(-title => $title,
561 -style=>{-src=>[$inc."gallery.css",
562 $inc."lightbox.css"]},
563 -script=>[{-code=>"var incPrefix='$inc';"},
564 {-src=>$inc."gallery.js"},
565 {-src=>$inc."lightbox.js"}]),
566 a({-href=>"../index.html"},"UP"),"\n",
574 my $IND = $self->{-IND};
576 print $IND end_center,end_html,"\n";
578 close($IND) if ($IND);
580 print STDERR "title=",$self->{-title},
581 ", numofsubs=",$self->{-numofsubs},
582 ", numofimgs=",$self->{-numofimgs},"\n";
584 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
588 my $rsslink=$rssobj->channel('link')."index.html";
590 title => $self->{-title},
592 description => $rsstitle,
599 my $IND = $self->{-IND};
601 print $IND h2("Albums"),"\n",start_table,"\n";
606 my $IND = $self->{-parent}->{-IND};
607 my $name = $self->{-base};
608 my $title = $self->{-title};
610 $self->{-parent}->{-numofsubs}++;
611 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
612 td(a({-href=>$name.'/index.html'},$title))),"\n";
617 my $IND = $self->{-IND};
619 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
624 my $IND = $self->{-IND};
625 my $first = $self->{-firstimg}->{-base};
626 my $slideref = sprintf(".html/%s-slide.html",$first);
628 print $IND h2("Images"),"\n",
629 a({-href=>$slideref},'Slideshow'),
635 my $IND = $self->{-parent}->{-IND};
636 my $name = $self->{-base};
637 my $title = $self->{-info}->{'Comment'};
638 $title = $name unless ($title);
639 my $thumb = $self->{$sizes[0]};
640 my $medium = $self->{$sizes[1]};
641 my $info = $self->{-info};
642 my ($w, $h) = dim($info);
644 $self->{-parent}->{-numofimgs}++;
645 print $IND start_div({-class=>'ibox',-id=>$name,
646 -OnClick=>"HideIbox('$name');"}),"\n",
647 start_div({-class=>'iboxtitle'}),
648 span({-style=>'float: left;'},b("Info for $name")),
649 span({-style=>'float: right;'},
650 a({-href=>"#",-OnClick=>"HideIbox('$name');"},"Close")),
651 br({-clear=>'all'}),"\n",
656 print $IND table({-class=>'slide'},Tr(td(
657 a({-href=>".html/$name-info.html",-title=>'Image Info',
658 -onClick=>"return showIbox('$name');"},$title),
660 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
661 img({-src=>$thumb})),
663 a({-href=>$name,-title=>'Original Image'},"($w x $h)"),
669 my $IND = $self->{-IND};
671 print $IND br({-clear=>'all'}),hr,"\n\n";
676 my $info = $self->{-info};
693 $msg.=start_table({-class=>'infotable'})."\n";
694 foreach my $k(@infokeys) {
695 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
697 $msg.=end_table."\n";