a0cb8eebef6089317ae33056ddfef89ca91737e7
[mkgallery.git] / mkgallery.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
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/
8
9 # Copyright (c) 2006 Eugene G. Crosser
10
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.
14 #
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:
18 #
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.
26
27 package FsObj;
28
29 use strict;
30 use Carp;
31 use POSIX qw/getcwd strftime/;
32 use CGI qw/:html *table *Tr *center *div/;
33 use Image::Info qw/image_info dim/;
34 use Term::ReadLine;
35 use Getopt::Long;
36
37 use Image::Magick;
38
39 my @sizes = (160, 640);
40
41 ######################################################################
42
43 my $debug = 0;
44 my $asktitle = 0;
45 my $noasktitle = 0;
46
47 GetOptions('asktitle'=>\$asktitle,
48                 'noasktitle'=>\$noasktitle,
49                 'debug'=>\$debug);
50
51 my $term = new Term::ReadLine "Edit Title";
52
53 FsObj->new(getcwd)->iterate;
54
55 sub new {
56         my $this = shift;
57         my $class;
58         my $self;
59         if (ref($this)) {
60                 $class = ref($this);
61                 my $parent = $this;
62                 my $name = shift;
63                 my $fullpath = $parent->{-fullpath}.'/'.$name;
64                 $self = {
65                                 -parent=>$parent,
66                                 -root=>$parent->{-root},
67                                 -base=>$name,
68                                 -fullpath=>$fullpath,
69                                 -inc=>'../'.$parent->{-inc},
70                         };
71         } else {
72                 $class = $this;
73                 my $root=shift;
74                 $self = {
75                                 -root=>$root,
76                                 -fullpath=>$root,
77                                 -inc=>getinc($root),
78                         };
79         }
80         bless $self, $class;
81         if ($debug) {
82                 print "new $class:\n";
83                 foreach my $k(keys %$self) {
84                         print "\t$k\t=\t$self->{$k}\n";
85                 }
86         }
87         return $self;
88 }
89
90 sub getinc {
91         my $fullpath=shift;     # this is not a method
92         my $depth=20;           # arbitrary max depth
93
94         my $inc=".include";
95         while ( ! -d $fullpath."/".$inc ) {
96                 $inc = "../".$inc;
97                 last unless ($depth-- > 0);
98         }
99         if ($depth > 0) {
100                 return $inc.'/';                # prefix with trailing slash
101         } else {
102                 return 'NO-.INCLUDE-IN-PATH/';  # won't work anyway
103         }
104 }
105
106 sub iterate {
107         my $self = shift;
108         my $fullpath .= $self->{-fullpath};
109         print "iterate in dir $fullpath\n" if ($debug);
110
111         my $youngest=0;
112         my @rdirlist;
113         my @rimglist;
114         my $D;
115         unless (opendir($D,$fullpath)) {
116                 warn "cannot opendir $fullpath: $!";
117                 return;
118         }
119         while (my $de = readdir($D)) {
120                 next if ($de =~ /^\./);
121                 my $child = $self->new($de);
122                 if ($child->isdir) {
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);
128                 }
129         }
130         closedir($D);
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];
136
137         print "Dir: $self->{-fullpath}\n" if ($debug);
138
139 # 1. first of all, fill title for this directory and create hidden subdirs
140
141         $self->initdir;
142
143 # 2. recurse into subdirectories to get their titles filled
144 #    before we start writing out subalbum list
145
146         foreach my $dir(@dirlist) {
147                 $dir->iterate;
148         }
149
150 # no need to go beyond this point if the directory timestamp did not
151 # change since we built index.html file last time.
152
153         my @istat = stat($self->{-fullpath}.'/index.html');
154         return unless ($youngest > $istat[9]);
155
156 # 3. iterate through images to build cross-links,
157
158         my $previmg = undef;
159         foreach my $img(@imglist) {
160                 # list-linking must be done before generating
161                 # aux html because aux pages rely on prev/next refs
162                 if ($previmg) {
163                         $previmg->{-nextimg} = $img;
164                         $img->{-previmg} = $previmg;
165                 }
166                 $previmg=$img;
167         }
168
169 # 4. create scaled versions and aux html pages
170
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.
176                 $img->makescaled;
177                 # finally, make aux html pages
178                 $img->makeaux;
179         }
180
181 # 5. start building index.html for the directory
182
183         $self->startindex;
184
185 # 6. iterate through subdirectories to build subalbums list
186
187         if (@dirlist) {
188                 $self->startsublist;
189                 foreach my $dir(@dirlist) {
190                         $dir->sub_entry;
191                 }
192                 $self->endsublist;
193         }
194
195 # 7. iterate through images to build thumb list
196
197         if (@imglist) {
198                 $self->startimglist;
199                 foreach my $img(@imglist) {
200                         print "Img: $img->{-fullpath}\n" if ($debug);
201                         $img->img_entry;
202                 }
203                 $self->endimglist;
204         }
205
206 # 8. comlplete building index.html for the directory
207
208         $self->endindex;
209 }
210
211 sub isdir {
212         my $self = shift;
213         return ( -d $self->{-fullpath} );
214 }
215
216 sub isimg {
217         my $self = shift;
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";
225                 }
226                 return 0;
227         }
228
229         tryapp12($info) unless ($info->{'ExifVersion'});
230
231         $self->{-isimg} = 1;
232         $self->{-info} = $info;
233         return 1;
234 }
235
236 sub tryapp12 {
237         my $info = shift;       # this is not a method
238         my $app12;
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-/);
242         }
243         return unless ($app12); # bad luck
244         my $seenfirstline=0;
245         foreach my $ln(split /[\r\n]+/,$app12) {
246                 $ln =~ s/[[:^print:]\000]/ /g;
247                 unless ($seenfirstline) {
248                         $seenfirstline=1;
249                         $info->{'Make'}=$ln;
250                         next;
251                 }
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))
256                                                         unless ($v < 0);
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;
267                 }
268         }
269 }
270
271 sub initdir {
272         my $self = shift;
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 );
277         }
278         $self->edittitle;
279 }
280
281 sub edittitle {
282         my $self = shift;
283         my $fullpath = $self->{-fullpath};
284         my $title;
285         my $T;
286         if (open($T,'<'.$fullpath.'/.title')) {
287                 $title = <$T>;
288                 $title =~ s/[\r\n]*$//;
289                 close($T);
290         }
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";
300                         close($T);
301                 }
302         }
303         unless ($title) {
304                 $title=substr($fullpath,length($self->{-root}));
305         }
306         $self->{-title}=$title;
307         print "title in $fullpath is $title\n" if ($debug);
308 }
309
310 sub makescaled {
311         my $self = shift;
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;
317
318         foreach my $size(@sizes) {
319                 my $nref = '.'.$size.'/'.$name;
320                 my $nfn = $dn.'/'.$nref;
321                 my $factor=$size/$max;
322                 if ($factor >= 1) {
323                         $self->{$size} = $name; # unscaled version will do
324                 } else {
325                         $self->{$size} = $nref;
326                         if (isnewer($fn,$nfn)) {
327                                 doscaling($fn,$nfn,$factor,$w,$h);
328                         }
329                 }
330         }
331 }
332
333 sub isnewer {
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
339 }
340
341 sub doscaling {
342         my ($src,$dest,$factor,$w,$h) = @_;     # this is not a method
343         my $im = new Image::Magick;
344         my $err;
345         print "doscaling $src -> $dest by $factor\n" if ($debug);
346         $err = $im->Read($src);
347         unless ($err) {
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\"");
354         }
355         undef $im;
356 }
357
358 sub makeaux {
359         my $self = shift;
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);
367
368         print "slide: \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
369
370         # slideshow
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]};
375                         my $fwdref;
376                         my $bakref;
377                         if ($nref) {
378                                 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
379                         } else {
380                                 $fwdref = '../index.html';
381                         }
382                         if ($pref) {
383                                 $bakref = sprintf("%s-%s.html",$pref,$refresh);
384                         } else {
385                                 $bakref = '../index.html';
386                         }
387                         my $toggleref;
388                         my $toggletext;
389                         if ($refresh eq 'slide') {
390                                 $toggleref=sprintf("%s-static.html",$name);
391                                 $toggletext = 'Stop!';
392                         } else {
393                                 $toggleref=sprintf("%s-slide.html",$name);
394                                 $toggletext = 'Play-&gt;';
395                         }
396                         my $F;
397                         unless (open($F,'>'.$fn)) {
398                                 warn "cannot open \"$fn\": $!";
399                                 next;
400                         }
401                         if ($refresh eq 'slide') {
402                                 print $F start_html(
403                                         -title=>$title,
404                                         -bgcolor=>"#808080",
405                                         -head=>meta({-http_equiv=>'Refresh',
406                                                 -content=>"3; url=$fwdref"}),
407                                         -style=>{-src=>$inc."gallery.css"},
408                                         ),"\n";
409                                                 
410                         } else {
411                                 print $F start_html(-title=>$title,
412                                         -bgcolor=>"#808080",
413                                         -style=>{-src=>$inc."gallery.css"},
414                                         ),"\n";
415                         }
416                         print $F start_center,"\n",
417                                 h1($title),"\n",
418                                 start_table({-class=>'navi'}),start_Tr,"\n",
419                                 td(a({-href=>"../index.html"},"Index")),"\n",
420                                 td(a({-href=>$bakref},"&lt;&lt;Prev")),"\n",
421                                 td(a({-href=>$toggleref},$toggletext)),"\n",
422                                 td(a({-href=>$fwdref},"Next&gt;&gt;")),"\n",
423                                 end_Tr,
424                                 end_table,"\n",
425                                 table({-class=>'picframe'},
426                                         Tr(td(img({-src=>$imgsrc})))),"\n",
427                                 end_center,"\n",
428                                 end_html,"\n";
429                         close($F);
430                 }
431         }
432
433         # info html
434         my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
435         if (isnewer($self->{-fullpath},$fn)) {
436                 my $F;
437                 unless (open($F,'>'.$fn)) {
438                         warn "cannot open \"$fn\": $!";
439                         return;
440                 }
441                 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
442                 print $F start_html(-title=>$title,
443                                 -style=>{-src=>$inc."gallery.css"},),"\n",
444                         start_center,"\n",
445                         h1($title),"\n",
446                         table({-class=>'ipage'},
447                                 Tr(td(img({-src=>$imgsrc})),
448                                         td($self->infotable))),
449                         a({-href=>'../index.html'},'Index'),"\n",
450                         end_center,"\n",
451                         end_html,"\n";
452                 close($F);
453         }
454 }
455
456 sub startindex {
457         my $self = shift;
458         my $fn = $self->{-fullpath}.'/index.html';
459         my $block = $self->{-fullpath}.'/.noindex';
460         $fn = '/dev/null' if ( -f $block );
461         my $IND;
462         unless (open($IND,'>'.$fn)) {
463                 warn "cannot open $fn: $!";
464                 return;
465         }
466         $self->{-IND} = $IND;
467
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",
477                 start_center,"\n",
478                 h1($title),"\n",
479                 "\n";
480 }
481
482 sub endindex {
483         my $self = shift;
484         my $IND = $self->{-IND};
485
486         print $IND end_center,end_html,"\n";
487
488         close($IND) if ($IND);
489         undef $self->{-IND};
490 }
491
492 sub startsublist {
493         my $self = shift;
494         my $IND = $self->{-IND};
495
496         print $IND h2("Albums"),"\n",start_table,"\n";
497 }
498
499 sub sub_entry {
500         my $self = shift;
501         my $IND = $self->{-parent}->{-IND};
502         my $name = $self->{-base};
503         my $title = $self->{-title};
504
505         print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
506                         td(a({-href=>$name.'/index.html'},$title))),"\n";
507 }
508
509 sub endsublist {
510         my $self = shift;
511         my $IND = $self->{-IND};
512
513         print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
514 }
515
516 sub startimglist {
517         my $self = shift;
518         my $IND = $self->{-IND};
519         my $first = $self->{-firstimg}->{-base};
520         my $slideref = sprintf(".html/%s-slide.html",$first);
521
522         print $IND h2("Images"),"\n",
523                 a({-href=>$slideref},'Slideshow'),
524                 "\n";
525 }
526
527 sub img_entry {
528         my $self = shift;
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);
537
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",
545                 end_div,"\n",
546                 $self->infotable,
547                 end_div,"\n";
548
549         print $IND table({-class=>'slide'},Tr(td(
550                 a({-href=>".html/$name-info.html",-title=>'Image Info',
551                         -onClick=>"return showIbox('$name');"},$title),
552                 br,
553                 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
554                         img({-src=>$thumb})),
555                 br,
556                 a({-href=>$name,-title=>'Original Image'},"($w x $h)"),
557                 br))),"\n";
558 }
559
560 sub endimglist {
561         my $self = shift;
562         my $IND = $self->{-IND};
563
564         print $IND br({-clear=>'all'}),hr,"\n\n";
565 }
566
567 sub infotable {
568         my $self = shift;
569         my $info = $self->{-info};
570         my $msg='';
571
572         my @infokeys=(
573                 'DateTime',
574                 'ExposureTime',
575                 'FNumber',
576                 'Flash',
577                 'ISOSpeedRatings',
578                 'MeteringMode',
579                 'ExposureProgram',
580                 'FocalLength',
581                 'FileSource',
582                 'Make',
583                 'Model',
584                 'Software',
585         );
586         $msg.=start_table({-class=>'infotable'})."\n";
587         foreach my $k(@infokeys) {
588                 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
589         }
590         $msg.=end_table."\n";
591 }
592