title: In PHP GD: How to scale image to certain width, while maintaining its proportions. Then crop the height from the bottom
body: <p>To clarify, I am looking to imitate the following behavior that occurs in HTML/CSS.</p>
  
  <pre><code>&lt;div style="width: 260px; height: 174px"&gt;
    &lt;img src="path/img.png" style="width: 100%; overflow: hidden"&gt;  
  &lt;/div&gt;
  </code></pre>
  
  <p>This causes the width to always be my preferred width, while still maintaining its proportions. It then appears as if the bottom part of the height is getting cut off, since the overflow is set to hidden. I would like to imitate this behavior while using the PHP GD library to generate the image. I apologize in advance if there is an obvious solution to this, but this is what I tried for far too long to avail.</p>
  
  <pre><code>$image = imagecreatefromstring(file_get_contents($uploadfile));
  $filename = $uploaddir . '/thumb.png';
  
  $thumb_width = 260;
  $thumb_height = 174;
  
  $width = imagesx($image);
  $height = imagesy($image);
  
  $original_aspect = $width / $height;
  $thumb_aspect = $thumb_width / $thumb_height;
  
  if ( $original_aspect &gt;= $thumb_aspect )
  {
    $new_height = $thumb_height;
    $new_width = $width / ($height / $thumb_height);
  }
  else
  {
    $new_width = $thumb_width;
    $new_height = $height / ($width / $thumb_width);
  }
  
  $thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
  
  imagecopyresampled($thumb, $image,
                 0, 0,
                 0, -$thumb_height,
                 $new_width, $new_height,
                 $width, $height);
  imagepng($thumb, $filename, 5);
  </code></pre>