summaryrefslogtreecommitdiffstats
path: root/generate_gallery.pl
blob: a5f3c8be13a9e52a253710ca31ce331c599e5441 (plain)
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
#!/usr/bin/perl
use warnings;
use strict;
use File::Basename;
use Cwd qw(cwd abs_path);
use File::MimeInfo qw(mimetype);
use Image::Magick;
use Getopt::Long;
use Module::Load;
use Pod::Usage;

my $templatedir = abs_path(dirname($0))."/generate_gallery.d";

my $startdir = cwd();

my %opts = ();
GetOptions(\%opts, "exif|e", "verbose|v", "help|h") or pod2usage(2);
pod2usage(0) if $opts{help};

pod2usage(-verbose => 0) if (@ARGV== 0);

if ($opts{exif}) {
	load Image::ExifTool, "ImageInfo";
};

for my $dir (@ARGV) {
	chdir $startdir;
	unless (-d $dir) {
		print "Argument is not a directory: $dir. Ignoring.\n" if $opts{verbose};
		next;
	}
	my $abs_dir = abs_path $dir;
	chdir $abs_dir;
	mkdir "thumbs" unless -d "thumbs";
	my $html;

	for my $file (glob("*")) {
		unless (mimetype($file) =~ /^image\/.*/) {
			print "ignoring non-image: $dir/$file\n" if $opts{verbose};
			next;
		}
		
		unless ( -e "$abs_dir/thumbs/$file") {
			print "processing $dir/$file ...\n";
			my $img = new Image::Magick;
			$img->Read("$abs_dir/$file");
			$img->Thumbnail(geometry=>"200x200");
			$img->Write("$abs_dir/thumbs/$file");
		}
		$html .= "<div class=\"thumbnail\">\n";
		$html .= "<a rel=\"images\" class=\"thumbnail\" ";
		if ($opts{exif}) {
			my $tags = ImageInfo($file, "Aperture", "ShutterSpeed", "FocalLength");
			$html .= "title=\"".$tags->{ShutterSpeed}."s, ".$tags->{FocalLength}." @ F ".$tags->{Aperture}."\"";
		}
		$html .= "href=\"$file\">\n";
		$html .= "<img class=\"thumbnail\" alt=\"\" src=\"thumbs/$file\" />\n";
		$html .= "</a>\n</div>\n";
	}
	open TEMPLATE, "<", "$templatedir/template.html";
	open OUTPUT, ">", "$abs_dir/index.html";
	while (<TEMPLATE>) {
		if (/%%CONTENT%%/) {
			print OUTPUT $html;
		} else {
			print OUTPUT $_;
		}
	}
	close TEMPLATE;
	close OUTPUT;
	system "cp", "-r", "$templatedir/fancybox", ".";
}
__END__

=head1 NAME

generate_gallery.pl - Generate simple, static HTML gallery

=head1 SYNOPSIS

generate_gallery.pl [options] <directory ...>

 Options:
   --help, -h      short help message
   --verbose, -v   be verbose during operation
   --exif, -e      add exif information of each image to the page

=head1 OPTIONS

=over 8

=item B<--help>, B<-h>

Print a short help message.

=item B<--verbose>, B<-v>

Be verbose.

=item B<--exif>, B<-e>

Add the following exif information to the page:
shutter speed, ISO, focal length, aperture

This is only recomended for pictures taken with digital cameras.

=back

=head1 DESCRIPTION

This program will generate thumbnails for all images in the given directores and
create a static HTML gallery page.

=cut