summaryrefslogtreecommitdiff
path: root/include/gen-xhtml-thumbnails
blob: 3c64ea9f83ff92c082850f465d86df3c5198fe8a (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
#!/usr/bin/perl
use strict;
use warnings;
use Image::Imlib2;

my $directory = shift || '.';
my $thumbdir  = "$directory/.thumbs";
my $indexfile = "$directory/index.xhtml";
my ($file, $image, $thumb);
my ($dx, $dy, $tx, $ty);
$| = 1;
open(my $index, '>', $indexfile);
print $index <<ficken;
<!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>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	<link rel="stylesheet" type="text/css" href="foo.css"/>
</head>
<body><div>
ficken

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

opendir(my $dirhandle, $directory) or die("Cannot open $directory: $!");
print "Generating thumbnails [ - cached | + generated ]\n";
foreach $file (readdir($dirhandle)) {
	next unless ($file =~ /\.(png|jpe?g)$/i);

print $index <<ficken;
	<a href="$file"><img src=".thumbs/$file"/></a>
ficken

	if (-e "$thumbdir/$file") {
		print '-';
		next;
	}
	$image = Image::Imlib2->load($file);
	($dx, $dy) = ($image->width, $image->height);
	if ($dx > $dy) {
		$thumb = $image->create_scaled_image(150, 0);
	} else {
		$thumb = $image->create_scaled_image(0, 150);
	}
	$thumb->set_quality(60);
	$thumb->save("$thumbdir/$file");
	chmod(0644, "$thumbdir/$file");
	print '+';
}
print "\n";

print $index <<ficken;
</div></body>
</html>
ficken

close($index);
chmod(0644, $indexfile);