多个图像在上载时调整大小并重命名

I am trying to resize the image. It works fine with single image. But with multiple image, I have to use loop to get images one by one.

I am getting the error==> Fatal error: Cannot redeclare createthumb()

Below is my code for index

HTML

<input id="files" name="files[]" multiple="multiple" accept="image/*" type="file">

PHP

$count = count($_FILES['files']['name']);

    $i = 0;
    while($i<$count)
    {
        move_uploaded_file($_FILES["files"]["tmp_name"][$i],"uploaded_files/" . $_FILES["files"]["name"][$i]);

        //===========>Image Resize<=============
        function createthumb($name,$filename,$new_w,$new_h)
        {
            $system=explode(".",$name);

            if (preg_match("/jpg|JPG|jpeg|JPEG/",$system[1])){$src_img=imagecreatefromjpeg($name);}

            elseif (preg_match("/png|PNG/",$system[1])){$src_img=imagecreatefrompng($name);}

            elseif (preg_match("/gif|GIF/",$system[1])){$src_img=imagecreatefromgif($name);}


            $old_x=imageSX($src_img);
            $old_y=imageSY($src_img);

            $thumb_w=500;
            $thumb_h=300;

            $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
            imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

            if (preg_match("/png/",$system[1]))
            {
                imagepng($dst_img,$filename); 
            } 
                elseif (preg_match("/gif/",$system[1]))
            {
                imagegif($dst_img,$filename); 
            }
            else{
            imagejpeg($dst_img,$filename); 
            }

            imagedestroy($dst_img); 
            imagedestroy($src_img); 
        }
        $source_photo = 'uploaded_files/'. $_FILES["files"]["name"][$i];
        $dest_photo = 'uploaded_files/'.$id.'_'.$i.'jpg';
         //$d has some defined velue
        $d = createthumb($source_photo, $dest_photo, 400, 400);



        //===========>Image Resize<============
        $i++;
    }

It would be great if any one fix it.

The error message said "Cannot redeclare createthumb()" because the function createthumb declaration is located inside a while loop. Could you please move it outside the loop?

Try moving the create_thumb() function declaration to the top/bottom of the script - as you have it inside the while loop, I imagine it's trying to redeclare the function on the second iteration, which is failing.

Edit

There's also an error in this line:

$dest_photo = 'uploaded_files/'.$id.'_'.$i.'jpg';

$id hasn't been defined anywhere, and there should be an extra "." in front of the 'jpg' to set the extension correctly. Try something like:

$dest_photo = 'uploaded_files/thumb.jpg';

Should work. You'll just need to do some extra logic to change the extension if the uploaded file is a GIF/PNG/etc.

You are doing function declaration inside while loop.
Do like this...

$count = count($_FILES['files']['name']);

$i = 0;
while($i<$count)
{
    move_uploaded_file($_FILES["files"]["tmp_name"][$i],"uploaded_files/" . $_FILES["files"]["name"][$i]);

    //===========>Image Resize<=============

    $source_photo = 'uploaded_files/'. $_FILES["files"]["name"][$i];
    $dest_photo = 'uploaded_files/'.$id.'_'.$i.'jpg';
     //$d has some defined velue
    $d = createthumb($source_photo, $dest_photo, 400, 400);

    //===========>Image Resize<============
    $i++;
}

function createthumb($name,$filename,$new_w,$new_h)
{
    // function defination        
}