title: Adding Crop to PHP Image Resize Script
body: <p>I have an existing image upload script (below) which works fine but I would like to add a crop function to it, so each uploaded photo maintains the right aspect ration, but is cropped to, say, 200 x 200px.</p>
  
  <p>I have looked at other questions on SO relating to this but ideally I'd like to add cropping to my script, rather than implement a whole new one, if that makes sense.</p>
  
  <p>Could anyone help?</p>
  
  <p>Thanks as always.</p>
  
  <pre><code>mkdir("images/$user_id");
  $saveto = "images/$user_id/$user_id.jpg";
  move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
  $typeok = TRUE;
  
  switch($_FILES['image']['type'])
  {
      case "image/gif":   $src = imagecreatefromgif($saveto); break;
  
      case "image/jpeg":  // Both regular and progressive jpegs
      case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
  
      case "image/png":   $src = imagecreatefrompng($saveto); break;
  
      default:            $typeok = FALSE; break;
  }
  
  if ($typeok)
  {
  
  
  
  
      list($w, $h) = getimagesize($saveto);
      $max = 200;
      $tw  = $w;
      $th  = $h;
  
      if ($w &gt; $h &amp;&amp; $max &lt; $w)
      {
          $th = $max / $w * $h;
          $tw = $max;
      }
      elseif ($h &gt; $w &amp;&amp; $max &lt; $h)
      {
          $tw = $max / $h * $w;
          $th = $max;
      }
      elseif ($max &lt; $w)
      {
          $tw = $th = $max;
      }
  
      $tmp = imagecreatetruecolor($tw, $th);
      imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
      imageconvolution($tmp, array( // Sharpen image
                              array(-1, -1, -1),
                              array(-1, 16, -1),
                              array(-1, -1, -1)
                             ), 8, 0);
      imagejpeg($tmp, $saveto);
      imagedestroy($tmp);
      imagedestroy($src);
  
  
  }
  </code></pre>
  
  <p>EDIT: I have found the following script which works fine when its on its own page, however I'm having trouble implementing it either into or after my existing upload script - I get a few 'failed to open stream: No such file or directory' errors - however the path to the image is correct (i've echoed it out to be sure):</p>
  
  <pre><code>$filename = 'images/$user_id/$user_id.jpg';
  
  // Get dimensions of the original image
  list($current_width, $current_height) = getimagesize($filename);
  
  // The x and y coordinates on the original image where we
  // will begin cropping the image
  $left = 25;
  $top = 25;
  
  // This will be the final size of the image (e.g. how many pixels
  // left and down we will be going)
  $crop_width = 200;
  $crop_height = 200;
  
  // Resample the image
  $canvas = imagecreatetruecolor($crop_width, $crop_height);
  $current_image = imagecreatefromjpeg($filename);
  imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
  imagejpeg($canvas, $filename, 100);
  </code></pre>
  
  <p>Could someone help me put the two together?</p>
  
  <p>Thanks</p>