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);
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;
80 print STDERR <<__END__;
82 --help: print help message and exit
83 --incpath: do not try to find .include diretory upstream, use
84 specified path (absolute or relavive). Use with causion.
85 --debug: print a lot of debugging info to stdout as you run
86 --asktitle: ask to edit album titles even if there are ".title" files
87 --noasktitle: don't ask to enter album titles even where ".title"
88 files are absent. Use partial directory names as titles.
89 --rssfile=...: build RSS feed for newly added "albums", give name of rss file
103 my $fullpath = $parent->{-fullpath}.'/'.$name;
106 -root=>$parent->{-root},
108 -fullpath=>$fullpath,
109 -inc=>'../'.$parent->{-inc},
110 -rss=>'../'.$parent->{-rss},
124 print "new $class:\n";
125 foreach my $k(keys %$self) {
126 print "\t$k\t=\t$self->{$k}\n";
133 my $fullpath=shift; # this is not a method
134 my $depth=20; # arbitrary max depth
137 return $incpath."/.include";
141 while ( ! -d $fullpath."/".$inc ) {
143 last unless ($depth-- > 0);
146 return $inc.'/'; # prefix with trailing slash
148 return 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
153 my $fullpath=shift; # this is not a method
154 my $depth=20; # arbitrary max depth
156 return "" unless $rssfile;
159 while ( ! -f $fullpath."/".$rss ) {
161 last unless ($depth-- > 0);
164 $rssobj->{'file'} = $rss;
165 $rssobj->{'rss'} = new XML::RSS (version=>2);
166 $rssobj->{'rss'}->parsefile($rss);
167 my $itemstodel = @{$rssobj->{'rss'}->{'items'}} - 15;
168 while ($itemstodel-- > 0) {
169 pop(@{$rssobj->{'rss'}->{'items'}})
171 $rssobj->{'rss'}->save($rssobj->{'file'});
174 print STDERR "There is no $rssfile in this or parent ".
175 "directories, you must create one with mkgalrss.pl\n";
182 my $fullpath .= $self->{-fullpath};
183 print "iterate in dir $fullpath\n" if ($debug);
189 unless (opendir($D,$fullpath)) {
190 warn "cannot opendir $fullpath: $!";
193 while (my $de = readdir($D)) {
194 next if ($de =~ /^\./);
195 my $child = $self->new($de);
196 my @stat = stat($child->{-fullpath});
197 $youngest = $stat[9] if ($youngest < $stat[9]);
199 push(@rdirlist,$child);
200 } elsif ($child->isimg) {
201 push(@rimglist,$child);
205 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
206 undef @rdirlist; # inplace sorting would be handy here
207 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
208 undef @rimglist; # optimize away unsorted versions
209 $self->{-firstimg} = $imglist[0];
211 print "Dir: $self->{-fullpath}\n" if ($debug);
213 # 1. first of all, fill title for this directory and create hidden subdirs
217 # 2. recurse into subdirectories to get their titles filled
218 # before we start writing out subalbum list
220 foreach my $dir(@dirlist) {
224 # 3. iterate through images to build cross-links,
227 foreach my $img(@imglist) {
228 # list-linking must be done before generating
229 # aux html because aux pages rely on prev/next refs
231 $previmg->{-nextimg} = $img;
232 $img->{-previmg} = $previmg;
237 # 4. create scaled versions and aux html pages
239 foreach my $img(@imglist) {
240 # scaled versions must be generated before aux html
241 # and main image index because they both rely on
242 # refs to scaled images and they may be just original
243 # images, this is not known before we try scaling.
245 # finally, make aux html pages
249 # no need to go beyond this point if the directory timestamp did not
250 # change since we built index.html file last time.
252 my @istat = stat($self->{-fullpath}.'/index.html');
253 return unless ($youngest > $istat[9]);
255 # 5. start building index.html for the directory
259 # 6. iterate through subdirectories to build subalbums list
263 foreach my $dir(@dirlist) {
269 # 7. iterate through images to build thumb list
273 foreach my $img(@imglist) {
274 print "Img: $img->{-fullpath}\n" if ($debug);
280 # 8. comlplete building index.html for the directory
287 return ( -d $self->{-fullpath} );
292 my $fullpath = $self->{-fullpath};
293 return 0 unless ( -f $fullpath );
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')) {
362 $title =~ s/[\r\n]*$//;
365 if ($asktitle || (!$title && !$noasktitle)) {
366 my $prompt = $self->{-base};
367 $prompt = '/' unless ($prompt);
368 my $OUT = $term->OUT || \*STDOUT;
369 print $OUT "Enter title for $fullpath\n";
370 $title = $term->readline($prompt.' >',$title);
371 $term->addhistory($title) if ($title);
372 if (open($T,'>'.$fullpath.'/.title')) {
373 print $T $title,"\n";
378 $title=substr($fullpath,length($self->{-root}));
380 $self->{-title}=$title;
381 print "title in $fullpath is $title\n" if ($debug);
386 my $fn = $self->{-fullpath};
387 my $name = $self->{-base};
388 my $dn = $self->{-parent}->{-fullpath};
389 my ($w, $h) = dim($self->{-info});
390 my $max = ($w > $h)?$w:$h;
392 foreach my $size(@sizes) {
393 my $nref = '.'.$size.'/'.$name;
394 my $nfn = $dn.'/'.$nref;
395 my $factor=$size/$max;
397 $self->{$size}->{'url'} = $name; # unscaled version
398 $self->{$size}->{'dim'} = [$w, $h];
400 $self->{$size}->{'url'} = $nref;
401 $self->{$size}->{'dim'} = [int($w*$factor+.5),
403 if (isnewer($fn,$nfn)) {
404 doscaling($fn,$nfn,$factor,$w,$h);
411 my ($fn1,$fn2) = @_; # this is not a method
412 my @stat1=stat($fn1);
413 my @stat2=stat($fn2);
414 return (!@stat2 || ($stat1[9] > $stat2[9]));
415 # true if $fn2 is absent or is older than $fn1
419 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
423 my $im = new Image::Magick;
424 print "doscaling $src -> $dest by $factor\n" if ($debug);
425 if ($err = $im->Read($src)) {
426 warn "ImageMagick: read \"$src\": $err";
428 $im->Scale(width=>$w*$factor,height=>$h*$factor);
429 $err=$im->Write($dest);
430 warn "ImageMagick: write \"$dest\": $err" if ($err);
434 if ($err) { # fallback to command-line tools
435 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
441 my $name = $self->{-base};
442 my $dn = $self->{-parent}->{-fullpath};
443 my $pref = $self->{-previmg}->{-base};
444 my $nref = $self->{-nextimg}->{-base};
445 my $inc = $self->{-inc};
446 my $title = $self->{-info}->{'Comment'};
447 $title = $name unless ($title);
449 print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
452 for my $refresh('static', 'slide') {
453 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
454 if (isnewer($self->{-fullpath},$fn)) {
455 my $imgsrc = '../'.$self->{$sizes[1]}->{'url'};
459 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
461 $fwdref = '../index.html';
464 $bakref = sprintf("%s-%s.html",$pref,$refresh);
466 $bakref = '../index.html';
470 if ($refresh eq 'slide') {
471 $toggleref=sprintf("%s-static.html",$name);
472 $toggletext = 'Stop!';
474 $toggleref=sprintf("%s-slide.html",$name);
475 $toggletext = 'Play->';
478 unless (open($F,'>'.$fn)) {
479 warn "cannot open \"$fn\": $!";
482 binmode($F, ":utf8");
483 if ($refresh eq 'slide') {
488 -head=>meta({-http_equiv=>'Refresh',
489 -content=>"3; url=$fwdref"}),
490 -style=>{-src=>$inc."gallery.css"},
494 print $F start_html(-title=>$title,
497 -style=>{-src=>$inc."gallery.css"},
500 print $F start_table({-class=>'navi'}),start_Tr,"\n",
501 td(a({-href=>"../index.html"},"Index")),"\n",
502 td(a({-href=>$bakref},"<<Prev")),"\n",
503 td(a({-href=>$toggleref},$toggletext)),"\n",
504 td(a({-href=>$fwdref},"Next>>")),"\n",
505 td({-class=>'title'},$title),"\n",
508 center(table({-class=>'picframe'},
509 Tr(td(img({-src=>$imgsrc}))))),"\n",
516 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
517 if (isnewer($self->{-fullpath},$fn)) {
519 unless (open($F,'>'.$fn)) {
520 warn "cannot open \"$fn\": $!";
523 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
524 print $F start_html(-title=>$title,
526 -style=>{-src=>$inc."gallery.css"},
528 {-src=>$inc."mootools.js"},
529 {-src=>$inc."urlparser.js"},
530 {-src=>$inc."infopage.js"},
534 table({-class=>'ipage'},
535 Tr(td(img({-src=>$imgsrc})),
536 td($self->infotable))),
537 a({-href=>'../index.html',-class=>'conceal'},
547 my $fn = $self->{-fullpath}.'/index.html';
548 my $block = $self->{-fullpath}.'/.noindex';
549 $fn = '/dev/null' if ( -f $block );
551 unless (open($IND,'>'.$fn)) {
552 warn "cannot open $fn: $!";
555 binmode($IND, ":utf8");
556 $self->{-IND} = $IND;
558 my $inc = $self->{-inc};
559 my $title = $self->{-title};
562 $rsslink=Link({-rel=>'alternate',
563 -type=>'application/rss+xml',
565 -href=>$self->{-rss}});
567 print $IND start_html(-title => $title,
570 -style=>{-src=>$inc."gallery.css"},
572 {-src=>$inc."mootools.js"},
573 {-src=>$inc."overlay.js"},
574 {-src=>$inc."urlparser.js"},
575 {-src=>$inc."multibox.js"},
576 {-src=>$inc."slideshow.js"},
577 {-src=>$inc."gallery.js"},
578 {-code=>"var incPrefix='$inc';"}
580 a({-href=>"../index.html"},"UP"),"\n",
588 my $IND = $self->{-IND};
590 print $IND end_center,end_html,"\n";
592 close($IND) if ($IND);
595 my $rsstitle=sprintf "%s [%d images, %d subalbums]",
599 my $rsslink=$rssobj->{'rss'}->channel('link')."index.html";
600 $rssobj->{'rss'}->add_item(
601 title => $self->{-title},
603 description => $rsstitle,
610 my $IND = $self->{-IND};
612 print $IND h2("Albums"),"\n",start_table,"\n";
617 my $IND = $self->{-parent}->{-IND};
618 my $name = $self->{-base};
619 my $title = $self->{-title};
621 $self->{-parent}->{-numofsubs}++;
622 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
623 td(a({-href=>$name.'/index.html'},$title))),"\n";
628 my $IND = $self->{-IND};
630 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
635 my $IND = $self->{-IND};
636 my $first = $self->{-firstimg}->{-base};
637 my $slideref = sprintf(".html/%s-slide.html",$first);
639 print $IND h2("Images ",
640 a({-href=>$slideref,-class=>'showStart',-id=>$first},
641 '> slideshow')),"\n";
646 my $IND = $self->{-parent}->{-IND};
647 my $name = $self->{-base};
648 my $title = $self->{-info}->{'Comment'};
649 $title = $name unless ($title);
650 my $thumb = $self->{$sizes[0]}->{'url'};
651 my $info = $self->{-info};
652 my ($w, $h) = dim($info);
654 my $i=0+$self->{-parent}->{-numofimgs};
655 $self->{-parent}->{-numofimgs}++;
657 print $IND a({-name=>$name}),"\n",
658 start_table({-class=>'slide'}),start_Tr,start_td,"\n",
659 div({-class=>'slidetitle',-id=>$name},
660 "\n ",a({-href=>".html/$name-info.html",
661 -title=>'Image Info: '.$name,
664 div({-class=>'slideimage',-id=>$name},
665 "\n ",a({-href=>".html/$name-static.html",
669 img({-src=>$thumb})),"\n"),"\n",
670 start_div({-class=>'varimages',-id=>$name,-name=>$title}),"\n";
671 foreach my $sz(@sizes) {
672 my $src=$self->{$sz}->{'url'};
673 my $w=$self->{$sz}->{'dim'}->[0];
674 my $h=$self->{$sz}->{'dim'}->[1];
675 print $IND " ",a({-href=>$src,
677 (($sz == 640)?"slideshowThumbnail":""),
678 -title=>"Reduced to ".$w."x".$h},
681 print $IND " ",a({-href=>$name,
682 -title=>'Original'},$w."x".$h),
684 end_td,end_Tr,end_table,"\n";
689 my $IND = $self->{-IND};
691 print $IND br({-clear=>'all'}),hr,"\n\n";
696 my $info = $self->{-info};
713 $msg.=start_table({-class=>'infotable'})."\n";
714 foreach my $k(@infokeys) {
715 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
717 $msg.=end_table."\n";