Unable to Resize Multiple Images in PHP

advertisements

I am trying to upload various images into a dynamically created folder on my server, then take each image and resize it while uploading it into the folder as well creating a new image and a new name.. example: image.jpg (original image) and image-resized.jpg (being the thumbnail image).

The thing I can not figure out is how to resize all images. I am not sure if I should put it in a loop. All I need is for each image I upload (could be 5 a time). It loops through and resizes them all and not just a single image. Here is my code any help would be appreciated!

Code to create folders and move picture into those folders:

// Desired folder structure
        $structure = './Fotos/'.$newfolder;

        // To create the nested structure, the $recursive parameter
        // to mkdir() must be specified.

        if (!mkdir($structure, 0, true)) {
        die('Failed to create folders...');
}else{
    $placefoldername = mysql_query("INSERT INTO datefolders (FolderDate) VALUES ('$newfolder')") or die(mysql_error());
    echo "<div class=\"success\">El folder fue agregado con exito.<input type=\"button\" name=\"close\" value=\"X\" class=\"close\" /></div>";
    }}

// ...

    }

if(isset($_POST['upload'])){
    $FolderDate = $_POST['fecha-folder'];
    $FolderName = $_POST['FolderName'];
    $hour = $_POST['hour'];

        // Desired folder structure
        $structure = './Fotos/'.$FolderDate.'/'.$hour.'/'.$FolderName;

        // To create the nested structure, the $recursive parameter
        // to mkdir() must be specified.

        for($i=0;$i<count($_FILES['fileupload']['name']);$i++) {
            $names = $_FILES['fileupload']['name'][$i];
            $target_path = "Fotos/".$FolderDate."/".$hour."/".$FolderName."/";

            $target_path = $target_path . basename( $_FILES['fileupload']['name'][$i]); 

            if(move_uploaded_file($_FILES['fileupload']['tmp_name'][$i], $target_path)) {
             $success = 1;

Code to create a smaller (resized image) and also place into the already created folder:

$img = $names;
        $imgPath = $structure;

        function resizeImage($img, $imgPath, $suffix, $by, $quality)
{
    //Create a thunbnail image by resizing the picture
    // Open the original image.
    $original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<em>$imgPath/$img</em>)");
    list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");

    // Determine new width and height.
    $newWidth = ($width/$by);
    $newHeight = ($height/$by);

    // Resample the image.
    $tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
    imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");

    // Create the new file name.
    $newNameE = explode(".", $img);
    $newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .'';

    // Save the image.
    imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");

    // Clean up.
    imagedestroy($original);
    imagedestroy($tempImg);
    return true;
}
$resize = resizeImage($img, $imgPath, "-resized", 23, 100);


Why are you defining the function resizeImage in the for loop? It is being redefined every time the loop iterates. This could be part of the problem. Define the function outside the loop and see if that works.