code: public static int[] getNewSize(
          int width,
          int height,
          int maxWidth,
          int maxHeight)
      {
          if (width <= maxWidth && height <= maxHeight) {
              return new int[]{width, height};
          }
          double thumbRatio = (double) maxWidth / (double) maxHeight;
          double imageRatio = (double) width / (double) height;
          if (thumbRatio < imageRatio) {
              maxHeight = (int) (maxWidth / imageRatio);
          } else {
              maxWidth = (int) (maxHeight * imageRatio);
          }
          return new int[]{maxWidth, maxHeight};
      }
docs: Calculates new size of an image with the specified max borders keeping
  the ratio between height and width of the image.
  
  @param width the initial width of an image
  @param height the initial height of an image
  @param maxWidth the maximal width of an image
  @param maxHeight the maximal height of an image
  @return a new size of an image where the height and width don't overcome
  the specified borders; the size keeps the initial image ratio
  between width and height