As you can see the program get's the main picture directory (This is how the photo album works). All the pictures in the photo album must be under this directory (If specifically in it, or in subdirectories). The program should go through all those subdirectories and update every file who's above the minimal dimensions (update = create a thumbnail). The implementation is definitely not the most efficient one. It's just I've tried several ways and still get (Pretty much) the same result. I just left the last one I tried.#!/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, $w +idth, $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; } } } }
In reply to A very deep recursion and the GD module (problem) by ido50
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |