title: Crop Uploaded Image To 1:1 Ratio PHP
body: <p>I have my own PHP function allowing users to update their profile image. </p>
  
  <p>It all works fine, however, I am having a problem where users can upload any size image they want; i.e: <code>564 x 346</code>. </p>
  
  <p>I do not want this to happen. I would like the image that they select to upload to be cropped to a 1:1 ratio and centred; i.e: it goes from <code>564 x 346</code> to <code>346 x 346</code> and centres to the middle of the image.</p>
  
  <p>None of the scripts that I have found seem to work for my site (at least the way I want them to).</p>
  
  <p>This is the code I am currently using to update their avatar. It consists of checking if they have a correct file extension &amp; if the image is less than 256kb in size:</p>
  
  <pre><code>    $ext = array('jpg', 'jpeg', 'png');
      $file       = $_FILES['avatar']['name'];
      $fileext    = strtolower(end(explode('.', $file)));
      $filetmp    = $_FILES['avatar']['tmp_name'];
  
      if(!in_array($fileext, $ext)){
          $errors[] = 'Please select a valid file type. (JPG, JPEG or PNG)';  }
      if($_FILES['avatar']['size'] &gt; 256000){
          $errors[] = 'Avatars cannot exceed a 256kb file size.';
      }
  
      if(empty($errors)){
          updateAvatar($conn, $username, $filetmp, $fileext);
      } else if(!empty($errors)){
          echo output_errors($errors);
      }
      if(isset($_SESSION['updateAvat'])){
          flash('You have successfully updated your avatar.');
          unset($_SESSION['updateAvat']);
      }
  </code></pre>
  
  <p>This is the <code>updateAvatar()</code> function that I have made and is called on line 13:</p>
  
  <pre><code>function updateAvatar($conn, $username, $filetmp, $fileext){
      $file = md5(microtime() . $filetmp) . '.' . $fileext;
      $filepth = './data/user_data/img/udid/prof/' . $file;
      move_uploaded_file($filetmp, $filepth);
      if(mysqli_query($conn, "UPDATE users SET profile = '$file' WHERE username = '$username'")){
          $_SESSION['updateAvat'] = md5(microtime() . $filetmp);
      } else {
          $errors[] = 'There was a problem updating your avatar. Please try again.';
      }
  }
  </code></pre>
  
  <p>However, this is not enough and does not allow my users profile page to work or look the way it should, I am going for something along the lines of how Google or Twitter do their avatar's.</p>
  
  <p>All help is appreciated.
  Cheers.</p>