title: Image resizing and cropping with Php
body: <p>I'm using a php script to upload and resize an image, pretty simple:</p>
  
  <pre><code>if($_SERVER["REQUEST_METHOD"] == "POST") {
      $image = $_FILES["image_upload"];
      $uploadedfile = $image['tmp_name'];
  
      if ($image) {
          $filename = stripslashes($_FILES['image_upload']['name']);
          $extension = getExtension($filename);
          $extension = strtolower($extension);
          if (($extension != "jpg") &amp;&amp; ($extension != "jpeg") &amp;&amp; ($extension != "png") &amp;&amp; ($extension != "gif")) {
              $error_txt = 'Immagine incoretta';
              $errors=1;
          } else {
              $size=filesize($uploadedfile);
              if ($size &gt; MAX_SIZE*1024) {
                  $error_txt = "Immagine troppo grande";
                  $errors=1;
              }
              if($extension=="jpg" || $extension=="jpeg" ) {
                  $uploadedfile = $uploadedfile;
                  $src = imagecreatefromjpeg($uploadedfile);
              } else if($extension=="png") {
                  $uploadedfile = $uploadedfile;
                  $src = imagecreatefrompng($uploadedfile);
              } else {
                  $src = imagecreatefromgif($uploadedfile);
              }
  
              list($width,$height)=getimagesize($uploadedfile);
  
              $newwidth=500;
              $newheight=375;
              $tmp=imagecreatetruecolor($newwidth,$newheight);
  
  
  
              imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
  
  
              $filename = "images/". generateRandomString(5) . $image['name'];
  
              imagejpeg($tmp,$filename,100);
  
              imagedestroy($src);
              imagedestroy($tmp);
          }
      }
  </code></pre>
  
  <p>I want to got a bit further, right now im just resizing the image no matter the proportions, the think is, i want to resize it to a fixed with and height without losing the original proportion, and this of course is achieved through the cropping+resize of the original image.</p>
  
  <p>I have no idea how to do this using my actual <em>imagecreatetruecolor</em> and <em>imagecopyresampled</em> functions, and looking to the php manual seems is not very easy.</p>
  
  <p>There is a very <a href="http://www.binarymoon.co.uk/demo/timthumb-basic/" rel="nofollow">good library</a> im trying to integrate to my code, the use its as simple as <strong>mysite.com/lib/timthumb.php?src=castle1.jpg&amp;h=180&amp;w=120</strong> but i dont know how to integrate that with my actual code.</p>
  
  <p>So, what do you suggest?</p>