summaryrefslogtreecommitdiff
path: root/bin/gen-xhtml-thumbnails
blob: c4895afc427094ddd2287a830275d210144375dd (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
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
#!/usr/bin/perl
# Copyright © 2009,2010 by Daniel Friesel <derf@derf.homelinux.org>
# License: WTFPL <http://sam.zoy.org/wtfpl>
use strict;
use warnings;
use Image::Imlib2;
use Getopt::Long;
use constant {
	DEFAULT_FILEMODE => oct(644),
};

my $directory = '.';
my $thumbdir  = "$directory/.thumbs";
my $indexfile = "$directory/index.xhtml";
my $title = '';
my $css_line = '';
my $thumb_max_dim = 150;
my $thumb_quality = 75;
my $thumb_spacing = 1.1;
my $css_source;
my ($dx, $dy);
my @files;
my $number = 0;
local $| = 1;

sub print_progress {
	if (($number % 60) == 0) {
		if ($number) {
			printf(" %4d/%d\n", $number, scalar(@files));
		}
		printf('[%3d%%] ', $number * 100 / @files);
	} elsif (($number % 10) == 0) {
		print ' ';
	}
	return;
}

GetOptions(
	'css=s'     => \$css_source,
	'size=i'    => \$thumb_max_dim,
	'spacing=f' => \$thumb_spacing,
	'title=s'   => \$title,
	'quality=i' => \$thumb_quality,
);

if (@ARGV > 0) {
	$directory = shift;
}

if (defined($css_source)) {
	$css_line = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$css_source\"/>";
}

open(my $index, '>', $indexfile) or die("Cannot open $indexfile for writing: $!");

print $index <<"EOD";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<title>$title</title>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	$css_line
</head>
<body><div>
EOD

if (! -d $thumbdir) {
	mkdir($thumbdir) or die("cannot create $thumbdir: $!");
}

opendir(my $dirhandle, $directory) or die("Cannot open directory $directory: $!");

foreach my $file (readdir($dirhandle)) {
	if ($file =~ / \. ( png | jp e? g ) $ /ix) {
		push(@files, $file);
	}
}

closedir($dirhandle);

print "Generating thumbnails [ - cached | + generated ]\n";

foreach my $file (sort { lc($a) cmp lc($b) } @files) {
	my $image = Image::Imlib2->load($file);
	my $thumb;

	if (defined($css_source)) {
		print {$index} '<div class="imgbox">';
	}
	else {
		printf {$index} (
			'<div style="float: left; width: %dpx; height: %dpx">',
			$thumb_max_dim * $thumb_spacing,
			$thumb_max_dim * $thumb_spacing,
		);
	}

	print {$index} "<a href=\"$file\"><img src=\".thumbs/$file\" alt=\"$file\" /></a></div>\n";
	print_progress;
	$number++;

	if (-e "$thumbdir/$file") {
		print '-';
		next;
	}

	($dx, $dy) = ($image->width, $image->height);

	if ($dx > $thumb_max_dim and $dy > $thumb_max_dim) {
		if ($dx > $dy) {
			$thumb = $image->create_scaled_image($thumb_max_dim, 0);
		}
		else {
			$thumb = $image->create_scaled_image(0, $thumb_max_dim);
		}
	}
	else {
		$thumb = $image;
	}

	$thumb->set_quality($thumb_quality);
	$thumb->save("$thumbdir/$file");

	chmod(DEFAULT_FILEMODE, "$thumbdir/$file");
	print '+';
}
print "\n";

print $index <<'EOD';
</div></body>
</html>
EOD

close($index) or die("Cannot close $indexfile: $!");
chmod(DEFAULT_FILEMODE, $indexfile);

__END__

=head1 NAME

gen-xhtml-thumbnails - Generate Thumbnails + Index for a set of images

=head1 SYNOPSIS

B<gen-xhtml-thumbnails> [I<options>] [I<directory>]

=head1 DESCRIPTION

gen-xhtml-thumbnails will create an F<index.xhtml> with a list (thumbnails) of
all images found if I<directory>; the thumbnails will link to the images.

The F<index.xhtml> file will always be created in I<directory>.
The thumbnails will be saved in F<.thumbs>, also in I<directory>.
If I<directory> is not specified, the current directory is used.

Note that only the images in the directory itself will be processed, recursion
is not supported.

=head1 OPTIONS

=over

=item B<--css> I<file>

Use I<file> for css definitions

=item B<--size> I<pixels>

Maximum thumbnail size (either width or height)

=item B<--spacing> I<float>

Use I<float> as spacing factor.
The size of each image element (image + possible border around it) is the
number of pixels (see --size) times I<float>.
So for B<1.1> you have a small border around each image, for B<1.0> you have
no border at all, etc.

=item B<--title> I<title>

Use I<title> as <title> for the XHTML output

=item B<--quality> I<int>

Set thumbnail quality.
Accepts values between 0 and 100, where 100 is the highest possible quality

=back

=head1 EXIT STATUS

Zero upon success, non-zero otherwise.

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

B<gen-xhtml-thumbnails> requires the Image::Imlib2 perl module.

=head1 BUGS AND LIMITATIONS

There is no detection for changed images, the thumbnails of deleted images are
not removed.

=head1 AUTHOR

Copyright (C) 2009, 2010 by Daniel Friesel E<gt>derf@chaosdorf.deE<lt>

=head1 LICENSE

    0. You just DO WHAT THE FUCK YOU WANT TO