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/;
33 use Image::Info qw/image_info dim/;
39 my @sizes = (160, 640);
41 ######################################################################
47 GetOptions('asktitle'=>\$asktitle,
48 'noasktitle'=>\$noasktitle,
51 my $term = new Term::ReadLine "Edit Title";
53 FsObj->new(getcwd)->iterate;
63 my $fullpath = $parent->{-fullpath}.'/'.$name;
66 -root=>$parent->{-root},
69 -inc=>'../'.$parent->{-inc},
82 print "new $class:\n";
83 foreach my $k(keys %$self) {
84 print "\t$k\t=\t$self->{$k}\n";
91 my $fullpath=shift; # this is not a method
92 my $depth=20; # arbitrary max depth
95 while ( ! -d $fullpath."/".$inc ) {
97 last unless ($depth-- > 0);
100 return $inc.'/'; # prefix with trailing slash
102 return 'NO-.INCLUDE-IN-PATH/'; # won't work anyway
108 my $fullpath .= $self->{-fullpath};
109 print "iterate in dir $fullpath\n" if ($debug);
115 unless (opendir($D,$fullpath)) {
116 warn "cannot opendir $fullpath: $!";
119 while (my $de = readdir($D)) {
120 next if ($de =~ /^\./);
121 my $child = $self->new($de);
123 push(@rdirlist,$child);
124 } elsif ($child->isimg) {
125 my @stat = stat($child->{-fullpath});
126 $youngest = $stat[9] if ($youngest < $stat[9]);
127 push(@rimglist,$child);
131 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
132 undef @rdirlist; # inplace sorting would be handy here
133 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
134 undef @rimglist; # optimize away unsorted versions
135 $self->{-firstimg} = $imglist[0];
137 print "Dir: $self->{-fullpath}\n" if ($debug);
139 # 1. first of all, fill title for this directory and create hidden subdirs
143 # 2. recurse into subdirectories to get their titles filled
144 # before we start writing out subalbum list
146 foreach my $dir(@dirlist) {
150 # no need to go beyond this point if the directory timestamp did not
151 # change since we built index.html file last time.
153 my @istat = stat($self->{-fullpath}.'/index.html');
154 return unless ($youngest > $istat[9]);
156 # 3. iterate through images to build cross-links,
159 foreach my $img(@imglist) {
160 # list-linking must be done before generating
161 # aux html because aux pages rely on prev/next refs
163 $previmg->{-nextimg} = $img;
164 $img->{-previmg} = $previmg;
169 # 4. create scaled versions and aux html pages
171 foreach my $img(@imglist) {
172 # scaled versions must be generated before aux html
173 # and main image index because they both rely on
174 # refs to scaled images and they may be just original
175 # images, this is not known before we try scaling.
177 # finally, make aux html pages
181 # 5. start building index.html for the directory
185 # 6. iterate through subdirectories to build subalbums list
189 foreach my $dir(@dirlist) {
195 # 7. iterate through images to build thumb list
199 foreach my $img(@imglist) {
200 print "Img: $img->{-fullpath}\n" if ($debug);
206 # 8. comlplete building index.html for the directory
213 return ( -d $self->{-fullpath} );
218 my $fullpath = $self->{-fullpath};
219 return 0 unless ( -f $fullpath );
220 my $info = image_info($fullpath);
221 if (my $error = $info->{error}) {
222 if (($error !~ "Unrecognized file format") &&
223 ($error !~ "Can't read head")) {
224 warn "File \"$fullpath\": $error\n";
229 tryapp12($info) unless ($info->{'ExifVersion'});
232 $self->{-info} = $info;
237 my $info = shift; # this is not a method
239 # dirty hack to take care of Image::Info parser strangeness
240 foreach my $k(keys %$info) {
241 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
243 return unless ($app12); # bad luck
245 foreach my $ln(split /[\r\n]+/,$app12) {
246 $ln =~ s/[[:^print:]\000]/ /g;
247 unless ($seenfirstline) {
252 my ($k,$v)=split /=/,$ln,2;
253 if ($k eq 'TimeDate') {
254 $info->{'DateTime'} =
255 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
257 } elsif ($k eq 'Shutter') {
258 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
259 } elsif ($k eq 'Flash') {
260 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
261 } elsif ($k eq 'Type') {
262 $info->{'Model'} = $v;
263 } elsif ($k eq 'Version') {
264 $info->{'Software'} = $v;
265 } elsif ($k eq 'Fnumber') {
266 $info->{'FNumber'} = $v;
273 my $fullpath = $self->{-fullpath};
274 for my $subdir(@sizes, 'html') {
275 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
276 mkdir($tdir,0755) unless ( -d $tdir );
283 my $fullpath = $self->{-fullpath};
286 if (open($T,'<'.$fullpath.'/.title')) {
288 $title =~ s/[\r\n]*$//;
291 if ($asktitle || (!$title && !$noasktitle)) {
292 my $prompt = $self->{-base};
293 $prompt = '/' unless ($prompt);
294 my $OUT = $term->OUT || \*STDOUT;
295 print $OUT "Enter title for $fullpath\n";
296 $title = $term->readline($prompt.' >',$title);
297 $term->addhistory($title) if ($title);
298 if (open($T,'>'.$fullpath.'/.title')) {
299 print $T $title,"\n";
304 $title=substr($fullpath,length($self->{-root}));
306 $self->{-title}=$title;
307 print "title in $fullpath is $title\n" if ($debug);
312 my $fn = $self->{-fullpath};
313 my $name = $self->{-base};
314 my $dn = $self->{-parent}->{-fullpath};
315 my ($w, $h) = dim($self->{-info});
316 my $max = ($w > $h)?$w:$h;
318 foreach my $size(@sizes) {
319 my $nref = '.'.$size.'/'.$name;
320 my $nfn = $dn.'/'.$nref;
321 my $factor=$size/$max;
323 $self->{$size} = $name; # unscaled version will do
325 $self->{$size} = $nref;
326 if (isnewer($fn,$nfn)) {
327 doscaling($fn,$nfn,$factor,$w,$h);
334 my ($fn1,$fn2) = @_; # this is not a method
335 my @stat1=stat($fn1);
336 my @stat2=stat($fn2);
337 return (!@stat2 || ($stat1[9] > $stat2[9]));
338 # true if $fn2 is absent or is older than $fn1
342 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
343 my $im = new Image::Magick;
345 print "doscaling $src -> $dest by $factor\n" if ($debug);
346 $err = $im->Read($src);
348 $im->Scale(width=>$w*$factor,height=>$h*$factor);
349 $err=$im->Write($dest);
350 warn "ImageMagick: write \"$dest\": $err" if ($err);
351 } else { # fallback to command-line tools
352 warn "ImageMagick: read \"$src\": $err";
353 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
360 my $name = $self->{-base};
361 my $dn = $self->{-parent}->{-fullpath};
362 my $pref = $self->{-previmg}->{-base};
363 my $nref = $self->{-nextimg}->{-base};
364 my $inc = $self->{-inc};
365 my $title = $self->{-info}->{'Comment'};
366 $title = $name unless ($title);
368 print "slide: \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
371 for my $refresh('static', 'slide') {
372 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
373 if (isnewer($self->{-fullpath},$fn)) {
374 my $imgsrc = '../'.$self->{$sizes[1]};
378 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
380 $fwdref = '../index.html';
383 $bakref = sprintf("%s-%s.html",$pref,$refresh);
385 $bakref = '../index.html';
389 if ($refresh eq 'slide') {
390 $toggleref=sprintf("%s-static.html",$name);
391 $toggletext = 'Stop!';
393 $toggleref=sprintf("%s-slide.html",$name);
394 $toggletext = 'Play->';
397 unless (open($F,'>'.$fn)) {
398 warn "cannot open \"$fn\": $!";
401 if ($refresh eq 'slide') {
405 -head=>meta({-http_equiv=>'Refresh',
406 -content=>"3; url=$fwdref"}),
407 -style=>{-src=>$inc."gallery.css"},
411 print $F start_html(-title=>$title,
413 -style=>{-src=>$inc."gallery.css"},
416 print $F start_center,"\n",
418 start_table({-class=>'navi'}),start_Tr,"\n",
419 td(a({-href=>"../index.html"},"Index")),"\n",
420 td(a({-href=>$bakref},"<<Prev")),"\n",
421 td(a({-href=>$toggleref},$toggletext)),"\n",
422 td(a({-href=>$fwdref},"Next>>")),"\n",
425 table({-class=>'picframe'},
426 Tr(td(img({-src=>$imgsrc})))),"\n",
434 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
435 if (isnewer($self->{-fullpath},$fn)) {
437 unless (open($F,'>'.$fn)) {
438 warn "cannot open \"$fn\": $!";
441 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
442 print $F start_html(-title=>$title,
443 -style=>{-src=>$inc."gallery.css"},),"\n",
446 table({-class=>'ipage'},
447 Tr(td(img({-src=>$imgsrc})),
448 td($self->infotable))),
449 a({-href=>'../index.html'},'Index'),"\n",
458 my $fn = $self->{-fullpath}.'/index.html';
459 my $block = $self->{-fullpath}.'/.noindex';
460 $fn = '/dev/null' if ( -f $block );
462 unless (open($IND,'>'.$fn)) {
463 warn "cannot open $fn: $!";
466 $self->{-IND} = $IND;
468 my $inc = $self->{-inc};
469 my $title = $self->{-title};
470 print $IND start_html(-title => $title,
471 -style=>{-src=>[$inc."gallery.css",
472 $inc."lightbox.css"]},
473 -script=>[{-code=>"var incPrefix='$inc';"},
474 {-src=>$inc."gallery.js"},
475 {-src=>$inc."lightbox.js"}]),
476 a({-href=>"../index.html"},"UP"),"\n",
484 my $IND = $self->{-IND};
486 print $IND end_center,end_html,"\n";
488 close($IND) if ($IND);
494 my $IND = $self->{-IND};
496 print $IND h2("Albums"),"\n",start_table,"\n";
501 my $IND = $self->{-parent}->{-IND};
502 my $name = $self->{-base};
503 my $title = $self->{-title};
505 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
506 td(a({-href=>$name.'/index.html'},$title))),"\n";
511 my $IND = $self->{-IND};
513 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
518 my $IND = $self->{-IND};
519 my $first = $self->{-firstimg}->{-base};
520 my $slideref = sprintf(".html/%s-slide.html",$first);
522 print $IND h2("Images"),"\n",
523 a({-href=>$slideref},'Slideshow'),
529 my $IND = $self->{-parent}->{-IND};
530 my $name = $self->{-base};
531 my $title = $self->{-info}->{'Comment'};
532 $title = $name unless ($title);
533 my $thumb = $self->{$sizes[0]};
534 my $medium = $self->{$sizes[1]};
535 my $info = $self->{-info};
536 my ($w, $h) = dim($info);
538 print $IND start_div({-class=>'ibox',-id=>$name,
539 -OnClick=>"HideIbox('$name');"}),"\n",
540 start_div({-class=>'iboxtitle'}),
541 span({-style=>'float: left;'},b("Info for $name")),
542 span({-style=>'float: right;'},
543 a({-href=>"#",-OnClick=>"HideIbox('$name');"},"Close")),
544 br({-clear=>'all'}),"\n",
549 print $IND table({-class=>'slide'},Tr(td(
550 a({-href=>".html/$name-info.html",-title=>'Image Info',
551 -onClick=>"return showIbox('$name');"},$title),
553 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
554 img({-src=>$thumb})),
556 a({-href=>$name,-title=>'Original Image'},"($w x $h)"),
562 my $IND = $self->{-IND};
564 print $IND br({-clear=>'all'}),hr,"\n\n";
569 my $info = $self->{-info};
586 $msg.=start_table({-class=>'infotable'})."\n";
587 foreach my $k(@infokeys) {
588 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
590 $msg.=end_table."\n";