summaryrefslogtreecommitdiffstats
path: root/generate_gallery.pl
blob: 931581d2d00a3ec74ef0dd81c52b6b3e423fcac1 (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
#!/usr/bin/perl
use warnings;
use strict;
use Image::ExifTool qw(ImageInfo);
use File::Basename;
use Cwd qw(cwd abs_path);
use File::MimeInfo qw(mimetype);
use Image::Magick;

if (@ARGV == 0) {
	print "usage: ".basename($0)." <directory>...\n";
	exit 0;
}

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

my $startdir = cwd();

for my $dir (@ARGV) {
	chdir $startdir;
	unless (-d $dir) {
		print "Argument is not a directory: $dir. Ignoring.\n";
		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";
			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";
		my $tags = ImageInfo($file, "Aperture", "ShutterSpeed", "FocalLength");
		$html .= "<a rel=\"images\" class=\"thumbnail\" title=\"".$tags->{ShutterSpeed}."s, ".$tags->{FocalLength}." @ F ".$tags->{Aperture}."\" 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", ".";
}