summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorDaniel Friesel <derf@derf.homelinux.org>2009-12-28 11:57:30 +0100
committerDaniel Friesel <derf@derf.homelinux.org>2009-12-28 11:57:30 +0100
commit81c765319249d86c7cbeb2c57d4a39dffb0f98c9 (patch)
tree6735fe8e726214cb771d86eedd72d4f01ef6d2b9 /bin
parentc6559974de52967619ee54d02846f71550fdb3e5 (diff)
Move gen-xhtml-thumbnails to bin/
Diffstat (limited to 'bin')
-rwxr-xr-xbin/gen-xhtml-thumbnails124
1 files changed, 124 insertions, 0 deletions
diff --git a/bin/gen-xhtml-thumbnails b/bin/gen-xhtml-thumbnails
new file mode 100755
index 0000000..7b25023
--- /dev/null
+++ b/bin/gen-xhtml-thumbnails
@@ -0,0 +1,124 @@
+#!/usr/bin/perl
+# Copyright © 2009 by Daniel Friesel <derf@derf.homelinux.org>
+# License: WTFPL <http://sam.zoy.org/wtfpl>
+use strict;
+use warnings;
+use Image::Imlib2;
+use constant {
+ DEFAULT_FILEMODE => oct(644),
+ THUMB_MAX_DIM => 150,
+ THUMB_QUALITY => 60,
+};
+
+my $directory = shift || '.';
+my $thumbdir = "$directory/.thumbs";
+my $indexfile = "$directory/index.xhtml";
+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;
+}
+
+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>
+ <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
+</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(@files)) {
+ my $image = Image::Imlib2->load($file);
+ my $thumb;
+
+ printf {$index} (
+ '<div style="float: left; width: %dpx; height: %dpx">',
+ THUMB_MAX_DIM * 1.1,
+ THUMB_MAX_DIM * 1.1,
+ );
+ 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 > $dy) {
+ $thumb = $image->create_scaled_image(THUMB_MAX_DIM, 0);
+ } else {
+ $thumb = $image->create_scaled_image(0, THUMB_MAX_DIM);
+ }
+
+ $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<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.