private Optional<Thumbnail> findBestFit(Image image, Integer width, Integer height) {
    if (width == null || height == null) {
      // First handle the case where we have only one dimension width or height
      for (Thumbnail thumbnail : image.getThumbnails()) {
        if ((thumbnail.getRatio().equals("1:0") && height == null)
            || (thumbnail.getRatio().equals("0:1") && width == null)) {
          return Optional.of(thumbnail);
        }
      }
      return Optional.absent();
    }

    // Then handle the general case where we have both dimensions width and height
    Thumbnail foundRatio = null;
    String expectedRatio = ImageUtils.imageRatio(width, height);

    for (Thumbnail thumbnail : image.getThumbnails()) {
      if (thumbnail.getRatio().equals(expectedRatio)) {
        if (thumbnail.getWidth().equals(width) && thumbnail.getHeight().equals(height)) {
          // Exact match, stop searching
          return Optional.of(thumbnail);
        } else {
          // Ratio match, keep searching
          foundRatio = thumbnail;
        }
      }
    }
    return Optional.fromNullable(foundRatio);
  }
  public ImageContext createImageContext(Image image, boolean featured) {
    ImageContext context =
        new ImageContext(
            "/attachments/"
                + image.getAttachment().getSlug()
                + "."
                + image.getAttachment().getExtension());

    context.setTitle(ContextUtils.safeString(image.getAttachment().getTitle()));
    context.setDescription(ContextUtils.safeString(image.getAttachment().getDescription()));
    context.put("featured", featured);
    context.put(
        "url",
        MessageFormat.format(
            "/images/{0}.{1}",
            image.getAttachment().getSlug(), image.getAttachment().getExtension()));

    if (theme != null && theme.getImageFormats().size() > 0) {
      for (String dimensionName : theme.getImageFormats().keySet()) {
        ImageFormatDefinition definition = theme.getImageFormats().get(dimensionName);
        Optional<Thumbnail> bestFit =
            findBestFit(image, definition.getWidth(), definition.getHeight());

        if (bestFit.isPresent()) {
          String url =
              MessageFormat.format(
                  "/images/thumbnails/{0}_{1,number,#}_{2,number,#}_{3,number,#}_{4,number,#}.{5}"
                      + "?width={6,number,#}&height={7,number,#}",
                  image.getAttachment().getSlug(),
                  bestFit.get().getX(),
                  bestFit.get().getY(),
                  bestFit.get().getWidth(),
                  bestFit.get().getHeight(),
                  image.getAttachment().getExtension(),
                  definition.getWidth(),
                  definition.getHeight());
          context.put("theme_" + dimensionName + "_url", url);
        } else {
          String url =
              MessageFormat.format(
                  "/images/{0}.{1}?width={2,number,#}&height={3,number,#}",
                  image.getAttachment().getSlug(),
                  image.getAttachment().getExtension(),
                  definition.getWidth(),
                  definition.getHeight());
          context.put("theme_" + dimensionName + "_url", url);
        }
      }
    }
    return context;
  }