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);
114 unless (opendir($D,$fullpath)) {
115 warn "cannot opendir $fullpath: $!";
118 while (my $de = readdir($D)) {
119 next if ($de =~ /^\./);
120 my $child = $self->new($de);
122 push(@rdirlist,$child);
123 } elsif ($child->isimg) {
124 push(@rimglist,$child);
128 my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
129 undef @rdirlist; # inplace sorting would be handy here
130 my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
131 undef @rimglist; # optimize away unsorted versions
132 $self->{-firstimg} = $imglist[0];
134 print "Dir: $self->{-fullpath}\n" if ($debug);
136 # 1. first of all, fill title for this directory and create hidden subdirs
140 # 2. recurse into subdirectories to get their titles filled
141 # before we start writing out subalbum list
143 foreach my $dir(@dirlist) {
147 # 3. iterate through images to build cross-links,
150 foreach my $img(@imglist) {
151 # list-linking must be done before generating
152 # aux html because aux pages rely on prev/next refs
154 $previmg->{-nextimg} = $img;
155 $img->{-previmg} = $previmg;
160 # 4. create scaled versions and aux html pages
162 foreach my $img(@imglist) {
163 # scaled versions must be generated before aux html
164 # and main image index because they both rely on
165 # refs to scaled images and they may be just original
166 # images, this is not known before we try scaling.
168 # finally, make aux html pages
172 # 5. start building index.html for the directory
176 # 6. iterate through subdirectories to build subalbums list
180 foreach my $dir(@dirlist) {
186 # 7. iterate through images to build thumb list
190 foreach my $img(@imglist) {
191 print "Img: $img->{-fullpath}\n" if ($debug);
197 # 8. comlplete building index.html for the directory
204 return ( -d $self->{-fullpath} );
209 my $fullpath = $self->{-fullpath};
210 return 0 unless ( -f $fullpath );
211 my $info = image_info($fullpath);
212 if (my $error = $info->{error}) {
213 if (($error !~ "Unrecognized file format") &&
214 ($error !~ "Can't read head")) {
215 warn "File \"$fullpath\": $error\n";
220 tryapp12($info) unless ($info->{'ExifVersion'});
223 $self->{-info} = $info;
228 my $info = shift; # this is not a method
230 # dirty hack to take care of Image::Info parser strangeness
231 foreach my $k(keys %$info) {
232 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
234 return unless ($app12); # bad luck
236 foreach my $ln(split /[\r\n]+/,$app12) {
237 $ln =~ s/[[:^print:]\000]/ /g;
238 unless ($seenfirstline) {
243 my ($k,$v)=split /=/,$ln,2;
244 if ($k eq 'TimeDate') {
245 $info->{'DateTime'} =
246 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
248 } elsif ($k eq 'Shutter') {
249 $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
250 } elsif ($k eq 'Flash') {
251 $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
252 } elsif ($k eq 'Type') {
253 $info->{'Model'} = $v;
254 } elsif ($k eq 'Version') {
255 $info->{'Software'} = $v;
256 } elsif ($k eq 'Fnumber') {
257 $info->{'FNumber'} = $v;
264 my $fullpath = $self->{-fullpath};
265 for my $subdir(@sizes, 'html') {
266 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
267 mkdir($tdir,0755) unless ( -d $tdir );
274 my $fullpath = $self->{-fullpath};
277 if (open($T,'<'.$fullpath.'/.title')) {
279 $title =~ s/[\r\n]*$//;
282 if ($asktitle || (!$title && !$noasktitle)) {
283 my $prompt = $self->{-base};
284 $prompt = '/' unless ($prompt);
285 my $OUT = $term->OUT || \*STDOUT;
286 print $OUT "Enter title for $fullpath\n";
287 $title = $term->readline($prompt.' >',$title);
288 $term->addhistory($title) if ($title);
289 if (open($T,'>'.$fullpath.'/.title')) {
290 print $T $title,"\n";
295 $title=substr($fullpath,length($self->{-root}));
297 $self->{-title}=$title;
298 print "title in $fullpath is $title\n" if ($debug);
303 my $fn = $self->{-fullpath};
304 my $name = $self->{-base};
305 my $dn = $self->{-parent}->{-fullpath};
306 my ($w, $h) = dim($self->{-info});
307 my $max = ($w > $h)?$w:$h;
309 foreach my $size(@sizes) {
310 my $nref = '.'.$size.'/'.$name;
311 my $nfn = $dn.'/'.$nref;
312 my $factor=$size/$max;
314 $self->{$size} = $name; # unscaled version will do
316 $self->{$size} = $nref;
317 if (isnewer($fn,$nfn)) {
318 doscaling($fn,$nfn,$factor,$w,$h);
325 my ($fn1,$fn2) = @_; # this is not a method
326 my @stat1=stat($fn1);
327 my @stat2=stat($fn2);
328 return (!@stat2 || ($stat1[9] > $stat2[9]));
329 # true if $fn2 is absent or is older than $fn1
333 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
334 my $im = new Image::Magick;
336 print "doscaling $src -> $dest by $factor\n" if ($debug);
337 $err = $im->Read($src);
339 $im->Scale(width=>$w*$factor,height=>$h*$factor);
340 $err=$im->Write($dest);
341 warn "ImageMagick: write \"$dest\": $err" if ($err);
342 } else { # fallback to command-line tools
343 warn "ImageMagick: read \"$src\": $err";
344 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
351 my $name = $self->{-base};
352 my $dn = $self->{-parent}->{-fullpath};
353 my $pref = $self->{-previmg}->{-base};
354 my $nref = $self->{-nextimg}->{-base};
355 my $inc = $self->{-inc};
356 my $title = $self->{-info}->{'Comment'};
357 $title = $name unless ($title);
359 print "slide: \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
362 for my $refresh('static', 'slide') {
363 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
364 my $imgsrc = '../'.$self->{$sizes[1]};
368 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
370 $fwdref = '../index.html';
373 $bakref = sprintf("%s-%s.html",$pref,$refresh);
375 $bakref = '../index.html';
379 if ($refresh eq 'slide') {
380 $toggleref=sprintf("%s-static.html",$name);
381 $toggletext = 'Stop!';
383 $toggleref=sprintf("%s-slide.html",$name);
384 $toggletext = 'Play->';
387 unless (open($F,'>'.$fn)) {
388 warn "cannot open \"$fn\": $!";
391 if ($refresh eq 'slide') {
392 print $F start_html(-title=>$title,
394 -head=>meta({-http_equiv=>'Refresh',
395 -content=>"3; url=$fwdref"}),
396 -style=>{-src=>$inc."gallery.css"},
400 print $F start_html(-title=>$title,
402 -style=>{-src=>$inc."gallery.css"},
405 print $F start_center,"\n",
407 start_table({-class=>'navi'}),start_Tr,"\n",
408 td(a({-href=>"../index.html"},"Index")),"\n",
409 td(a({-href=>$bakref},"<<Prev")),"\n",
410 td(a({-href=>$toggleref},$toggletext)),"\n",
411 td(a({-href=>$fwdref},"Next>>")),"\n",
414 table({-class=>'picframe'},
415 Tr(td(img({-src=>$imgsrc})))),"\n",
420 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
422 unless (open($F,'>'.$fn)) {
423 warn "cannot open \"$fn\": $!";
428 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
429 print $F start_html(-title=>$title,
430 -style=>{-src=>$inc."gallery.css"},),"\n",
433 table({-class=>'ipage'},Tr(td(img({-src=>$imgsrc})),td($self->infotable))),
434 a({-href=>'../index.html'},'Index'),"\n",
442 my $fn = $self->{-fullpath}.'/index.html';
444 unless (open($IND,'>'.$fn)) {
445 warn "cannot open $fn: $!";
448 $self->{-IND} = $IND;
450 my $inc = $self->{-inc};
451 my $title = $self->{-title};
452 print $IND start_html(-title => $title,
453 -style=>{-src=>[$inc."gallery.css",
454 $inc."lightbox.css"]},
455 -script=>[{-code=>"var incPrefix='$inc';"},
456 {-src=>$inc."gallery.js"},
457 {-src=>$inc."lightbox.js"}]),
458 a({-href=>"../index.html"},"UP"),"\n",
466 my $IND = $self->{-IND};
468 print $IND end_center,end_html,"\n";
470 close($IND) if ($IND);
476 my $IND = $self->{-IND};
478 print $IND h2("Albums"),"\n",start_table,"\n";
483 my $IND = $self->{-parent}->{-IND};
484 my $name = $self->{-base};
485 my $title = $self->{-title};
487 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
488 td(a({-href=>$name.'/index.html'},$title))),"\n";
493 my $IND = $self->{-IND};
495 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
500 my $IND = $self->{-IND};
501 my $first = $self->{-firstimg}->{-base};
502 my $slideref = sprintf(".html/%s-slide.html",$first);
504 print $IND h2("Images"),"\n",
505 a({-href=>$slideref},'Slideshow'),
511 my $IND = $self->{-parent}->{-IND};
512 my $name = $self->{-base};
513 my $title = $self->{-info}->{'Comment'};
514 $title = $name unless ($title);
515 my $thumb = $self->{$sizes[0]};
516 my $medium = $self->{$sizes[1]};
517 my $info = $self->{-info};
518 my ($w, $h) = dim($info);
520 print $IND start_div({-class=>'ibox',-id=>$name,
521 -OnClick=>"HideIbox('$name');"}),"\n",
522 start_div({-class=>'iboxtitle'}),
523 span({-style=>'float: left;'},b("Info for $name")),
524 span({-style=>'float: right;'},
525 a({-href=>"#",-OnClick=>"HideIbox('$name');"},"Close")),
526 br({-clear=>'all'}),"\n",
531 print $IND table({-class=>'slide'},Tr(td(
532 a({-href=>".html/$name-info.html",
533 -onClick=>"return showIbox('$name');"},$title),
535 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
536 img({-src=>$thumb})),
538 a({-href=>$name},"($w x $h)"),
544 my $IND = $self->{-IND};
546 print $IND br({-clear=>'all'}),hr,"\n\n";
551 my $info = $self->{-info};
568 $msg.=start_table({-class=>'infotable'})."\n";
569 foreach my $k(@infokeys) {
570 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
572 $msg.=end_table."\n";