#!/usr/bin/perl -w use GD; my @dirs; $directory = "D:/Temp"; getdirs($directory); for $dir (@dirs) { update($dir); } print "DONE\n"; sub getdirs { my $directory = shift; opendir (DIR, $directory); @files = readdir DIR; closedir DIR; push (@dirs, $directory); for $file (@files) { if (-d "$directory/$file") { push (@dirs, "$directory/$file") unless ($file eq "." || $file eq ".."); getdirs("$directory/$file"); } } } sub update { my $directory = shift; for $file (@files) { $full = "$directory/$file"; if (-f $full && $full =~ m/\.png/i) { open (PNG, "$full") || die; $image = newFromPng GD::Image(\*PNG) || die; close PNG; ($width,$height) = $image->getBounds(); if ($width <= 250 && $height <= 250) { # DO NOTHING } else { if ($width <= 250 && $height > 250) { $percent = (250*100)/$height; $newidth = ($width*$percent)/100; $desimage = new GD::Image($newidth, 250); $desimage->copyResized($image, 0, 0, 0, 0, $newidth, 250, $width, $height); } elsif ($width > 250 && $height <= 250) { $percent = (250*100)/$width; $newheight = ($height*$percent)/100; $desimage = new GD::Image(250, $newheight); $desimage->copyResized($image, 0, 0, 0, 0, 250, $newheight, $width, $height); } else { if ($width > $height) { $percent = (250*100)/$width; $newheight = ($height*$percent)/100; $desimage = new GD::Image(250, $newheight); $desimage->copyResized($image, 0, 0, 0, 0, 250, $newheight, $width, $height); } else { $percent = (250*100)/$height; $newidth = ($width*$percent)/100; $desimage = new GD::Image($newidth, 250); $desimage->copyResized($image, 0, 0, 0, 0, $newidth, 250, $width, $height); } } open (FILE, "+>$directory/less$file") or die "Can't open file: $!"; binmode FILE; print FILE $desimage->png; close FILE; } } } }