title: Most efficent way to scale down an image while maintaining aspect ratio?
body: <p>So I have a field where my image displays and my max height can be 375 and max width is 775.  I want it to be as close to as one of these values as possible to get the maximum size while maintaining the aspect ratio  This is what I came up with and it actually seems to work pretty well but I am thinking there is a better way I am not thinking of.</p>
  
  <pre><code>    InputStream in = new ByteArrayInputStream(fileData);
      BufferedImage buf = ImageIO.read(in);
  
      int maxWidth = 775;
      int maxHeight = 375;
      int newWidth;
      int newHeight;
      float height = buf.getHeight();
      float width = buf.getWidth();
      float ratio = (float) 0.0;
  
      if(height &gt; maxHeight || width &gt; maxWidth)
      {
  
       if (height &gt; width)
       {
          ratio = (height/width);
       }
       else if(width &gt; height)
           ratio = (width/height);
       while(height &gt; maxHeight || width &gt; maxWidth)
       {
          if (height &gt; width)
           {              
              height -= ratio;
              width -= 1;
           }
  
           else if(width &gt; height)
           {
                  width -= ratio; 
                  height -= 1;                                    
           }
  
      }
      }
      newWidth = (int) width;
      newHeight = (int) height;
  
      // Call method to scale image to appropriate size
      byte[] newByte = scale(fileData, newWidth, newHeight);
  </code></pre>