]> www.average.org Git - mkgallery.git/blob - mkgallery.pl
begin work on object design
[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/;
32 use CGI qw/:html *table *center *div/;
33 use Image::Info qw/image_info dim/;
34 use Image::Magick;
35
36 ######################################################################
37
38 FsObj->new(getcwd)->iterate;
39
40 sub new {
41         my $this = shift;
42         my $class;
43         my $self;
44         if (ref($this)) {
45                 $class = ref($this);
46                 my $parent = $this;
47                 my $path = $parent->{-path};
48                 my $name = shift;
49                 $path .= '/' if ($path);
50                 $path .= $name;
51                 my $fullpath = $parent->{-fullpath}.'/'.$name;
52                 $self = {-root=>$parent->{-root}, -path=>$path, -base=>$name,
53                                 -fullpath=>$fullpath};
54         } else {
55                 $class = $this;
56                 my $root=shift;
57                 $self = {-root=>$root, -fullpath=>$root};
58         }
59         bless $self, $class;
60         print "new $class: ($self->{-root}, $self->{-path}, $self->{-base}, $self->{-fullpath})\n";
61         return $self;
62 }
63
64 sub iterate {
65         my $self = shift;
66         my $fullpath .= $self->{-fullpath};
67         print "iterate in dir $fullpath\n";
68
69         my @rdirlist = ();
70         my @rimglist = ();
71         my $D;
72         unless (opendir($D,$fullpath)) {
73                 warn "cannot opendir $fullpath: $!";
74                 return;
75         }
76         while (my $de = readdir($D)) {
77                 next if ($de =~ /^\./);
78                 my $child = $self->new($de);
79                 if ($child->isdir) {
80                         push(@rdirlist,$child);
81                 } elsif ($child->isimg) {
82                         push(@rimglist,$child);
83                 }
84         }
85         closedir($D);
86         my @sdirlist = sort {$a->{-base} cmp $b->{-base}} @rdirlist;
87         undef @rdirlist; # inplace sorting would be handy here
88         my @simglist = sort {$a->{-base} cmp $b->{-base}} @rimglist;
89         undef @rimglist; # optimize away unsorted versions
90
91         foreach my $dir(@sdirlist) {
92                 print "Dir: $dir->{-fullpath}\n";
93                 $dir->iterate;
94         }
95         foreach my $img(@simglist) {
96                 print "Img: $img->{-fullpath}\n";
97         }
98 }
99
100 sub isdir {
101         my $self = shift;
102         return ( -d $self->{-fullpath} );
103 }
104
105 sub isimg {
106         my $self = shift;
107         my $fullpath = $self->{-fullpath};
108         return 0 unless ( -f $fullpath );
109         my $info = image_info($fullpath);
110         if (my $error = $info->{error}) {
111                 if (($error !~ "Unrecognized file format") &&
112                     ($error !~ "Can't read head")) {
113                         warn "File \"$fullpath\": $error\n";
114                 }
115                 return 0;
116         }
117         $self->{-info} = $info;
118         return 1;
119 }
120
121 ######################################################################
122 =cut
123 ######################################################################
124
125 &processdir(getcwd);
126
127 sub processdir {
128         my ($start,$dir)=@_;
129         my $dn=$start;
130         $dn .= "/".$dir if ($dir);
131         unless ( -d $dn ) {
132                 warn "not a directory: $dn";
133                 return;
134         }
135         my $D;
136         unless (opendir($D,$dn)) {
137                 warn "cannot opendir $dn: $!";
138                 return;
139         }
140
141 # recurse into subdirectories BEFORE opening index file
142
143         &iteratedir($D,$start,$dir,sub {
144                 my ($start,$dir,$base)=@_;
145                 my $ndir = $dir;
146                 $ndir .= "/" if ($ndir);
147                 $ndir .= $base;
148                 return unless ( -d $start."/".$ndir );
149                 &processdir($start,$ndir);
150         });
151
152 # fill in title
153
154         my $title=&gettitle($dn,$dir);
155
156 # get include prefix
157
158         my $inc=&getinclude($dn);
159
160 # generate directory index unless suppressed
161
162         if ( -e $dn."/.noindex" ) {
163                 open(STDOUT,">/dev/null");
164         } else {
165                 open(STDOUT,">".$dn."/index.html");
166         }
167
168 # write HTML header
169
170         print start_html(-title => $title,
171                         -style=>{-src=>[$inc."gallery.css",
172                                         $inc."lightbox.css"]},
173                         -script=>[{-code=>"var incPrefix='$inc';"},
174                                 {-src=>$inc."gallery.js"},
175                                 {-src=>$inc."lightbox.js"}]),"\n";
176         print a({-href=>"../index.html"},"UP");
177         print start_center,"\n";
178         print h1($title),"\n";
179
180 # create list of sub-albums
181
182         my $hassubdirs=0;
183         &iteratedir($D,$start,$dir,sub {
184                 my ($start,$dir,$base)=@_;
185                 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
186                 return unless ( -d $en );
187                 unless ($hassubdirs) {
188                         print hr,h2("Albums"),start_table,"\n";
189                         $hassubdirs=1;
190                 }
191                 &subalbum($base,&gettitle($en,$dir."/".$base));
192         });
193         print end_table,hr,"\n" if ($hassubdirs);
194
195 # create picture gallery
196
197         my @piclist=();
198         my @infolist=();
199
200         my $haspics=0;
201         &iteratedir($D,$start,$dir,sub {
202                 my ($start,$dir,$base)=@_;
203                 my $en=sprintf("%s/%s/%s",$start,$dir,$base);
204                 return unless ( -f $en );
205                 my $info = image_info($en);
206                 if (my $error = $info->{error}) {
207                         if (($error !~ "Unrecognized file format") &&
208                             ($error !~ "Can't read head")) {
209                                 print STDERR "File \"$en\": $error\n";
210                         }
211                         return;
212                 }
213                 if (&processfile($start,$dir,$base,$en,$info)) {
214                         $haspics=1;
215                         push(@piclist,$base);
216                         push(@infolist,$info);
217                 }
218         });
219
220 # write HTML footer
221
222         print br({-clear=>"all"}),"\n";
223         print a({-href=>".html/".$piclist[0]."-slide.html"},"Slideshow");
224         print hr,"\n" if ($haspics);
225         print end_center,"\n";
226         print end_html,"\n";
227
228         close(STDOUT);
229         closedir($D);
230
231 # generate html files for slideshow from @piclist
232
233         for (my $i=0;$i<=$#piclist;$i++) {
234                 my $base=$piclist[$i];
235                 my $pbase;
236                 my $nbase;
237                 $pbase=$piclist[$i-1] if ($i>0);
238                 $nbase=$piclist[$i+1] if ($i<$#piclist);
239                 for my $refresh('static','slide') {
240                         &mkauxfile($start,$dir,$pbase,$base,$nbase,
241                                         $refresh,$infolist[$i]);
242                 }
243         }
244
245 }
246
247 #############################################################
248 # helper functions
249 #############################################################
250
251 sub iteratedir {
252         my ($D,$start,$dir,$prog)=@_;
253         my @list=();
254         while (my $de=readdir($D)) {
255                 next if ($de =~ /^\./);
256                 push(@list,$de);
257         }
258         foreach my $de(sort @list) {
259                 &$prog($start,$dir,$de);
260         }
261         rewinddir($D);
262 }
263
264 sub getinclude {
265         my ($dn)=@_;
266
267         my $depth=20;
268         my $str="";
269         #print STDERR "start include ",$dn."/".$str.".include","\n";
270         while ( ! -d $dn."/".$str.".include" ) {
271                 #print STDERR "not include ",$dn."/".$str.".include","\n";
272                 $str.="../";
273                 last unless ($depth--);
274         }
275         #print STDERR "end include ",$dn."/".$str.".include","\n";
276         if ( -d $dn."/".$str.".include" ) {
277                 #print STDERR "return include ".$str.".include/".$fn,"\n";
278                 return $str.".include/";
279         } else {
280                 return ""; # won't work anyway but return something
281         }
282 }
283
284 sub gettitle {
285         my ($dir,$dflt)=@_;
286
287         my $F;
288         my $str;
289         if (open($F,"<".$dir."/.title")) {
290                 $str=<$F>;
291                 chop $str;
292                 close($F);
293         } else {
294                 print STDERR "enter title for $dir\n";
295                 $str=<>;
296                 if ($str =~ /^\s*$/) {
297                         $str=$dflt;
298                 }
299                 if (open($F,">".$dir."/.title")) {
300                         print $F $str,"\n";
301                         close($F);
302                 } else {
303                         print STDERR "cant open .title in $dir for writing: $!";
304                 }
305         }
306         return $str;
307 }
308
309 sub subalbum {
310         my ($base,$title)=@_;
311
312         print Tr({-bgcolor=>"#c0c0c0"},
313                 td(a({-href=>$base."/index.html"},$base)),
314                 td(a({-href=>$base."/index.html"},$title))),"\n";
315 }
316
317 sub processfile {
318         my ($start,$dir,$base,$fn,$info)=@_;
319
320         my ($w,$h) = dim($info);
321         my $title=$info->{'Comment'};
322         $title=$base unless ($title);
323         my $thumb=&scale($start,$dir,$base,$fn,160,$info);
324         my $medium=&scale($start,$dir,$base,$fn,640,$info);
325         print &infobox($info,$base,$fn),"\n";
326         print table({-class=>'slide'},Tr(td(
327                 a({-href=>".html/$base-info.html",
328                         -onClick=>"return showIbox('$base');"},$title),
329                 br,
330                 a({-href=>$medium,-rel=>"lightbox",-title=>$title},
331                         img({-src=>$thumb})),
332                 br,
333                 a({-href=>$base},"($w x $h)"),
334                 br))),"\n";
335         return 1;
336 }
337
338 sub infobox {
339         my ($info,$base,$fn)=@_;
340
341         my @infokeys=(
342                 'DateTime',
343                 'ExposureTime',
344                 'FNumber',
345                 'Flash',
346                 'ISOSpeedRatings',
347                 'MeteringMode',
348                 'ExposureProgram',
349                 'FocalLength',
350                 'FileSource',
351                 'Make',
352                 'Model',
353                 'Software',
354         );
355
356         my $msg=start_div({-class=>'ibox',-id=>$base,-OnClick=>"HideIbox('$base');"});
357         $msg.=span({-style=>'float: left;'},"Info for $base").
358                 span({-style=>'float: right;'},
359                         a({-href=>"#",-OnClick=>"HideIbox('$base');"},"Close"));
360         $msg.=br({-clear=>'all'});
361         $msg.=start_table;
362         foreach my $k(@infokeys) {
363                 $msg.=Tr(td($k.":"),td($info->{$k}));
364         }
365         $msg.=end_table;
366         $msg.=end_div;
367         return $msg;
368 }
369
370 sub mkauxfile {
371         my ($start,$dir,$pbase,$base,$nbase,$refresh,$info) =@_;
372         my $en=sprintf("%s/%s/.html/%s-%s.html",$start,$dir,$base,$refresh);
373         my $pref;
374         my $nref;
375         if ($pbase) {
376                 $pref=sprintf("%s-%s.html",$pbase,$refresh);
377         } else {
378                 $pref="../index.html";
379         }
380         if ($nbase) {
381                 $nref=sprintf("%s-%s.html",$nbase,$refresh);
382         } else {
383                 $nref="../index.html";
384         }
385         my $toggle;
386         my $toggleref;
387         if ($refresh eq 'slide') {
388                 $toggle='Stop!';
389                 $toggleref=sprintf("%s-static.html",$base);
390         } else {
391                 $toggle='Play-&gt;';
392                 $toggleref=sprintf("%s-slide.html",$base);
393         }
394
395         my $tdir=sprintf "%s/%s/.html",$start,$dir;
396         mkdir($tdir,0755) unless ( -d $tdir );
397
398         unless (open(STDOUT,">".$en)) {
399                 warn "cannot open $en: $!";
400                 return;
401         }
402         my $title=$info->{'Comment'};
403         $title=$base unless ($title);
404         if ($refresh eq 'slide') {
405                 print start_html(-title=>$title,
406                                 -bgcolor=>"#808080",
407                         -head=>meta({-http_equiv=>'Refresh',
408                                 -content=>"3; url=$nref"})),"\n";
409         } else {
410                 print start_html(-title=>$title,
411                                 -bgcolor=>"#808080"),"\n";
412         }
413         print start_center,"\n";
414         print h1($title);
415         print a({-href=>"../index.html"},"Index")," | ";
416         print a({-href=>$pref},"&lt;&lt;Prev")," | ";
417         print a({-href=>$toggleref},$toggle)," | ";
418         print a({-href=>$nref},"Next&gt;&gt;");
419         print p;
420         print img({-src=>"../.640/".$base}),"\n";
421         print end_center,"\n";
422         print end_html,"\n";
423         close(STDOUT);
424 }
425
426 sub scale {
427         my ($start,$dir,$base,$fn,$tsize,$info)=@_;
428         my ($w,$h) = dim($info);
429         my $max=($w>$h)?$w:$h;
430         my $factor=$tsize/$max;
431
432         return $base if ($factor >= 1);
433
434         my $tdir=sprintf "%s/%s/.%s",$start,$dir,$tsize;
435         mkdir($tdir,0755) unless ( -d $tdir );
436         my $tbase=sprintf ".%s/%s",$tsize,$base;
437         my $tfn=sprintf "%s/%s",$tdir,$base;
438         my @sstat=stat($fn);
439         my @tstat=stat($tfn);
440         return $tbase if (@tstat && ($sstat[9] < $tstat[9])); # [9] -> mtime
441
442         print STDERR "scale by $factor from $fn to $tfn\n";
443         &doscaling($fn,$tfn,$factor,$w,$h);
444         return $tbase;
445 }
446
447 sub doscaling {
448         my ($src,$dest,$factor,$w,$h)=@_;
449
450         my $im=new Image::Magick;
451         my $err;
452         #print STDERR "doscale $src -> $dest by $factor\n";
453         $err=$im->Read($src);
454         unless ($err) {
455                 $im->Scale(width=>$w*$factor,height=>$h*$factor);
456                 $err=$im->Write($dest);
457                 warn "ImageMagic: write \"$dest\": $err" if ($err);
458         } else {
459                 warn "ImageMagic: read \"$src\": $err";
460                 system("djpeg \"$src\" | pnmscale \"$factor\" | cjpeg >\"$dest\"");
461         }
462         undef $im;
463 }