#!/usr/bin/perl
#
#
# [make-thumbs.pl] 2002-03-14 René Scholz <Voland@Jena.Thur.de>
#
# Time-stamp: <2002-03-15 21:19:35, rscholz@jpiros.rnd.j.intershop.de>
#
#
# Usage: - creates thumbnail pics with convert (ImageMagick), if file is group-readable
# - creates a html file with a <DIV> area for the thumbnail pics
# which can be included in html pages via SSI with:
# <!--#include file="include.thumbs"-->
# - different image types can be used
# - the images are sorted alphabetically
#
#################################################################################
($NAME=$0) =~ s|.*/||; # get basename from $0
use Getopt::Std;
use vars qw($NAME $USAGE $opt_n $opt_h $opt_x $opt_y $opt_e $opt_o $opt_s $opt_t $opt_f
$opt_c $opt_r $pic $picext $miniext @PICFILES %TYPES $GEOMETRY $OUTPUT $OUTFILE
$i $alt $height $width $colors $img $groupreadble $minspace $vspace $hspace
$maxwidth %MINIWIDTH %MINIHEIGHT %MINIEXT);
use strict;
$USAGE=<<EOUSAGE;
Usage: $NAME [options]
Options:
-h Show this help.
-n pics How many thumbs in each row
-s pixel minimal horizontal and vertical space (in pixels*2)
-e ext[,ext,...] allowed image types (Ex: -e jpg,gif,png)
-t ext image type for the thumnail pics (Ex: -t jpg)
If -t is not used, the thumnails have the same type
as the original image
-o Overwrite existing thumbnail files.
-x width of thumbnails
-y height of thumbnails
Only one of the options -x or -y should be used
(Ex: -y 100 so that all thumbnails have the same height)
-c colors Number of colors in the thumbnail to use
-f file output file name (default: include.thumbs)
-r regex Search only matching image file names, ex: -r "Town*"
EOUSAGE
die $USAGE unless(@ARGV);
getopts('hn:s:e:f:x:y:c:t:or:');
die $USAGE if($opt_h);
if($opt_e)
{ foreach (split(",", $opt_e)) { $TYPES{$_} = 1; } }
else
{ %TYPES = ( "jpg" => 1 ) };
$minspace = $opt_s ? $opt_s : "5";
$colors = $opt_c ? "-colors $opt_c" : "";
$vspace = "vspace=\"$minspace\"";
$OUTFILE = $opt_f ? $opt_f : "include.thumbs";
$OUTPUT = "<DIV align=\"center\">\n";
if(-f ".geometry-mini")
{
$GEOMETRY = `cat .geometry-mini`; chomp($GEOMETRY);
$GEOMETRY = "x100" if($GEOMETRY eq "");
}
if ($opt_x && $opt_y) { $GEOMETRY = $opt_x . "x" . $opt_y . "!"; }
elsif($opt_x) { $GEOMETRY = $opt_x; }
elsif($opt_y) { $GEOMETRY = "x" . $opt_y; }
else { $GEOMETRY = "x100"; }
# get files which should be handled:
opendir(CDIR, ".") || die "Cant't open current directory $!\n";
while(defined($pic = readdir CDIR))
{
next if ($pic eq '.' || $pic eq '..');
if( $pic =~ /\.([^\.]+)$/ ) { $picext = $1; } else { $picext = ""; }
if(exists($TYPES{$picext}))
{
if(!$opt_r || $pic =~ /$opt_r/) { push(@PICFILES, $pic); }
}
}
closedir(CDIR);
# work on this files:
$maxwidth = 0;
foreach $pic (@PICFILES)
{
my ($E);
$_ = $pic;
if( /\.([^\.]+)$/ ) { $picext = $1; } else { $picext = ""; }
$miniext = $opt_t ? $opt_t : $picext;
next if(/\.mini\.$miniext$/);
s/\.$picext$/.mini.$miniext/;
# check if file is group readable (that means, the webserver can read it)
# if not, do nothing with this file, do not include it in the output.
$groupreadble = (stat $pic)[2] & 040;
next unless($groupreadble);
if(! -f $_ or $opt_o)
{
print "executing: convert $colors -geometry '$GEOMETRY' $pic $_\n";
system "convert $colors -geometry '$GEOMETRY' $pic $_";
}
$E=`identify $_`; # print " -> $E";
$E=~/ (\d+)x(\d+) /;
$width = $1; $height = $2;
$width+=1 if($width % 2 != 0);
$maxwidth = $width if($maxwidth < $width);
$MINIWIDTH{$pic} = $width;
$MINIHEIGHT{$pic} = $height;
$MINIEXT{$pic} = $miniext;
}
$i = $alt = 1;
foreach $pic (sort keys %MINIWIDTH)
{
$_ = $pic;
if( /\.([^\.]+)$/ ) { $picext = $1; } else { $picext = ""; }
$miniext = $MINIEXT{$pic};
s/\.$picext$/.mini.$miniext/;
$width = $MINIWIDTH{$pic};
$height = $MINIHEIGHT{$pic};
$hspace = ($maxwidth - $width) / 2 + $minspace;
$hspace = "hspace=\"$hspace\"";
$img = "ALT=\"$alt\" HEIGHT=\"$height\" WIDTH=\"$width\" BORDER=\"1\" $vspace $hspace";
$OUTPUT.=" <A HREF=\"$pic\"><IMG src=\"$_\" $img></A>\n";
if($i++ >= $opt_n) { $OUTPUT.=" <BR clear=\"all\">\n"; $i = 1; }
$alt++;
}
$OUTPUT.="</DIV>\n";
open (O, ">$OUTFILE") or die "Can't write to $OUTFILE: $!\n";
print O $OUTPUT;
close (O);