title: Keeping aspect ration when resizing images using WideImage PHP library
body: <p>I am looking to resize an image to a pre-defined maximum size keeping the aspect ratio with the help of WideImage Library.</p>
  
  <p><strong>Example:</strong></p>
  
  <p>Maximum Allowed Dimesions: 200x200 </p>
  
  <p>Input Image Dimension: 300x200 logo (1,5:1 aspect ratio), </p>
  
  <p>Current Output Image Dimension: 200x200</p>
  
  <p>Expected Output Image Dimension : 200x133 (1,5:1 aspect ratio).</p>
  
  <p>Currently the images are distorted as aspect ratio is changed. What should be done to keep that aspect ratio?</p>
  
  <p>I am using the code found below.</p>
  
  <pre><code>$targetWidth = 200;
  $targetHeight = 200;
  
  $sourceRatio = $sourceWidth / $sourceHeight;
  $targetRatio = $targetWidth / $targetHeight;
  
  if ( $sourceRatio &lt; $targetRatio ) {
      $scale = $sourceWidth / $targetWidth;
  } else {
      $scale = $sourceHeight / $targetHeight;
  }
  
  $resizeWidth = (int)($sourceWidth / $scale);
  $resizeHeight = (int)($sourceHeight / $scale);
  
  
  $img-&gt;resize($resizeWidth, $resizeHeight)
  </code></pre>
  
  <p>PS: I got the above logic from <a href="https://stackoverflow.com/questions/6355954/resize-image-without-distortion-keeping-aspect-ratio-then-crop-excess-using-wide">Resize image without distortion keeping aspect ratio then crop excess using WideImage</a></p>