#!/usr/bin/perl # Copyright © 2009,2010 by Daniel Friesel # License: 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 $thumb_show_names = 0; 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( 'c|css=s' => \$css_source, 'd|size=i' => \$thumb_max_dim, 's|spacing=f' => \$thumb_spacing, 'n|show-name' => \$thumb_show_names, 't|title=s' => \$title, 'q|quality=i' => \$thumb_quality, ); if (@ARGV > 0) { $directory = shift; } if (defined($css_source)) { $css_line = ""; } open(my $index, '>', $indexfile) or die("Cannot open $indexfile for writing: $!"); print $index <<"EOD"; $title $css_line
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 $thumb; if (defined($css_source)) { print {$index} '
'; } else { printf {$index} ( '
', 'text-align: center', 'font-size: 80%', 'float: left', $thumb_max_dim * $thumb_spacing, ($thumb_max_dim * $thumb_spacing) + ($thumb_show_names ? 10 : 0), ); } print {$index} "\"$file\"\n"; if ($thumb_show_names) { print {$index} "\n${file}"; } print {$index} "
\n"; print_progress; $number++; if (-e "$thumbdir/$file") { print '-'; next; } my $image = Image::Imlib2->load($file); ($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';
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 [I] [I] =head1 DESCRIPTION gen-xhtml-thumbnails will create an F with a list (thumbnails) of all images found if I; the thumbnails will link to the images. The F file will always be created in I. The thumbnails will be saved in F<.thumbs>, also in I. If I 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<-c>, B<--css> I Use I for css definitions =item B<-n>, B<--show-names> Show image names below thumbnails =item B<-d>, B<--size> I Maximum thumbnail size (either width or height) =item B<-s>, B<--spacing> I Use I as spacing factor. The size of each image element (image + possible border around it) is the number of pixels (see --size) times I. 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<-t>, B<--title> I Use I<title> as <title> for the XHTML output =item B<-q>, 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