reworked the thumbnailer for WGBP and added a switch to only thumbnail things without thumbnails

This commit is contained in:
Colin Kuskie 2007-03-01 22:19:45 +00:00
parent 89686f3fd9
commit 7fb381c646

View file

@ -14,65 +14,95 @@
#----------------------------------------- #-----------------------------------------
use File::stat; use File::stat;
use File::Find ();
use Image::Magick; use Image::Magick;
use Getopt::Long;
use lib "../lib"; use lib "../lib";
use WebGUI::Utility; use WebGUI::Utility;
my $thumbnailSize;
my $onlyMissingThumbnails;
if ($ARGV[0] ne ""){ my $path = shift @ARGV;
$results = recurseFileSystem($ARGV[0]);
} else { my $ok = GetOptions(
print "Usage: perl $0 <uploadsPath> [<thumbnailSize (50)>]\n"; 'size=i'=>\$thumbnailSize,
'missing'=>\$onlyMissingThumbnails,
);
unless ($path ne "" and $ok){
print <<USAGE;
Usage: perl $0 <uploadsPath> [--size=thumbnailSize] [--missing]
uploadsPath is the complete path to your uploads directory
--size allows you to override the default thumbnail size of 50
--missing says to only create thumbnails for images that are missing thumbnails.
USAGE
exit 0;
} }
$thumbnailSize ||= 50; ##set default
File::Find::find(\&findThumbs, $path);
#----------------------------------------- #-----------------------------------------
# getType(filename) sub findThumbs {
#----------------------------------------- ##Remember, by default we are chdir'ed to the directory with the files in it.
sub getType { ##Skip directories
my ($extension); return if -d $_;
$extension = $_[0];
$extension =~ s/.*\.(.*?)$/$1/; ##Only Thumbnail files that we should.
return lc($extension); return unless shouldThumbnail($_);
createThumbnail($_, $File::Find::dir);
return 1; ##Just for cleanliness
} }
#----------------------------------------- #-----------------------------------------
# createThumbnail(filename,path) # createThumbnail(filename,path)
#----------------------------------------- #-----------------------------------------
sub createThumbnail { sub createThumbnail {
my ($image, $x, $y, $r, $n, $type); my ($image, $x, $y, $r, $n, $type);
$type = getType($_[0]); my ($fileName, $fileDir) = @_;
if (isIn($type, qw(jpg jpeg gif png tif tiff bmp)) && !($_[0] =~ m/thumb-/)) { print "Nailing: $fileDir/$fileName\n";
print "Nailing: $_[1]/$_[0]\n"; $image = Image::Magick->new;
$image = Image::Magick->new; $image->Read($fileName);
$image->Read($_[1].'/'.$_[0]); ($x, $y) = $image->Get('width','height');
($x, $y) = $image->Get('width','height'); $r = $x>$y ? $x / $thumbnailSize : $y / $thumbnailSize;
$n = $ARGV[1] || 50; $image->Scale(width=>($x/$r),height=>($y/$r)) if ($r > 0);
$r = $x>$y ? $x / $n : $y / $n; if (isIn($type, qw(tif tiff bmp))) {
$image->Scale(width=>($x/$r),height=>($y/$r)) if ($r > 0); $image->Write('thumb-'.$fileName.'.png');
if (isIn($type, qw(tif tiff bmp))) { } else {
$image->Write($_[1].'/thumb-'.$_[0].'.png'); $image->Write($_[1].'/thumb-'.$fileName);
} else {
$image->Write($_[1].'/thumb-'.$_[0]);
}
} }
} }
#----------------------------------------- sub shouldThumbnail {
# recurseFileSystem(path) my ($fileName) = @_;
#-----------------------------------------
sub recurseFileSystem { my $fileType = getType($fileName);
my (@filelist, $file);
if (opendir(DIR,$_[0])) { ##I am a thumbnail, skip me
@filelist = readdir(DIR); return 0 if $fileName =~ m/thumb-/;
foreach $file (@filelist) {
unless ($file eq "." || $file eq "..") { ##I am not a graphics file, skip me
recurseFileSystem($_[0]."/".$file); return 0 if !isIn($fileType, qw(jpg jpeg gif png tif tiff bmp));
createThumbnail($file,$_[0]);
} ##My thumbnail already exists and I was told not to do it again
} return 0 if ($onlyMissingThumbnails && -e 'thumb-'.$fileName);
closedir(DIR);
} return 1;
} }
#-----------------------------------------
# getType(filename)
#-----------------------------------------
sub getType {
my ($fileName) = @_;
my ($extension) = $fileName =~ m/(\w+)$/;
return lc($extension);
}