Hi again!

Yesterday I got familiar with the GD module (Quite a nice one). I have recently wrote a Photo Album with Perl and decided to write a little subroutine using GD to create thumbnails to every picture added to the photo album whose dimensions are heigher than the minimum (Let's say 250x250). I wrote this little program as a test:

#!/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; } } } }
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.

My pic folder - D:/Temp, has something like 80 .tmp files, 1 .png file and a folder, which holds one .png file and one folder, which holds one .png file.
On my first implementations, Perl gave me a deep recursion message and printed like a million "DONE"s (The print "DONE\n" statement was in the update sub), although (As I recall) nothing got updated. This was very weird cause the recursion should occur only 2 times (According to the contents of D:/Temp). On this implementation, I still get a Deep recursion message and only the files in the first 2 folders are updated. No thumbnail is created to the one in the third folder.

Anyone's got an idea why this happens?!
Thanks again.

In reply to A very deep recursion and the GD module (problem) by ido50

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.