summaryrefslogtreecommitdiffstats
path: root/generate_gallery.pl
blob: 2ed321524570e8cc0dc6755cd0480987c402dbda (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
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
#!/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 = ();

$opts{template} = "$templatedir/template.html";
$opts{thumbnailsize} = 200;
$opts{basepath} = "";

GetOptions(\%opts, "exif|e", "verbose|v", "help|h", "filenamedesc", "template=s", "thumbnailsize|s=i", "htmlonly", "basepath=s") or pod2usage(2);
pod2usage(0) if $opts{help};

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

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

$opts{template} = abs_path($opts{template});
unless (-r $opts{template}) {
	print STDERR "Error: template file not readable: $!\n";
	exit 1;
}

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("*")) {
		my $description = "";
		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" if $opts{verbose};
			my $img = new Image::Magick;
			$img->Read("$abs_dir/$file");
			$img->Thumbnail(geometry=>$opts{thumbnailsize}."x".$opts{thumbnailsize});
			$img->Write("$abs_dir/thumbs/$file");
		}

		if ($opts{filenamedesc}) {
			my $basename = basename($file);
			$basename =~ s/(.*)\..*/$1/;
			$description = $basename;
			$description .= ": " if ($opts{exif});
		}
		if ($opts{exif}) {
			my $tags = ImageInfo($file, "Aperture", "ShutterSpeed", "FocalLength");
			if ($tags->{ShutterSpeed} && $tags->{FocalLength} && $tags->{Aperture}) {
				$description .= $tags->{ShutterSpeed}."s, ".$tags->{FocalLength}." @ F ".$tags->{Aperture};
			} else {
				print STDERR "Missing at least one needed exif tag for \"$file\". Ignoring.\n";
			}
		}

		$html .= "<div class=\"thumbnail\">\n";
		$html .= "<a rel=\"images\" class=\"thumbnail\" ";
		$html .= "title=\"$description\" " if $description;
		$html .= "href=\"$opts{basepath}$file\">\n";
		$html .= "<img class=\"thumbnail\" alt=\"\" src=\"$opts{basepath}thumbs/$file\" />\n";
		$html .= "</a>\n</div>\n";
	}
	open TEMPLATE, "<", "$opts{template}";
	open OUTPUT, ">", "$abs_dir/index.html" or die "Failed to open output file: $!";
	while (<TEMPLATE>) {
		if (/%%CONTENT%%/) {
			print OUTPUT $html;
		} else {
			print OUTPUT $_;
		}
	}
	close TEMPLATE;
	close OUTPUT;
	unless ($opts{htmlonly}) {
		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
   --filenamedesc              add the filename to the description
   --template <file>           change template file to use
   --htmlonly                  don't copy the fancybox directory
   --thumbnailsize, -s <size>  change size of the thumbnails in pixels
                                 (default: 200)
   --basepath <string>         Prepend this path to the URLs in the output HTML

=head1 DESCRIPTION

This program will generate thumbnails for all images in the given directores and
create a static HTML gallery 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 image description:
shutter speed, ISO, focal length, aperture

This is only recomended for pictures taken with digital cameras.

=item B<--filenamedesc>

Add the filename to the image description. The extension will be stripped.

=item B<--template> <file>

Change the template file to use. The file should contain the string "%%CONTENT%%".

=item B<--htmlonly>

Only generate thumbnails and the html file. This disables copying of fancybox.

=item B<--thumbnailsizes>, B<-s> <size>

Change the size of the generated thumbnails in pixels. Default: 200

=item B<--basepath> <string>

Prepend this path to the URLs in the output HTML. The path should probably end with a /.

=back

=cut