#!/usr/bin/perl use strict; use GD; use Image::Size; use IO::File; use IO::Dir; use constant MAX_WIDTH => 80; use constant MAX_HEIGHT => 80; use constant IMAGEPATH => "/usr/local/httpd/htdocs/thumbs/"; resize_images(IMAGEPATH, ".jpg", MAX_WIDTH, MAX_HEIGHT); sub resize_images {   my ($folder, $ext, $maxwidth, $maxheight) = @_;   my $dir = new IO::Dir;   if($dir->open($folder))   {      while(defined(my $filename = $dir->read))      {         my $file = "$folder/$filename";         next if $file =~ /^\.\.?$/;         next if -d $file;         next if $file !~ /$ext$/io;                 eval         {            my ($width, $height) = imgsize($file);            my $relation = ($height / $max_height) > ($width / $max_width) ? $height / $maxheight : $width / $maxwidth;            my $thumbnailheight = sprintf ("%.0f",$height / $relation);            my $thumbnailwidth = sprintf ("%.0f",$width / $relation);                  my $big = GD::Image->newFromJpeg($file);            my $lit = new GD::Image($thumbnailwidth, $thumbnailheight);            $lit->copyResized($big, 0, 0, 0, 0, $thumbnailwidth, $thumbnailheight, $width, $height);            my $fh = new IO::File;            if($fh->open("> $file))            {               print $fh $lit->jpeg;               $fh->close();            }            undef $lit;            undef $big;         }      }      $dir->close();   }   }