上传多个图像并调整jquery ajax php的大小

I am trying to create an image gallery in html, jquery ajax and php. So far it is working at 90% I would say, the images are uploaded to a specific folder and saved into the database, but I am having some problems when I start uploading from 6 pictures at the same time, some pictures are resized really small and not taking the values I give in the code.

Here is my HTML:

<form id="upload-images-form" enctype="multipart/form-data">
  <input type="file" name="imagesinput[]" id="imagesinput" multiple="multiple" accept="image/jpeg">
  <input id="upload-images-button" class="btn btn-success" name="submit" type="submit" value="Subir"/>
</form>

My jquery:

$('#upload-images-form').on("submit", function(e){

   e.preventDefault();
            console.log("uploading images");

   var formData = new FormData(this);

   var request = $.ajax({
      method: "POST",
      url: "upload_gallery/",
      cache: false,
      processData: false,
      contentType: false,
      data: formData,
      xhr: function() {
        var xhr = $.ajaxSettings.xhr();
        progressBar.parent().css('display','block');
        if (xhr.upload) {
           xhr.upload.addEventListener('progress', function(event) {
              var percent = 0;
              var position = event.loaded || event.position;
              var total = event.total;
              if (event.lengthComputable) {
                 percent = Math.ceil(position / total * 100);
              }
              //update progressbar
                progressBar.css("width", + percent +"%");
                progressBar.text(percent +"%");
           }, true);
        }
         return xhr;
      },
      mimeType:"multipart/form-data"
   });
   request.done(function( msg ) {
      console.log(msg);
   });
   request.fail(function( jqXHR, textStatus ) {
      console.log( "Request failed: " + textStatus );
   });
});

And finally my PHP, where I proccess and validate the images;

$images = $_FILES['imagesinput'];
$responses = array();
$resized_width = 1000; //new max-width for images
$resized_height = 700; // new max-height for images

if(isset($_FILES['imagesinput'])){

   $img_desc = reArrayFiles($images); // function to help handling the array coming from ajax

   $i = 0;
   $len = count($img_desc);

   foreach($img_desc as $val){ // array loop

      if($val['size'] <= 5 * 1024 * 1024){ // size validation

         if($val['type'] == "image/jpeg" || $val['type'] == "image/jpg"){ // format validation

            if(!is_dir(ROOT_URI.'/gallery/user_id_'.$id)){ // check if folder exist

               mkdir(ROOT_URI.'/gallery/user_id_'.$id, 0777);

            }

            $total_files = new FilesystemIterator('../../gallery/user_id_'.$id); // limit images

            if(iterator_count($total_files) === 20){

               array_push($responses, 'alert_total_files');
               echo json_encode($responses);
               die;

            }

            list($original_width, $original_height) = getimagesize($val['tmp_name']); // get image size

            $original_ratio = $original_width/$original_height;

            if ($resized_width / $resized_height > $original_ratio) {
               $resized_width = $resized_height * $original_ratio;
            } else {
               $resized_height = $resized_width / $original_ratio;
            }

            // resize image(proportional)
            $image_p = imagecreatetruecolor($resized_width, $resized_height);
            $resized_image = imagecreatefromjpeg($val['tmp_name']);
            imagecopyresampled($image_p, $resized_image, 0, 0, 0, 0, $resized_width, $resized_height, $original_width, $original_height);

            // verificar de que la imagen no existe, si existe, no se sube
            if(!file_exists('../../gallery/user_id_'.$id.'/'.$val['name'])){

               $conn = mysqli_connect($servername, $username, $password, $db);

               /* check connection */
               if (mysqli_connect_errno()) {
                  printf("Connect failed: %s
", mysqli_connect_error());
                  exit();
               }

               imagejpeg($image_p, ROOT_URI.'/local_gallery/local_id_'.$id.'/'.$val['name']);

               $path_url = BASE_URI.'app/gallery/user_id_'.$id.'/'.$val['name'];

               $insert =  mysqli_prepare($conn, "INSERT INTO user_gallery (id_user, path_url) VALUES (?, ?)");

               mysqli_stmt_bind_param($insert, "ss", $id, $path_url);

               if(mysqli_stmt_execute($insert)){

                  array_push($responses, 'alertsuccess');

               }

               imagedestroy($image_p);

             }
             else{
                array_push($responses, 'file_exists');

             }


           }
           else{

              array_push($responses, 'alertformat');

           }


        }
        else{

           array_push($responses, 'alertsize');

        }

        if($i == $len - 1){
           echo json_encode($responses);
        }

        $i++;

     }

}

function reArrayFiles($file){
   $file_ary = array();
   $file_count = count($file['name']);
   $file_key = array_keys($file);

   for($i=0;$i<$file_count;$i++){
    foreach($file_key as $val){
        $file_ary[$i][$val] = $file[$val][$i];
    }
   }
   return $file_ary;
 }

I'm trying to understand what could be the problem, and I just can think that maybe it is because the loop is too fast to give the correct values so then it still takes the old values from the previus image?

Some clues? I would really appreciate it, thanks.