More thumb probs :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

More thumb probs :(

Post by Steveo31 »

I'm trying to create a script that will take a folder and resize all the images in it to a destination folder. Problem is it won't get past the first image. It creates one and after that it times out. Not sure where the problem is.

Code: Select all

<?php

if (isset($_POST['submit'])) {
    
    $folder = $_POST['folder_name'];
    $target_directory = $_POST['target'];
   
    if (is_dir($folder)) {
        $dir = opendir($folder);
        while (false !== ($file = readdir($dir))) {
            //open the folder and read through it
            while ($file != '.' && $file != '..') {
                $names = explode('.', $file);
                $name = $names[0];
                $ext = $names[1];
                $thumb_name = $name.'_thumb.'.$ext;
                
                $target = $target_directory."/".$thumb_name;
                
                $large_size = GetImageSize($folder."/".$file);
                
                $large_maxwidth = 105;
                $large_maxheight = 95;
                
                $large_width = $large_size[0];
                $large_height = $large_size[1];
                
                $large_x_ratio = $large_maxwidth/$large_width;
                $large_y_ratio = $large_maxheight/$large_height;
                
                if (($large_width <= $large_maxwidth) && ($large_height <= $large_maxheight)) {
                    $large_tn_width = $large_width;
                    $large_tn_height = $large_height;
                } else if (($large_x_ratio * $large_height) < $large_maxheight) {
                    $large_tn_height = ceil($large_x_ratio * $large_height);
                    $large_tn_width = $large_maxwidth;
                } else {
                    $large_tn_width = ceil($large_y_ratio * $large_width);
                    $large_tn_height = $large_maxheight;
                }
                $large_src = ImageCreateFromJpeg($folder.'/'.$file);
                $large_dest = imageCreateTrueColor($large_tn_width, $large_tn_height);
                
                ImageCopyResampled($large_dest, $large_src, 0, 0, 0, 0, $large_tn_width, $large_tn_height, $large_width, $large_height);
                ImageJpeg($large_dest, $target, 70);
                
                ImageDestroy($large_src);
                ImageDestroy($large_dest);
            }
        }
    }
}
?>

<form action="resize.php" method="post" enctype="multipart/form-data">
<table>
    <tr>
        <td>Folder to Resize:</td>
        <td><input type="text" name="folder_name" /></td>
    </tr>
    <tr>
        <td>Destination Folder:</td>
        <td><input type="text" name="target" /></td>
    </tr>
    <tr>
        <td><input type="submit" name="submit" value="Resize!" /></td>
    </tr>
</table>
</form>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your nested while() creates an infinite loop. line 12's while, should be an if.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Ah feyd, beautiful.
Post Reply