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.
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 $self->{-info} = $info;
226 my $fullpath = $self->{-fullpath};
227 for my $subdir(@sizes, 'html') {
228 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
229 mkdir($tdir,0755) unless ( -d $tdir );
236 my $fullpath = $self->{-fullpath};
239 if (open($T,'<'.$fullpath.'/.title')) {
241 $title =~ s/[\r\n]*$//;
244 if ($asktitle || (!$title && !$noasktitle)) {
245 my $prompt = $self->{-base};
246 $prompt = '/' unless ($prompt);
247 my $OUT = $term->OUT || \*STDOUT;
248 print $OUT "Enter title for $fullpath\n";
249 $title = $term->readline($prompt.' >',$title);
250 $term->addhistory($title) if ($title);
251 if (open($T,'>'.$fullpath.'/.title')) {
252 print $T $title,"\n";
257 $title=substr($fullpath,length($self->{-root}));
259 $self->{-title}=$title;
260 print "title in $fullpath is $title\n" if ($debug);
265 my $fn = $self->{-fullpath};
266 my $name = $self->{-base};
267 my $dn = $self->{-parent}->{-fullpath};
268 my ($w, $h) = dim($self->{-info});
269 my $max = ($w > $h)?$w:$h;
271 foreach my $size(@sizes) {
272 my $nref = '.'.$size.'/'.$name;
273 my $nfn = $dn.'/'.$nref;
274 my $factor=$size/$max;
276 $self->{$size} = $name; # unscaled version will do
278 $self->{$size} = $nref;
279 if (isnewer($fn,$nfn)) {
280 doscaling($fn,$nfn,$factor,$w,$h);
287 my ($fn1,$fn2) = @_; # this is not a method
288 my @stat1=stat($fn1);
289 my @stat2=stat($fn2);
290 return (!@stat2 || ($stat1[9] > $stat2[9]));
291 # true if $fn2 is absent or is older than $fn1
295 my ($src,$dest,$factor,$w,$h) = @_; # this is not a method
296 my $im = new Image::Magick;
298 print "doscaling $src -> $dest by $factor\n" if ($debug);
299 $err = $im->Read($src);
301 $im->Scale(width=>$w*$factor,height=>$h*$factor);
302 $err=$im->Write($dest);
303 warn "ImageMagick: write \"$dest\": $err" if ($err);
304 } else { # fallback to command-line tools
305 warn "ImageMagick: read \"$src\": $err";
306 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
313 my $name = $self->{-base};
314 my $dn = $self->{-parent}->{-fullpath};
315 my $pref = $self->{-previmg}->{-base};
316 my $nref = $self->{-nextimg}->{-base};
317 my $inc = $self->{-inc};
318 my $title = $self->{-info}->{'Comment'};
319 $title = $name unless ($title);
321 print "slide: \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
324 for my $refresh('static', 'slide') {
325 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
326 my $imgsrc = '../'.$self->{$sizes[1]};
330 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
332 $fwdref = '../index.html';
335 $bakref = sprintf("%s-%s.html",$pref,$refresh);
337 $bakref = '../index.html';
341 if ($refresh eq 'slide') {
342 $toggleref=sprintf("%s-static.html",$name);
343 $toggletext = 'Stop!';
345 $toggleref=sprintf("%s-slide.html",$name);
346 $toggletext = 'Play->';
349 unless (open($F,'>'.$fn)) {
350 warn "cannot open \"$fn\": $!";
353 if ($refresh eq 'slide') {
354 print $F start_html(-title=>$title,
356 -head=>meta({-http_equiv=>'Refresh',
357 -content=>"3; url=$fwdref"}),
358 -style=>{-src=>$inc."gallery.css"},
362 print $F start_html(-title=>$title,
364 -style=>{-src=>$inc."gallery.css"},
367 print $F start_center,"\n",
369 start_table({-class=>'navi'}),start_Tr,"\n",
370 td(a({-href=>"../index.html"},"Index")),"\n",
371 td(a({-href=>$bakref},"<<Prev")),"\n",
372 td(a({-href=>$toggleref},$toggletext)),"\n",
373 td(a({-href=>$fwdref},"Next>>")),"\n",
376 table({-class=>'picframe'},
377 Tr(td(img({-src=>$imgsrc})))),"\n",
382 my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
384 unless (open($F,'>'.$fn)) {
385 warn "cannot open \"$fn\": $!";
390 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
391 print $F start_html(-title=>$title,
392 -style=>{-src=>$inc."gallery.css"},),"\n",
395 table({-class=>'ipage'},Tr(td(img({-src=>$imgsrc})),td($self->infotable))),
396 a({-href=>'../index.html'},'Index'),"\n",
404 my $fn = $self->{-fullpath}.'/index.html';
406 unless (open($IND,'>'.$fn)) {
407 warn "cannot open $fn: $!";
410 $self->{-IND} = $IND;
412 my $inc = $self->{-inc};
413 my $title = $self->{-title};
414 print $IND start_html(-title => $title,
415 -style=>{-src=>[$inc."gallery.css",
416 $inc."lightbox.css"]},
417 -script=>[{-code=>"var incPrefix='$inc';"},
418 {-src=>$inc."gallery.js"},
419 {-src=>$inc."lightbox.js"}]),
420 a({-href=>"../index.html"},"UP"),"\n",
428 my $IND = $self->{-IND};
430 print $IND end_center,end_html,"\n";
432 close($IND) if ($IND);
438 my $IND = $self->{-IND};
440 print $IND h2("Albums"),"\n",start_table,"\n";
445 my $IND = $self->{-parent}->{-IND};
446 my $name = $self->{-base};
447 my $title = $self->{-title};
449 print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
450 td(a({-href=>$name.'/index.html'},$title))),"\n";
455 my $IND = $self->{-IND};
457 print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
462 my $IND = $self->{-IND};
463 my $first = $self->{-firstimg}->{-base};
464 my $slideref = sprintf(".html/%s-slide.html",$first);
466 print $IND h2("Images"),"\n",
467 a({-href=>$slideref},'Slideshow'),
473 my $IND = $self->{-parent}->{-IND};
474 my $name = $self->{-base};
475 my $title = $self->{-info}->{'Comment'};
476 $title = $name unless ($title);
477 my $thumb = $self->{$sizes[0]};
478 my $medium = $self->{$sizes[1]};
479 my $info = $self->{-info};
480 my ($w, $h) = dim($info);
482 print $IND start_div({-class=>'ibox',-id=>$name,
483 -OnClick=>"HideIbox('$name');"}),"\n",
484 start_div({-class=>'iboxtitle'}),
485 span({-style=>'float: left;'},b("Info for $name")),
486 span({-style=>'float: right;'},
487 a({-href=>"#",-OnClick=>"HideIbox('$name');"},"Close")),
488 br({-clear=>'all'}),"\n",
493 print $IND table({-class=>'slide'},Tr(td(
494 a({-href=>".html/$name-info.html",
495 -onClick=>"return showIbox('$name');"},$title),
497 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
498 img({-src=>$thumb})),
500 a({-href=>$name},"($w x $h)"),
506 my $IND = $self->{-IND};
508 print $IND br({-clear=>'all'}),hr,"\n\n";
513 my $info = $self->{-info};
530 $msg.=start_table({-class=>'infotable'})."\n";
531 foreach my $k(@infokeys) {
532 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
534 $msg.=end_table."\n";