]> git.defcon.no Git - plsgen/blob - plsgen
Three new features, three bugfixes. Version 0.4 code.
[plsgen] / plsgen
1 #!/usr/bin/perl
2 use Image::ExifTool;
3 use POSIX;
4 use AppConfig;
5 use Getopt::Long;
6 use threads;
7 use threads::shared;
8 use strict;
9
10 # DONE: Removed some default values in config, allowing empty options
11 # TODO: Stripping of image suffix for HTML file w/config option?
12 # TODO: Templating of EXIF
13 # TODO: Priority/sorting of EXIF tags
14 # TODO: Possibility for hide/show EXIF
15 # TODO: Clear old generated files and meta on regen
16 # TODO: Use perlmagick et. al instead of convert/jhead..
17 # BUG: The naive handling of filenames breaks on special characters
18
19 my $version = "0.3";
20
21 # Runtime data
22 my $title = undef;
23 my $htmlonly = 0;
24 my $configfile = undef;
25 my $halp = undef;
26 my $album_comment = undef;
27 my $doexif = 1;
28 my $threadcounter = 0;
29 share ( $threadcounter );
30
31 GetOptions (
32 "title=s" => \$title,
33 "htmlonly!" => \$htmlonly,
34 "exif!" => \$doexif,
35 "config=s" => \$configfile,
36 "help" => \$halp
37 );
38 if ( $halp )
39 {
40 print "\nplsgen version " . $version . "\n";
41 print "Copyright Jon Langseth, BSD lisence\n\n";
42 print " --title='Your album title'\n";
43 print " Sets the album title. Title will be stored in .title\n";
44 print " If no title is given, it will be read from .title, if present\n";
45 print " --htmlonly\n";
46 print " Add this option to only generate HTML files\n";
47 print " No image operations will be performed with this option\n";
48 print " --noexif\n";
49 print " Forces EXIF data block not to be written to HTML output\n";
50 print " --config=/path/to/config\n";
51 print " Overrides default config file location.\n";
52 print " Default is to look for ./plsgen.cfg, then ../plsgen.cfg\n";
53 print " and finally /etc/plsgen.cfg.\n";
54 print "\n";
55 exit;
56 }
57
58 # Configuration data
59 my $config = get_config( $configfile );
60 my $full_tpl_file = $config->full_tpl_file;
61 my $index_tpl_file = $config->index_tpl_file;
62 my $css_file = $config->css_file;
63 my $navigation_script = $config->navigation_script;
64 my $up_arrow_navigate = $config->up_arrow_navigate;
65 my $disable_rescale = $config->disable_rescale;
66 my $columns = $config->columns;
67 my $rows = $config->rows;
68 my $thumb_pre = $config->thumb_pre;
69 my $thumb_post = $config->thumb_post;
70 my $thumb_maxwidth = $config->thumb_maxwidth;
71 my $thumb_maxheight = $config->thumb_maxheight;
72 my $view_maxwidth = $config->view_maxwidth;
73 my $view_maxheight = $config->view_maxheight;
74 my $comment_pre = $config->comment_pre;
75 my $comment_post = $config->comment_post;
76 my $idx_prev_text = $config->idx_prev_text;
77 my $idx_next_text = $config->idx_next_text;
78 my $idx_ret_text = $config->idx_ret_text;
79 my $footer_tag = $config->footer_tag;
80 my $concurrent = $config->concurrent_threads;
81
82 # Get or save the title, depending..
83 if ( (not $title) && ( -f ".title" ) )
84 {
85 open TF, "<.title";
86 $title = <TF>;
87 chomp($title);
88 close TF;
89 }
90 elsif ( $title )
91 {
92 open TF, ">.title";
93 print TF $title . "\n";
94 close TF;
95 }
96
97 # Get the album comment, if available
98 if ( -f "comment.txt" )
99 {
100 open CF, "<comment.txt";
101 while (<CF>) { $album_comment .= $_; }
102 close CF;
103 }
104
105 mkdir "thumb";
106 mkdir "view";
107
108 # Glob file names to an array
109 my @images = glob("*png *.jpg *.JPG *.gif *.GIF");
110 # Keep count of total number of images
111 my $imagecount = $#images+1;
112
113 my ($current, $previous, $next);
114 my $indexcount = 1;
115 my $indexfile;
116
117 my $main_meta = "<link rel='stylesheet' href='" . $css_file . "' type='text/css' />";
118 my $navigation_header = "<script type='text/javascript' src='" . $navigation_script . "'></script>";
119
120 my $thumbs = "<div class='thumbnails'>";
121
122 my $indexes = ceil( $imagecount/($rows*$columns) );
123 my $gentime = localtime;
124
125 # In groups of N ( X rows x Y colums of index):
126 # - Step through image array:
127 for ( my $i = 1; $i <= $imagecount; $i++)
128 {
129 $current = $images[$i-1];
130 # - - Previous image is previous location in array, unless cur=first
131 $previous = $images[$i-2] if $i > 1;
132 $previous = undef if $i == 1;
133 # - - Next image is next location in array, unless cur=last.
134 $next = $images[$i] if $i < ( $imagecount);
135 $next = undef if $i >= ( $imagecount);
136
137 #Set up text-strings for template replacement
138 if ( $indexcount == 1 ) { $indexfile = "index.html"; }
139 else { $indexfile = "index" . $indexcount . ".html"; }
140 my $navscript = gen_navscript( $previous, $next, $indexfile );
141 my $position = $i . " of " . $imagecount;
142 my $prev_text = "<a href='" . $previous . ".html'><img src='thumb/" . $previous . "' alt='Previous image' /></a>";
143 my $next_text = "<a href='" . $next . ".html'><img src='thumb/" . $next . "' alt='Next image' /></a>";
144 my $cur_index_text = "<a href='". $indexfile ."'>" . $idx_ret_text . "</a>";
145 my $current_display = $current;
146 $current_display = "view/" . $current unless $disable_rescale;
147
148 # Check for, and load comment from FILENAME.txt here..
149 my $comment = undef;
150 if ( -f $current . ".txt" )
151 {
152 open CF, "<" . $current . ".txt";
153 while (<CF>) { $comment .= $_; }
154 close CF;
155 }
156 if ( $comment )
157 {
158 $comment = $comment_pre . $comment . $comment_post;
159 }
160
161 printf ("Processing image %s: %s\n", $position, $current);
162
163 # - - If rotated according to EXIF, do rotation
164 my $exif = get_exifdata( $current );
165 if ( $exif->{'Orientation'} =~ m/rotate/i )
166 {
167 system("jhead -autorot " . $current . ">/dev/null") unless $htmlonly;
168 }
169 my $exif_text = get_exifblock($exif) if $doexif;
170
171 ## - - Create thumbnail and normal view images, support threading
172 if ( not $htmlonly )
173 {
174 $threadcounter++;
175 my $thread = threads->create (
176 \&scale_image_t,
177 $current, $thumb_maxwidth, $thumb_maxheight,
178 $view_maxwidth, $view_maxheight, $disable_rescale);
179
180 if ( $threadcounter >= $concurrent )
181 {
182 foreach my $thr ( threads->list() ) { $thr->join(); }
183 }
184 }
185 # - - Save a reference to the "primary image"
186 if ( not -f ".indeximage" )
187 {
188 open IM, ">.indeximage";
189 print IM $current;
190 close IM;
191 }
192 # - - Create full view HTML file
193 my $cur_html;
194 open TEMPLATE, "<" . $full_tpl_file or die "UNABLE TO LOAD TEMPLATE $full_tpl_file\n";
195 while (<TEMPLATE>)
196 {
197 if ( $previous ) { $_ =~ s/%\{previous\}/$prev_text/; }
198 else { $_ =~ s/%\{previous\}//; }
199 if ( $next ) { $_ =~ s/%\{next\}/$next_text/; }
200 else { $_ =~ s/%\{next\}//; }
201 $_ =~ s/%\{index\}/$cur_index_text/;
202 $_ =~ s/%\{title\}/$title/;
203 $_ =~ s/%\{main_meta\}/$main_meta/;
204 $_ =~ s/%\{navigation_script\}/$navigation_header/;
205 $_ =~ s/%\{position\}/$position/;
206 $_ =~ s/%\{current\}/$current/;
207 $_ =~ s/%\{current_display\}/$current_display/;
208 $_ =~ s/%\{comment\}/$comment/;
209 $_ =~ s/%\{exif\}/$exif_text/;
210 $_ =~ s/%\{gallery_timestamp\}/$gentime/;
211 $_ =~ s/%\{navscript\}/$navscript/;
212 $_ =~ s/%\{footer_tag\}/$footer_tag/;
213 $cur_html .= $_;
214 }
215 close TEMPLATE;
216 open HTML, ">" . $current . ".html" or die "UNABLE TO WRITE\n";
217 print HTML $cur_html;
218 close HTML;
219
220 # - - Append image thumbnail code to current index content
221 $thumbs .= $thumb_pre . "<a href='" . $current . ".html'><img src='thumb/" . $current . "' alt='View " . $current . "' /></a>" . $thumb_post;
222 if ( $i % ($rows*$columns) == 0 )
223 {
224 $thumbs .= "</div>";
225 # - - On each Y, terminate index file/group:
226 make_index( $index_tpl_file, $indexcount, $indexes, $thumbs, $album_comment );
227 $thumbs = "<div class='thumbnails'>";
228 $indexcount++;
229 }
230 elsif ( $i % ($columns) == 0 )
231 {
232 # - - On each X, terminate index content row
233 $thumbs .= "\n</div>\n<div class='thumbnails'>";
234 }
235 }
236 $thumbs .= "</div>";
237 make_index( $index_tpl_file, $indexcount, $indexes, $thumbs, $album_comment );
238
239 printf ("Waiting for %d threads to finish\n", $threadcounter) if $threadcounter;
240 foreach my $thr ( threads->list() ) { $thr->join(); }
241 # Done.
242
243
244 # -------------- Functions supporting above code -----------------------
245 sub make_index
246 {
247 my $tpl = shift;
248 my $idxcount = shift;
249 my $lastidx = shift;
250 my $thumbs = shift;
251 my $comment = shift;
252
253 my $gentime = localtime;
254 my $html;
255
256 my $prev_file;
257 my $prev_text;
258 my $next_file;
259 my $next_text;
260 my $indexfile;
261
262 # If index counter is 1, filename is index.html
263 $indexfile = ( $indexcount ==1 ) ? "index.html" : "index" . $idxcount . ".html";
264
265 # If index counter is > 1, add previous index link.
266 if ( $idxcount > 1 )
267 {
268 $prev_file = "index" . ($idxcount-1) . ".html";
269 $prev_file = "index.html" if ( $idxcount == 2 );
270 $prev_text = "<a href='$prev_file'>" . $idx_prev_text . "</a>";
271 }
272
273 # If current image is last in array, do not add nex-index link
274 if ( $idxcount < $lastidx )
275 {
276 $next_file = "index" . ($idxcount+1) . ".html";
277 $next_text = "<a href='" . $next_file . "'>" . $idx_next_text . "</a>";
278 }
279
280 if ( $comment )
281 {
282 $comment = $comment_pre . $comment . $comment_post;
283 }
284
285 my $position = $indexcount . " of " . $lastidx;
286 my $navscript = gen_navscript( $prev_file, $next_file );
287
288 open TEMPLATE, "<" . $tpl or die "UNABLE TO LOAD TEMPLATE\n";
289 while (<TEMPLATE>)
290 {
291 $_ =~ s/%\{previous\}/$prev_text/;
292 $_ =~ s/%\{next\}/$next_text/;
293 $_ =~ s/%\{title\}/$title/;
294 $_ =~ s/%\{comment\}/$comment/;
295 $_ =~ s/%\{position\}/$position/;
296 $_ =~ s/%\{main_meta\}/$main_meta/;
297 $_ =~ s/%\{navigation_script\}/$navigation_header/;
298 $_ =~ s/%\{thumbnails\}/$thumbs/;
299 $_ =~ s/%\{gallery_timestamp\}/$gentime/;
300 $_ =~ s/%\{navscript\}/$navscript/;
301 $_ =~ s/%\{footer_tag\}/$footer_tag/;
302 $html .= $_;
303 }
304 close TEMPLATE;
305 open HTML, ">" . $indexfile or die "UNABLE TO WRITE\n";
306 print HTML $html;
307 close HTML;
308
309 }
310
311 sub get_exifdata ($)
312 {
313 my $image = shift;
314 my $exifTool = new Image::ExifTool;
315 $exifTool->Options(Unknown => 1);
316
317 my $exif;
318 my $info = $exifTool->ImageInfo($image);
319 $exif->{'Make'} = $info->{'Make'};
320 $exif->{'Model'} = $info->{'Model'};
321 $exif->{'Orientation'} = $info->{'Orientation'};
322 $exif->{'ExposureTime'} = $info->{'ExposureTime'};
323 $exif->{'FNumber'} = $info->{'FNumber'};
324 $exif->{'ISO'} = $info->{'ISO'};
325 $exif->{'CreateDate'} = $info->{'CreateDate'};
326 $exif->{'ExposureCompensation'} = $info->{'ExposureCompensation'};
327 $exif->{'Flash'} = $info->{'Flash'};
328 $exif->{'FocalLength'} = $info->{'FocalLength'};
329 $exif->{'ExposureMode'} = $info->{'ExposureMode'};
330 $exif->{'Macro'} = $info->{'Macro'};
331 $exif->{'LensType'} = $info->{'LensType'};
332 return $exif;
333 }
334
335 sub get_exifblock
336 {
337 my $exif = shift;
338 my $exifTool = new Image::ExifTool;
339 my $block = "<table id='exifdata' cellspacing='0'>\n";
340 $block .= "<tr class='exifhead'><td>EXIF Parameter</td><td>Value</td></tr>\n";
341 my $tagcount = 0;
342 my $flipflop = 0;
343 foreach my $tag ( keys %$exif )
344 {
345 next if $tag =~ m/Orientation/;
346 my $val = $exif->{$tag};
347 next unless $val;
348 $block .= "<tr class='exiflight'>" if $flipflop;
349 $block .= "<tr class='exifdark'>" if not $flipflop;
350 $block .= "\t<td>";
351 $block .= $exifTool->GetDescription($tag);
352 $block .= "</td>\n";
353 $block .= "\t<td>";
354 $block .= $val;
355 $block .= "</td>\n";
356 $block .= "</tr>\n";
357 $flipflop = not $flipflop;
358 $tagcount++;
359 }
360 $block .= "</table>";
361 $block = undef if not $tagcount;
362 return $block;
363 }
364
365 sub gen_navscript
366 {
367 my $prev = shift;
368 my $next = shift;
369 my $index = shift;
370
371 $prev =~ s/\.html//;
372 $next =~ s/\.html//;
373
374 my $scriptbuffer = "<script type='text/javascript'>\n";
375 $scriptbuffer .= "\tnav_reg_prev('" . $prev . "');\n" if $prev;
376 $scriptbuffer .= "\tnav_reg_next('" . $next . "');\n" if $next;
377 if ( $up_arrow_navigate == 1 )
378 {
379 $scriptbuffer .= "\tnav_reg_index('" . $index . "');\n" if $index;
380 }
381 $scriptbuffer .= "\tnav_reg_onkeypress();\n";
382 $scriptbuffer .= "</script>\n";
383 return $scriptbuffer;
384 }
385
386 sub scale_image_t
387 {
388 my $current = shift;
389 my $thumb_maxwidth = shift;
390 my $thumb_maxheight = shift;
391 my $view_maxwidth = shift;
392 my $view_maxheight = shift;
393 my $disable_rescale = shift;
394
395 my $tgeom = $thumb_maxwidth . "x" . $thumb_maxheight;
396 my $vgeom = $view_maxwidth . "x" . $view_maxheight;
397 # - - Create thumbnail image (resize to new image)
398 system("convert " . $current . " -geometry '" . $tgeom . ">' thumb/" . $current);
399 # - - Create normal display image (resize to new image)
400 system("convert " . $current . " -geometry '" . $vgeom . ">' view/" . $current) unless $disable_rescale;
401 $threadcounter--;
402
403 }
404
405 sub get_config
406 {
407 # My standard way of implementing AppConfig use ...
408 my $filename = shift;
409 if ( not $filename )
410 {
411 my @files = ( "./plsgen.cfg", "../plsgen.cfg", "/etc/plsgen.cfg" );
412 foreach my $file ( @files ) {
413 $filename = $file;
414 last if ( -f $filename );
415 }
416 }
417
418 my $cfg = AppConfig->new(
419 {
420 CASE => 1,
421 ERROR => \&cfg_error,
422 GLOBAL => {
423 DEFAULT => "<undef>",
424 ARGCOUNT => AppConfig::ARGCOUNT_ONE
425 },
426 }
427
428 );
429 $cfg->define('full_tpl_file');
430 $cfg->full_tpl_file("/usr/local/share/plsgen/full.tpl");
431 $cfg->define('index_tpl_file');
432 $cfg->index_tpl_file("/usr/local/share/plsgen/index.tpl");
433
434 $cfg->define('css_file');
435 $cfg->css_file("../style.css");
436 $cfg->define('navigation_script');
437 $cfg->navigation_script("../nav.js");
438
439 $cfg->define('up_arrow_navigate');
440 $cfg->up_arrow_navigate(1);
441
442 $cfg->define('columns');
443 $cfg->columns(4);
444 $cfg->define('rows');
445 $cfg->rows(3);
446
447 $cfg->define('concurrent_threads');
448 $cfg->concurrent_threads(1);
449
450 $cfg->define('thumb_maxwidth');
451 $cfg->thumb_maxwidth(160);
452 $cfg->define('thumb_maxheight');
453 $cfg->thumb_maxheight(120);
454 $cfg->define('view_maxwidth');
455 $cfg->view_maxwidth(800);
456 $cfg->define('view_maxheight');
457 $cfg->view_maxwidth(600);
458 $cfg->define('disable_rescale');
459 $cfg->disable_rescale(0);
460
461 $cfg->define('thumb_pre');
462 $cfg->define('thumb_post');
463
464 $cfg->define('comment_pre');
465 $cfg->define('comment_post');
466
467 $cfg->define('idx_prev_text');
468 $cfg->define('idx_next_text');
469 $cfg->define('idx_ret_text');
470
471 $cfg->define('footer_tag');
472 $cfg->footer_tag('plsgen : Perl Simple Gallery Generator ' . $version);
473
474 $cfg->file($filename) if -f $filename;
475 return $cfg;
476 }
477
478 sub cfg_error {
479 die "Error occured in config-handling: $_\n";
480 }
481