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