title: Resize image to maximum width or height in PHP without cropping
body: <p>I have an AJAX form that sends 4 pictures to and PHP form handler. The accepted formats are .gif, .png, .jpg or .jpeg. I am checking the image file type using javascript before sending the form.</p>
  
  <p>Now, in PHP I have this code:</p>
  
  <pre><code>    while($x &lt;= 4) {
          $target_dir = "files/";
          $target_file = $target_dir . basename($_FILES["fileUpload".$x]["name"]);
          $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
          if (move_uploaded_file($_FILES["fileUpload".$x]["tmp_name"], $target_file)) {
                      $filename = date("d-m-Y---H-i-s---").rand(10000,99999);
                      $oldfile = $target_file;
                      $newfile = $target_dir . $filename . "." . $imageFileType;
                      rename($oldfile, $newfile);
                      $file[$x] = $filename . "." . $imageFileType;
                      $fileCounter++;
          }
        $x++;
      }
  </code></pre>
  
  <p>Some people will upload large image files and I want to automatically resize them to a maximum width/height without cropping the image. How can I do that using PHP?</p>