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

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

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

my $startdir = cwd();

my %opts = ();
GetOptions(\%opts, "exif|e", "verbose|v", "help|h");


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", ".";
}