/
PageGen.pm
531 lines (435 loc) · 15.8 KB
/
PageGen.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package PageGen;
use 5.008;
use strict;
use warnings;
use Carp;
use POSIX;
use File::Basename;
use File::Spec;
use Image::ExifTool;
# All hacks to make PAR/pp work
use Image::ExifTool::Photoshop;
use Image::ExifTool::IPTC;
use Image::ExifTool::PrintIM;
use Image::ExifTool::Pentax;
use Image::ExifTool::Canon;
use Image::ExifTool::FujiFilm;
use Image::ExifTool::Olympus;
my $non_pic_pattern = '(avi|gif|mov|mp4|flv|ogv|mkv|webm)$'; # move up to config when this works....
my $verbose = 1;
require Exporter;
our @ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use PageGen ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
make_pic_page
get_file_caption
print_shooting_details
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
make_pic_page
get_file_caption
print_shooting_details
);
our $VERSION = '1.02';
# Preloaded methods go here.
#
# make_pic_page (file) -
#
# Generates a html page containing the image "file", the exif info from file,
# and next and back links for other pictures.
# TODO
# . Make this a public method type thing. So I can generate a page any time.
sub make_pic_page {
my $file = shift;
my %opt = %{$_[0]};
my $pichtmldir = $opt{"pichtmldir"} || ".";
my $prevfile = $opt{"prevfile"};
my $nextfile = $opt{"nextfile"};
my $style = $opt{"css"};
my $style2 = $opt{"css2"};
# First off, make a webpage that displays the picture, we'll
# HTML::Template this later I think
my $basename;
my $nonPicture = "false";
if ($file =~/$non_pic_pattern/i) {
$basename = basename($file);
$nonPicture = "true";
} else {
$basename = basename($file, ".jpg");
}
my $page = "$basename.html";
$opt{"page"} = $page;
mkdir $pichtmldir unless (-e $pichtmldir);
if ((-e $pichtmldir) and (not -d $pichtmldir)) {
croak "Output dir exists but is not a directory!";
}
my $tmpfn = File::Spec->catfile($pichtmldir, $page);
open PH, ">$tmpfn";
# get the current file handle and save it for later
my $save_fh = select;
select PH;
if ($nonPicture ne "false") {
make_video_page($file, \%opt);
} else {
make_pic_page_inner($file, \%opt);
}
print "\n</body>\n</html>";
close PH;
select $save_fh;
}
sub make_pic_page_inner() {
my $file = shift;
my %opt = %{$_[0]};
my $prevfile = $opt{"prevfile"};
my $nextfile = $opt{"nextfile"};
my $style = $opt{"css"};
my $style2 = $opt{"css2"};
my $page = $opt{"page"};
my $file_source = $opt{"file_source"};
my $comment = $opt{"comment"} || "No comment in file";
my ($pic_basename, $path, $suffix) = fileparse($file);
my $exif = Image::ExifTool::ImageInfo($file);
if (my $error = $exif->{Error}) {
close PH;
croak "Can't parse $file : $error\n";
}
my $width = $exif->{ImageWidth};
my $height = $exif->{ImageHeight};
print <<HERE;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:og="http://ogp.me/ns#"
>
<head>
<link rel='StyleSheet' href='$style' type='text/css'/>
<link rel='StyleSheet' href='$style2' type='text/css'/>
<title>$comment - Large View ($file)</title>
<meta property='og:type' content='article'/>
<meta property='og:title' content='$comment'/>
<meta property='og:description' content='A shared photo from the album: $opt{album_title}'/>
</head>
<body>
HERE
print_nav_links($prevfile, $nextfile);
print "\n<h2>$comment</h2>";
print <<HERE;
<div class="embed-container">
<img src="$pic_basename" alt="$comment"/>
</div>
HERE
print_nav_links($prevfile, $nextfile);
if ($file =~ /pano/) {
print_pano_details($file_source);
} else {
print_shooting_details($file_source);
}
print_share_fb();
print_cc_details($page, $comment, \%opt);
print_scripts();
}
sub make_video_page() {
my $infile = shift;
my %opt = %{$_[0]};
my $pichtmldir = $opt{"pichtmldir"} || ".";
my $prevfile = $opt{"prevfile"};
my $nextfile = $opt{"nextfile"};
my $style = $opt{"css"};
my $style2 = $opt{"css2"};
my $page = $opt{"page"};
print STDOUT "getting filecaption for $infile\n" if $verbose;
my $captionFile;
if ($infile =~ /ogv$/i) {
$captionFile = $infile;
} else {
$captionFile = "$infile.ogv";
}
my $comment = $opt{"comment"} || get_file_caption("$captionFile");
my $file = basename($infile);
my ($mp4file, $ogvfile);
if ($file =~ /ogv$/i) {
print STDOUT "input video is ogv, assuming split pair: $file\n" if $verbose;
$ogvfile = $file;
$file =~ s/ogv$//i; # Strip ogv off to get back to .avi or .mkv...
$mp4file = $ogvfile;
$mp4file =~ s/ogv$/mp4/i;
} else {
$ogvfile = "$file.ogv";
$mp4file = "$file.mp4";
}
print '<!DOCTYPE HTML>';
print "<html>";
print "<head>";
print "<link rel=\"StyleSheet\" href=\"$style\" type=\"text/css\"/>";
print "<link rel=\"StyleSheet\" href=\"$style2\" type=\"text/css\"/>";
print "\n<title>$comment - Large View ($file)</title>\n";
print "</head><body>\n";
print_nav_links($prevfile, $nextfile);
print "\n<h2>$comment</h2>";
# we'll set preload, because, well, they had to click to get to this page!
## FIXME - XXX need to be able to decide whether we are remaking (and so
# already have both videos, and need to change suffix, or add suffix)
print <<HERE;
<video width="1280" height="720" controls="controls" preload>
<source src="$ogvfile" type="video/ogg" />
<source src="$mp4file" type="video/mp4" /><!--[if gt IE 6]>
<object width="1280" height="720" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"><!
[endif]--><!--[if !IE]><!-->
<object width="1280" height="720" type="video/quicktime" data="$mp4file"><!--<![endif]-->
<param name="src" value="$mp4file" />
<param name="autoplay" value="false" />
<param name="showlogo" value="false" />
<p>
<img src="thumbs/TN_$file.jpg" alt="\$comment\"/>
</p>
<p>
<strong>No video playback capabilities detected.</strong>
Try clicking one of these links instead, which might work for you.<br/>
<a href="$mp4file">MPEG4 / H.264 ".mp4" (Internet Explorer/Quicktime)</a> |
<a href="$ogvfile">Ogg Theora & Vorbis ".ogv" (higher res, more open format)</a>
</p>
<!--[if gt IE 6]><!-->
</object><!--<![endif]-->
</video>
HERE
print_video_details($infile);
print_share_fb();
print_cc_details($page, $comment, \%opt);
}
sub print_nav_links() {
my $prevfile = shift;
my $nextfile = shift;
print "<div>\n<ul class=\"pagination\">";
if (defined $prevfile) {
# if there's no .jpg on the end, doesn't remove it :)
$prevfile = basename($prevfile, ".jpg");
print "<li class=\"pagination-prev\"><a href=\"$prevfile.html\" id=\"prevfile\">← Prev</a></li>\n";
}
print "<li class=\"pagination-up\"><a href=\"./\" id=\"upfile\">Up</a></li>\n";
if (defined $nextfile) {
$nextfile = basename($nextfile, ".jpg");
print "<li class=\"pagination-next\"><a href=\"$nextfile.html\" id=\"nextfile\">Next →</a></li>\n";
}
print "</ul>\n</div><div style=\"clear:both;\"></div>\n";
}
# Returns undefined if there's no caption, so this can be a clean utility, rather than imposing external behaviours
sub get_file_caption {
my $file = shift;
# Get IPTC header
my $et = new Image::ExifTool;
my $info = $et->ImageInfo($file);
my $rawCaption =
$et->GetValue("Title", Group=>"XMP") ||
$et->GetValue("Title", Group=>"Matroska") ||
$et->GetValue("Caption-Abstract") ||
$et->GetValue("Description", Group=>"XMP") ||
$et->GetValue("UserComment");
my $printCaption = "";
$printCaption = $rawCaption if (defined $rawCaption);
print STDOUT "PageGen: filecaption for $file is '$printCaption'\n" if $verbose;
return $rawCaption;
}
# file is the relative path back to the current document.
sub print_cc_details() {
my $file = shift;
my $title = shift;
my %opt = %{$_[0]};
my $creativeCommons = $opt{"creativeCommons"};
my $creativeCommonsTitle = $opt{"creativeCommonsTitle"};
my $creativeCommonsEmail = $opt{"creativeCommonsEmail"};
my $creativeCommonsName = $opt{"creativeCommonsName"};
if ($creativeCommons ne "false") {
print "\n\n";
print <<HERE;
<div id="creativeCommons">
<a rel="license" href="https://creativecommons.org/licenses/$creativeCommons/">
<img alt="Creative Commons License" style="border-width:0"
src="https://i.creativecommons.org/l/$creativeCommons/88x31.png" width="88" height="31" />
</a><br />
<em>$title</em>, by $creativeCommonsName
is licensed under a <a rel="license" href="https://creativecommons.org/licenses/$creativeCommons/">$creativeCommonsTitle</a>.
<br />Permissions beyond the scope of this license may be available at <a href="mailto:$creativeCommonsEmail" rel="cc:morePermissions">$creativeCommonsEmail</a>.
</div>
HERE
}
}
sub print_share_fb() {
print <<FB_SHARE;
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.4";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-share-button" data-layout="button"></div>
FB_SHARE
}
sub print_pano_details ($) {
my $file = shift;
my $exif = Image::ExifTool::ImageInfo($file);
if (my $error = $exif->{Error}) {
close PH;
croak "Can't parse $file : $error\n";
}
my $date = $exif->{DateTimeOriginal} || $exif->{DateCreated} || "Bad camera!";
# Start printing it all out
print "<p><br/><em>Panoramic image! scroll sideways!</em></p>\n";
print "<h3>Shooting Details</h3>\n";
printf "<p>Date Taken: %s<br/>\n", $date;
print "This is stitched panorama of multiple images, cut and cropped and possibly edited for effect.";
print " No shooting details would be relevant here.";
}
sub print_video_details ($) {
my $file = shift;
my $filename = basename($file);
my $captionFile;
if ($file =~ /ogv$/i) {
$captionFile = $file;
} else {
$captionFile = "$file.ogv";
}
my $exif = Image::ExifTool::ImageInfo("$captionFile"); # FIXME - should we fall back?
if (my $error = $exif->{Error}) {
close PH;
croak "Can't parse $file : $error\n";
}
my $date = $exif->{CreateDate} || $exif->{CreationTime} || "Bad file info!";
my $filesize = $exif->{FileSize};
print "<h3>File Details</h3>\n";
printf "<p>Date Created: %s<br/>\n", $date;
printf "File size: %s<br/>\n", $filesize;
printf "<p>Direct Download: <a href='$filename.mp4'>MP4</a> or <a href='$filename.ogv'>OGV</a></p>";
}
sub print_scripts() {
print <<HERE;
<script src="/js/keyboard-pagination.min.js"></script>
<script type="text/javascript">
keyboardPagination( '.pagination',
{
prev: '.pagination-prev',
next: '.pagination-next',
up: '.pagination-up'
});
</script>
HERE
}
sub print_shooting_details ($) {
my $file = shift;
# Get exif data, and start processing!
my $exif = Image::ExifTool::ImageInfo($file);
if (my $error = $exif->{Error}) {
close PH;
croak "Can't parse $file : $error\n";
}
my $model = $exif->{Model} || "Unknown Camera";
my $owner = $exif->{OwnerName} ||
$exif->{Artist} ||
$exif->{Creator} || "Unset";
# Handle zooms as well as primes
my $lens_long = $exif->{"LongFocal"};
my $lens_short = $exif->{"ShortFocal"};
my $lens_raw = $exif->{"Lens"};
my $lens;
if (defined $lens_raw) {
# then let's just use it, and we're done....
$lens = $lens_raw;
} else {
unless ((defined $lens_long) and (defined $lens_short)) {
#this is a bit canon specific :) so instead, let's assume it's all zooms
$lens = "Zoom";
} else {
if ($lens_long == $lens_short) {
$lens = "Fixed";
} else {
# So it's a zoom, but we need to check whether it's measured in mm
# or not. some cameras don't measure in mm. God knows why :)
my $units_per_mm = $exif->{"FocalUnits"} || 1;
$lens_short /= $units_per_mm;
$lens_long /= $units_per_mm;
$lens = sprintf "%d-%d mm", floor($lens_short), floor($lens_long);
}
}
}
my $focal = $exif->{"FocalLength"};
if (defined($focal)) {
my $temp = $focal;
$temp =~ s/mm//;
my $scale;
if ($model =~ /REBEL/) {
$scale = 1.6;
} else {
$scale = $exif->{"ScaleFactor35efl"};
}
if (defined($scale)) {
$focal .= sprintf " (35mm equiv %d mm)", floor($temp * $scale);
}
} else {
$focal = "Unknown";
}
my $shutter = $exif->{ShutterSpeed} || "Unknown";
my $aperture = $exif->{Aperture} || "Unknown";
my $iso = $exif->{ISO} || "Unknown";
my $date = $exif->{DateTimeOriginal} || $exif->{DateCreated} || "Bad camera!";
# Start printing it all out
print "<h3>Shooting Details</h3>\n";
print "<p>\n";
# Add a label for art photos, to clearly say that they may not be real
if ($file =~ /art/) {
print "Note: this photo has been edited for artistic effect, exif details are from original source
image<br/>\n";
}
# label and link for stacked photos....
if ($file =~ /stack/) {
print "Note: this photo is a <a href=\"/pics2/stack_details.html\">stack of two images</a>, the exif should not be";
print "considered reliable.<br/>\n";
}
printf "Date Taken: %s<br/>\n", $date;
printf "Exposure: f/%s & %s sec<br/>\n", $aperture, $shutter;
printf "Lens: %s @ %s<br/>\n", $lens, $focal;
printf "Camera: %s (Owner: %s)<br/>\n", $model, $owner;
printf "Iso: %s<br/>\n", $iso;
print "</p>";
#optionally add shooting mode, flash details, metering?
}
1;
__END__
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
PageGen - Perl extension for blah blah blah
=head1 SYNOPSIS
use PageGen;
blah blah blah
=head1 ABSTRACT
This should be the abstract for PageGen.
The abstract is used when making PPD (Perl Package Description) files.
If you don't want an ABSTRACT you should also edit Makefile.PL to
remove the ABSTRACT_FROM option.
=head1 DESCRIPTION
Stub documentation for PageGen, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.
Blah blah blah.
=head2 EXPORT
None by default.
=head1 SEE ALSO
Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.
If you have a mailing list set up for your module, mention it here.
If you have a web site set up for your module, mention it here.
=head1 AUTHOR
Karl Palsson, E<lt>karl@c47.orgE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2003 by Karl Palsson
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut