Main »

Thumbn 2



thumbn2.pl new version


#!/usr/bin/perl -WT

# crawls down dir tree and generates thumbs/ dirs then generates and saves thumbnails to the thumbs/ dir.
# usage:
#    ls -1 *.JPG | xargs -i{} ./thumbn.pl {}
#    dir ./thumb must already created.
#
#   OR
#   find pics/ \( -iname *.jpg -o -iname *.jpeg -o -iname *.png -o -iname *.gif -o -iname *.tif -o -iname *.tiff \) -exec ./thumbn.pl {} \;
#
#   to remove all the 'thumbs' directories:
#       find pics/ -type d -name thumbs -exec rm -rf {} \;

use strict;
use warnings;
use Image::Magick;
use File::Spec::Functions qw(splitpath);

my $thumbX = 75;
my $thumbY = 50;
my $thumbxMid = 640;
my $thumbyMid = 480;

my $filename = shift;
my ($drive, $dir, $file) = splitpath ($filename);
my $fileMid = $file;
if( $dir =~ /thumbs\/*$/ ){exit;}
print "  dir=$dir,   file=$file\n";
genDir($dir);
$file =~ s/(\..*?)$/_thumb$1/;
if($1 !~ /\.(gif|png|jpg|jpeg|tif|tiff)/i){exit;}
$fileMid =~ s/(\..*?)$/_thumb_Mid$1/;
if($1 !~ /\.(gif|png|jpg|jpeg|tif|tiff)/i){exit;}

if(!-e "$dir/thumbs/$file"){
    my $image = Image::Magick->new ();
    $image->ReadImage ($filename);
    my ($height, $width) = $image->Get ('height', 'width');
    my ($x, $y) = $height < $width ? ($thumbX, $thumbY) : ($thumbY, $thumbX);

    $image->Thumbnail (width=>$x, height=>$y);
    $image->Write ("$dir/thumbs/$file");
}

if(!-e "$dir/thumbs/$fileMid"){
    my $image = Image::Magick->new ();
    $image->ReadImage ($filename);
    my ($height, $width) = $image->Get ('height', 'width');
    my ($x, $y) = $height < $width ? ($thumbxMid, $thumbyMid) : ($thumbyMid, $thumbxMid);

    $image->Thumbnail (width=>$x, height=>$y);
    $image->Write ("$dir/thumbs/$fileMid");
}


sub genDir {
    my($dir) = @_;
    if(!-e "$dir/thumbs"){mkdir "$dir/thumbs";}
} # end sub genDir.


top level subjects:

Page last modified on March 26, 2010, at 04:18 AM

^