Package net.pterodactylus.sone.data

Examples of net.pterodactylus.sone.data.Image


  /**
   * {@inheritDoc}
   */
  @Override
  public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
    Image image = null;
    if (data instanceof String) {
      image = core.getImage((String) data, false);
    } else if (data instanceof Image) {
      image = (Image) data;
    }
    if (image == null) {
      return null;
    }
    String imageClass = String.valueOf(parameters.get("class"));
    int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE);
    int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE);
    String mode = String.valueOf(parameters.get("mode"));
    String title = String.valueOf(parameters.get("title"));

    TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
    linkTemplateContext.set("class", imageClass);
    if (image.isInserted()) {
      linkTemplateContext.set("src", "/" + image.getKey());
      linkTemplateContext.set("forceDownload", true);
    } else {
      linkTemplateContext.set("src", "getImage.html?image=" + image.getId());
    }
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();
    if ("enlarge".equals(mode)) {
      double scale = Math.max(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
      linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
      linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
      linkTemplateContext.set("left", String.format("%dpx", (int) (maxWidth - (imageWidth * scale)) / 2));
      linkTemplateContext.set("top", String.format("%dpx", (int) (maxHeight - (imageHeight * scale)) / 2));
    } else {
      double scale = 1;
      if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) {
        scale = Math.min(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
      }
      linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
      linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
    }
    linkTemplateContext.set("alt", Optional.fromNullable(title).or(image.getDescription()));
    linkTemplateContext.set("title", Optional.fromNullable(title).or(image.getTitle()));

    StringWriter stringWriter = new StringWriter();
    linkTemplate.render(linkTemplateContext, stringWriter);
    return stringWriter.toString();
  }
View Full Code Here

TOP

Related Classes of net.pterodactylus.sone.data.Image

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.