1a3688439edf8acf58a7d031b90c9bf85b4f9974
[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 use Encode;
37 use encoding 'utf-8';
38 binmode(STDOUT, ":utf8");
39
40 my $haveimagick = eval { require Image::Magick; };
41 { package Image::Magick; }      # to make perl compiler happy
42
43 my @sizes = (160, 640);
44
45 ######################################################################
46
47 my $incpath;
48 my $debug = 0;
49 my $asktitle = 0;
50 my $noasktitle = 0;
51
52 charset("utf-8");
53
54 GetOptions(     'incpath'=>\$incpath,
55                 'asktitle'=>\$asktitle,
56                 'noasktitle'=>\$noasktitle,
57                 'debug'=>\$debug);
58
59 my $term = new Term::ReadLine "Edit Title";
60
61 FsObj->new(getcwd)->iterate;
62
63 sub new {
64         my $this = shift;
65         my $class;
66         my $self;
67         if (ref($this)) {
68                 $class = ref($this);
69                 my $parent = $this;
70                 my $name = shift;
71                 my $fullpath = $parent->{-fullpath}.'/'.$name;
72                 $self = {
73                                 -parent=>$parent,
74                                 -root=>$parent->{-root},
75                                 -base=>$name,
76                                 -fullpath=>$fullpath,
77                                 -inc=>'../'.$parent->{-inc},
78                         };
79         } else {
80                 $class = $this;
81                 my $root=shift;
82                 $self = {
83                                 -root=>$root,
84                                 -fullpath=>$root,
85                                 -inc=>getinc($root),
86                         };
87         }
88         bless $self, $class;
89         if ($debug) {
90                 print "new $class:\n";
91                 foreach my $k(keys %$self) {
92                         print "\t$k\t=\t$self->{$k}\n";
93                 }
94         }
95         return $self;
96 }
97
98 sub getinc {
99         my $fullpath=shift;     # this is not a method
100         my $depth=20;           # arbitrary max depth
101
102         if ($incpath) {
103                 return $incpath."/.include";
104         }
105
106         my $inc=".include";
107         while ( ! -d $fullpath."/".$inc ) {
108                 $inc = "../".$inc;
109                 last unless ($depth-- > 0);
110         }
111         if ($depth > 0) {
112                 return $inc.'/';                # prefix with trailing slash
113         } else {
114                 return 'NO-.INCLUDE-IN-PATH/';  # won't work anyway
115         }
116 }
117
118 sub iterate {
119         my $self = shift;
120         my $fullpath .= $self->{-fullpath};
121         print "iterate in dir $fullpath\n" if ($debug);
122
123         my $youngest=0;
124         my @rdirlist;
125         my @rimglist;
126         my $D;
127         unless (opendir($D,$fullpath)) {
128                 warn "cannot opendir $fullpath: $!";
129                 return;
130         }
131         while (my $de = readdir($D)) {
132                 next if ($de =~ /^\./);
133                 my $child = $self->new($de);
134                 my @stat = stat($child->{-fullpath});
135                 $youngest = $stat[9] if ($youngest < $stat[9]);
136                 if ($child->isdir) {
137                         push(@rdirlist,$child);
138                 } elsif ($child->isimg) {
139                         push(@rimglist,$child);
140                 }
141         }
142         closedir($D);
143         my @dirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
144         undef @rdirlist; # inplace sorting would be handy here
145         my @imglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
146         undef @rimglist; # optimize away unsorted versions
147         $self->{-firstimg} = $imglist[0];
148
149         print "Dir: $self->{-fullpath}\n" if ($debug);
150
151 # 1. first of all, fill title for this directory and create hidden subdirs
152
153         $self->initdir;
154
155 # 2. recurse into subdirectories to get their titles filled
156 #    before we start writing out subalbum list
157
158         foreach my $dir(@dirlist) {
159                 $dir->iterate;
160         }
161
162 # 3. iterate through images to build cross-links,
163
164         my $previmg = undef;
165         foreach my $img(@imglist) {
166                 # list-linking must be done before generating
167                 # aux html because aux pages rely on prev/next refs
168                 if ($previmg) {
169                         $previmg->{-nextimg} = $img;
170                         $img->{-previmg} = $previmg;
171                 }
172                 $previmg=$img;
173         }
174
175 # 4. create scaled versions and aux html pages
176
177         foreach my $img(@imglist) {
178                 # scaled versions must be generated before aux html
179                 # and main image index because they both rely on
180                 # refs to scaled images and they may be just original
181                 # images, this is not known before we try scaling.
182                 $img->makescaled;
183                 # finally, make aux html pages
184                 $img->makeaux;
185         }
186
187 # no need to go beyond this point if the directory timestamp did not
188 # change since we built index.html file last time.
189
190         my @istat = stat($self->{-fullpath}.'/index.html');
191         return unless ($youngest > $istat[9]);
192
193 # 5. start building index.html for the directory
194
195         $self->startindex;
196
197 # 6. iterate through subdirectories to build subalbums list
198
199         if (@dirlist) {
200                 $self->startsublist;
201                 foreach my $dir(@dirlist) {
202                         $dir->sub_entry;
203                 }
204                 $self->endsublist;
205         }
206
207 # 7. iterate through images to build thumb list
208
209         if (@imglist) {
210                 $self->startimglist;
211                 foreach my $img(@imglist) {
212                         print "Img: $img->{-fullpath}\n" if ($debug);
213                         $img->img_entry;
214                 }
215                 $self->endimglist;
216         }
217
218 # 8. comlplete building index.html for the directory
219
220         $self->endindex;
221 }
222
223 sub isdir {
224         my $self = shift;
225         return ( -d $self->{-fullpath} );
226 }
227
228 sub isimg {
229         my $self = shift;
230         my $fullpath = $self->{-fullpath};
231         return 0 unless ( -f $fullpath );
232         my $info = image_info($fullpath);
233         if (my $error = $info->{error}) {
234                 if (($error !~ "Unrecognized file format") &&
235                     ($error !~ "Can't read head")) {
236                         warn "File \"$fullpath\": $error\n";
237                 }
238                 return 0;
239         }
240
241         tryapp12($info) unless ($info->{'ExifVersion'});
242
243         $self->{-isimg} = 1;
244         $self->{-info} = $info;
245         return 1;
246 }
247
248 sub tryapp12 {
249         my $info = shift;       # this is not a method
250         my $app12;
251         # dirty hack to take care of Image::Info parser strangeness
252         foreach my $k(keys %$info) {
253                 $app12=substr($k,6).$info->{$k} if ($k =~ /^App12-/);
254         }
255         return unless ($app12); # bad luck
256         my $seenfirstline=0;
257         foreach my $ln(split /[\r\n]+/,$app12) {
258                 $ln =~ s/[[:^print:]\000]/ /g;
259                 unless ($seenfirstline) {
260                         $seenfirstline=1;
261                         $info->{'Make'}=$ln;
262                         next;
263                 }
264                 my ($k,$v)=split /=/,$ln,2;
265                 if ($k eq 'TimeDate') {
266                         $info->{'DateTime'} =
267                                 strftime("%Y:%m:%d %H:%M:%S", localtime($v))
268                                                         unless ($v < 0);
269                 } elsif ($k eq 'Shutter') {
270                         $info->{'ExposureTime'} = '1/'.int(1000000/$v+.5);
271                 } elsif ($k eq 'Flash') {
272                         $info->{'Flash'} = $v?'Flash fired':'Flash did not fire';
273                 } elsif ($k eq 'Type') {
274                         $info->{'Model'} = $v;
275                 } elsif ($k eq 'Version') {
276                         $info->{'Software'} = $v;
277                 } elsif ($k eq 'Fnumber') {
278                         $info->{'FNumber'} = $v;
279                 }
280         }
281 }
282
283 sub initdir {
284         my $self = shift;
285         my $fullpath = $self->{-fullpath};
286         for my $subdir(@sizes, 'html') {
287                 my $tdir=sprintf "%s/.%s",$self->{-fullpath},$subdir;
288                 mkdir($tdir,0755) unless ( -d $tdir );
289         }
290         $self->edittitle;
291 }
292
293 sub edittitle {
294         my $self = shift;
295         my $fullpath = $self->{-fullpath};
296         my $title;
297         my $T;
298         if (open($T,'<'.$fullpath.'/.title')) {
299                 $title = <$T>;
300                 $title =~ s/[\r\n]*$//;
301                 close($T);
302         }
303         if ($asktitle || (!$title && !$noasktitle)) {
304                 my $prompt = $self->{-base};
305                 $prompt = '/' unless ($prompt);
306                 my $OUT = $term->OUT || \*STDOUT;
307                 print $OUT "Enter title for $fullpath\n";
308                 $title = $term->readline($prompt.' >',$title);
309                 $term->addhistory($title) if ($title);
310                 if (open($T,'>'.$fullpath.'/.title')) {
311                         print $T $title,"\n";
312                         close($T);
313                 }
314         }
315         unless ($title) {
316                 $title=substr($fullpath,length($self->{-root}));
317         }
318         $self->{-title}=$title;
319         print "title in $fullpath is $title\n" if ($debug);
320 }
321
322 sub makescaled {
323         my $self = shift;
324         my $fn = $self->{-fullpath};
325         my $name = $self->{-base};
326         my $dn = $self->{-parent}->{-fullpath};
327         my ($w, $h) = dim($self->{-info});
328         my $max = ($w > $h)?$w:$h;
329
330         foreach my $size(@sizes) {
331                 my $nref = '.'.$size.'/'.$name;
332                 my $nfn = $dn.'/'.$nref;
333                 my $factor=$size/$max;
334                 if ($factor >= 1) {
335                         $self->{$size} = $name; # unscaled version will do
336                 } else {
337                         $self->{$size} = $nref;
338                         if (isnewer($fn,$nfn)) {
339                                 doscaling($fn,$nfn,$factor,$w,$h);
340                         }
341                 }
342         }
343 }
344
345 sub isnewer {
346         my ($fn1,$fn2) = @_;                    # this is not a method
347         my @stat1=stat($fn1);
348         my @stat2=stat($fn2);
349         return (!@stat2 || ($stat1[9] > $stat2[9]));
350         # true if $fn2 is absent or is older than $fn1
351 }
352
353 sub doscaling {
354         my ($src,$dest,$factor,$w,$h) = @_;     # this is not a method
355
356         my $err=1;
357         if ($haveimagick) {
358                 my $im = new Image::Magick;
359                 print "doscaling $src -> $dest by $factor\n" if ($debug);
360                 if ($err = $im->Read($src)) {
361                         warn "ImageMagick: read \"$src\": $err";
362                 } else {
363                         $im->Scale(width=>$w*$factor,height=>$h*$factor);
364                         $err=$im->Write($dest);
365                         warn "ImageMagick: write \"$dest\": $err" if ($err);
366                 }
367                 undef $im;
368         }
369         if ($err) {     # fallback to command-line tools
370                 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
371         }
372 }
373
374 sub makeaux {
375         my $self = shift;
376         my $name = $self->{-base};
377         my $dn = $self->{-parent}->{-fullpath};
378         my $pref = $self->{-previmg}->{-base};
379         my $nref = $self->{-nextimg}->{-base};
380         my $inc = $self->{-inc};
381         my $title = $self->{-info}->{'Comment'};
382         $title = $name unless ($title);
383
384         print "slide: \"$title\": \"$pref\"->\"$name\"->\"$nref\"\n" if ($debug);
385
386         # slideshow
387         for my $refresh('static', 'slide') {
388                 my $fn = sprintf("%s/.html/%s-%s.html",$dn,$name,$refresh);
389                 if (isnewer($self->{-fullpath},$fn)) {
390                         my $imgsrc = '../'.$self->{$sizes[1]};
391                         my $fwdref;
392                         my $bakref;
393                         if ($nref) {
394                                 $fwdref = sprintf("%s-%s.html",$nref,$refresh);
395                         } else {
396                                 $fwdref = '../index.html';
397                         }
398                         if ($pref) {
399                                 $bakref = sprintf("%s-%s.html",$pref,$refresh);
400                         } else {
401                                 $bakref = '../index.html';
402                         }
403                         my $toggleref;
404                         my $toggletext;
405                         if ($refresh eq 'slide') {
406                                 $toggleref=sprintf("%s-static.html",$name);
407                                 $toggletext = 'Stop!';
408                         } else {
409                                 $toggleref=sprintf("%s-slide.html",$name);
410                                 $toggletext = 'Play-&gt;';
411                         }
412                         my $F;
413                         unless (open($F,'>'.$fn)) {
414                                 warn "cannot open \"$fn\": $!";
415                                 next;
416                         }
417                         binmode($F, ":utf8");
418                         if ($refresh eq 'slide') {
419                                 print $F start_html(
420                                         -encoding=>"utf-8",
421                                         -title=>$title,
422                                         -bgcolor=>"#808080",
423                                         -head=>meta({-http_equiv=>'Refresh',
424                                                 -content=>"3; url=$fwdref"}),
425                                         -style=>{-src=>$inc."gallery.css"},
426                                         ),"\n";
427                                                 
428                         } else {
429                                 print $F start_html(-title=>$title,
430                                         -encoding=>"utf-8",
431                                         -bgcolor=>"#808080",
432                                         -style=>{-src=>$inc."gallery.css"},
433                                         ),"\n";
434                         }
435                         print $F start_center,"\n",
436                                 h1($title),"\n",
437                                 start_table({-class=>'navi'}),start_Tr,"\n",
438                                 td(a({-href=>"../index.html"},"Index")),"\n",
439                                 td(a({-href=>$bakref},"&lt;&lt;Prev")),"\n",
440                                 td(a({-href=>$toggleref},$toggletext)),"\n",
441                                 td(a({-href=>$fwdref},"Next&gt;&gt;")),"\n",
442                                 end_Tr,
443                                 end_table,"\n",
444                                 table({-class=>'picframe'},
445                                         Tr(td(img({-src=>$imgsrc})))),"\n",
446                                 end_center,"\n",
447                                 end_html,"\n";
448                         close($F);
449                 }
450         }
451
452         # info html
453         my $fn = sprintf("%s/.html/%s-info.html",$dn,$name);
454         if (isnewer($self->{-fullpath},$fn)) {
455                 my $F;
456                 unless (open($F,'>'.$fn)) {
457                         warn "cannot open \"$fn\": $!";
458                         return;
459                 }
460                 my $imgsrc = sprintf("../.%s/%s",$sizes[0],$name);
461                 print $F start_html(-title=>$title,
462                                 -encoding=>"utf-8",
463                                 -style=>{-src=>$inc."gallery.css"},),"\n",
464                         start_center,"\n",
465                         h1($title),"\n",
466                         table({-class=>'ipage'},
467                                 Tr(td(img({-src=>$imgsrc})),
468                                         td($self->infotable))),
469                         a({-href=>'../index.html'},'Index'),"\n",
470                         end_center,"\n",
471                         end_html,"\n";
472                 close($F);
473         }
474 }
475
476 sub startindex {
477         my $self = shift;
478         my $fn = $self->{-fullpath}.'/index.html';
479         my $block = $self->{-fullpath}.'/.noindex';
480         $fn = '/dev/null' if ( -f $block );
481         my $IND;
482         unless (open($IND,'>'.$fn)) {
483                 warn "cannot open $fn: $!";
484                 return;
485         }
486         binmode($IND, ":utf8");
487         $self->{-IND} = $IND;
488
489         my $inc = $self->{-inc};
490         my $title = $self->{-title};
491         print $IND start_html(-title => $title,
492                         -encoding=>"utf-8",
493                         -style=>{-src=>[$inc."gallery.css",
494                                         $inc."lightbox.css"]},
495                         -script=>[{-code=>"var incPrefix='$inc';"},
496                                 {-src=>$inc."gallery.js"},
497                                 {-src=>$inc."lightbox.js"}]),
498                 a({-href=>"../index.html"},"UP"),"\n",
499                 start_center,"\n",
500                 h1($title),"\n",
501                 "\n";
502 }
503
504 sub endindex {
505         my $self = shift;
506         my $IND = $self->{-IND};
507
508         print $IND end_center,end_html,"\n";
509
510         close($IND) if ($IND);
511         undef $self->{-IND};
512 }
513
514 sub startsublist {
515         my $self = shift;
516         my $IND = $self->{-IND};
517
518         print $IND h2("Albums"),"\n",start_table,"\n";
519 }
520
521 sub sub_entry {
522         my $self = shift;
523         my $IND = $self->{-parent}->{-IND};
524         my $name = $self->{-base};
525         my $title = $self->{-title};
526
527         print $IND Tr(td(a({-href=>$name.'/index.html'},$name)),
528                         td(a({-href=>$name.'/index.html'},$title))),"\n";
529 }
530
531 sub endsublist {
532         my $self = shift;
533         my $IND = $self->{-IND};
534
535         print $IND end_table,"\n",br({-clear=>'all'}),hr,"\n\n";
536 }
537
538 sub startimglist {
539         my $self = shift;
540         my $IND = $self->{-IND};
541         my $first = $self->{-firstimg}->{-base};
542         my $slideref = sprintf(".html/%s-slide.html",$first);
543
544         print $IND h2("Images"),"\n",
545                 a({-href=>$slideref},'Slideshow'),
546                 "\n";
547 }
548
549 sub img_entry {
550         my $self = shift;
551         my $IND = $self->{-parent}->{-IND};
552         my $name = $self->{-base};
553         my $title = $self->{-info}->{'Comment'};
554         $title = $name unless ($title);
555         my $thumb = $self->{$sizes[0]};
556         my $medium = $self->{$sizes[1]};
557         my $info = $self->{-info};
558         my ($w, $h) = dim($info);
559
560         print $IND start_div({-class=>'ibox',-id=>$name,
561                                 -OnClick=>"HideIbox('$name');"}),"\n",
562                 start_div({-class=>'iboxtitle'}),
563                 span({-style=>'float: left;'},b("Info for $name")),
564                 span({-style=>'float: right;'},
565                         a({-href=>"#",-OnClick=>"HideIbox('$name');"},"Close")),
566                 br({-clear=>'all'}),"\n",
567                 end_div,"\n",
568                 $self->infotable,
569                 end_div,"\n";
570
571         print $IND table({-class=>'slide'},Tr(td(
572                 a({-href=>".html/$name-info.html",-title=>'Image Info',
573                         -onClick=>"return showIbox('$name');"},$title),
574                 br,
575                 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
576                         img({-src=>$thumb})),
577                 br,
578                 a({-href=>$name,-title=>'Original Image'},"($w x $h)"),
579                 br))),"\n";
580 }
581
582 sub endimglist {
583         my $self = shift;
584         my $IND = $self->{-IND};
585
586         print $IND br({-clear=>'all'}),hr,"\n\n";
587 }
588
589 sub infotable {
590         my $self = shift;
591         my $info = $self->{-info};
592         my $msg='';
593
594         my @infokeys=(
595                 'DateTime',
596                 'ExposureTime',
597                 'FNumber',
598                 'Flash',
599                 'ISOSpeedRatings',
600                 'MeteringMode',
601                 'ExposureProgram',
602                 'FocalLength',
603                 'FileSource',
604                 'Make',
605                 'Model',
606                 'Software',
607         );
608         $msg.=start_table({-class=>'infotable'})."\n";
609         foreach my $k(@infokeys) {
610                 $msg.=Tr(td($k.":"),td($info->{$k}))."\n" if ($info->{$k});
611         }
612         $msg.=end_table."\n";
613 }
614