title: Resize images in PHP without using third-party libraries?
body: <p>In one of my applications, I'm using the code snippet below to copy uploaded images to a directory. It works fine but copying large images (> 2MB) takes more time than ideal and I really don't need images this big, so, I'm looking for a way to resize the images. How to achieve this using PHP?</p>
  
  <pre><code>&lt;?php
  
  $uploadDirectory = 'images/0001/';
  $randomNumber = rand(0, 99999); 
  $filename = basename($_FILES['userfile']['name']);
  $filePath = $uploadDirectory.md5($randomNumber.$filename);
  
  // Check if the file was sent through HTTP POST.
  
  if (is_uploaded_file($_FILES['userfile']['tmp_name']) == true) {
  
      // Validate the file size, accept files under 5 MB (~5e+6 bytes).
  
      if ($_FILES['userfile']['size'] &lt;= 5000000) {
  
          // Move the file to the path specified.
  
          if (move_uploaded_file($_FILES['userfile']['tmp_name'], $filePath) == true) {
  
              // ...
  
          }
  
      }
  
  }
  
  ?&gt;
  </code></pre>