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