Use Perl to resize images to thumbnail size. Helpful when you are supporting websites with a TON of images.
Requires
Image::Magick
#!/usr/bin/perl
#Name: thumbgen.pl
#Desc: Generate thumbnail sized images from full product images.
#Author: Rob Owens
##Change##
#initial:rowens/100411
#
#Notes:
#Development Env:
#ImageRoot:/var/www/html/images
#Full:/var/www/html/images/full
#Thumb:/var/www/html/images/thumb
#
#estore.savtira.net width=70
#
# PerlMagick Method(s)
#my($image, $x);
#
#$image = Image::Magick->new;
#$x = $image->Read('Blood_for_Blood_God_by_NachoMon.jpg');
#warn "$x" if "$x";
#
#$x = $image->Resize(geometry=>'70x');
#warn "$x" if "$x";
#
#$x = $image->Write('Blood_thumb.jpg');
#warn "$x" if "$x";
#
#
#
#my $FullDir = "/var/www/html/images/full";
use Image::Magick;
require './lib/imagelib';
#my $FullDir = '/home/rowens/repos/imageresize/html/images/full';
#my $ThumbDir = '/home/rowens/repos/imageresize/html/images/thumb';
my $FullDir = $ARGV[0];
my $ThumbDir = $ARGV[1];
my $size = $ARGV[2];
my $x = x;
if (( $FullDir eq '' ) || ( $ThumbDir eq '' )) {
usage(basic);
exit 1;
} elsif ( ! -e $FullDir ) {
usage(source);
exit 1;
} elsif ( ! -e $ThumbDir ) {
usage(target);
exit 1;
}
if ( $size eq '' ) {
$Resize = '70x';
} else {
$Resize = $size . $x;
}
opendir(D, "$FullDir") || die "Can't opedir $FullDir: $!\n";
while ( my $file = readdir(D) ) {
# remove "." and ".." directories
next if ( $file =~ m/^\./ );
# only files
next if ( -d "$FullDir/$file" );
# all images into an array
push ( @tmpfull, "$file" );
}
closedir(D);
#print "@full";
foreach $tmpfile (@tmpfull) {
# printf "PRE: $ThumbDir/$tmpfile\n";
next if ( -e "$ThumbDir/$tmpfile");
# printf "PST: $ThumbDir/$tmpfile\n";
push(@full, "$tmpfile");
}
#
foreach $file (@full) {
resize("$FullDir/$file","$ThumbDir/$file","$Resize");
}
# ./lib/imagelib
#use Image::Magick
my ($image, $x);
$image = Image::Magick->new;
sub resize {
$infile = $_[0];
$outfile = $_[1];
$resize = $_[2];
my($image);
$image = Image::Magick->new;
$image->Read($infile);
$image->Resize(geometry=>$resize);
printf "Resizing image: $infile\n";
$image->Write($outfile);
return 0;
} print;
sub usage {
$err = $_[0];
if ( $err =~ /source/ ) {
printf "Error: Source Directory doesn't exist\n";
printf "Usage: thumbgen <source_directory> <target_directory> [pix width]\n\n";
} elsif ( $err =~ /target/ ) {
printf "Error: Target Directory doesn't exist\n";
printf "Usage: thumbgen <source_directory> <target_directory> [pix width]\n\n";
} elsif ( $err =~ /basic/ ) {
print<<EOF;
Thumb Generator: Used to generate thumbnails. Shrinking width size while
maintaining scale.
Usage: thumbgen <source_directory> <target_directory> [pix width]
Example(s):
Resize original images to thumbnail size, using default 70px wide:
thumbgen /var/www/html/images/full /var/www/html/images/thumb
Resize original images to thumbnail size, specifying 100px width:
thumbgen /var/www/html/images/full /var/www/html/images/thumb 100
NOTE! Program maintains the filename, hence output directory.
EOF
}
return 1;
} print;