/** * Creates a new instance of this plugin. * * @param name the name of the plugin * @param className the class name * @param context the XWiki context * @see XWikiDefaultPlugin#XWikiDefaultPlugin(String,String,com.xpn.xwiki.XWikiContext) */ public ImagePlugin(String name, String className, XWikiContext context) { super(name, className, context); init(context); }
@Override public void init(XWikiContext context) { super.init(context); initCache(context); String defaultQualityParam = context.getWiki().Param("xwiki.plugin.image.defaultQuality"); if (!StringUtils.isBlank(defaultQualityParam) && StringUtils.isNumeric(defaultQualityParam.trim())) { try { this.defaultQuality = Math.max(0, Math.min(1, Float.parseFloat(defaultQualityParam.trim()))); } catch (NumberFormatException e) { LOG.warn(String.format("Failed to parse xwiki.plugin.image.defaultQuality configuration parameter. " + "Using %s as the default image quality.", this.defaultQuality), e); } } }
initCache(context); this.imageCache == null ? shrinkImage(image, width, height, keepAspectRatio, quality, context) : downloadImageFromCache(image, width, height, keepAspectRatio, quality, context);
/** * Detects the height of an image attached to a wiki page. * * @param pageName the name of a wiki page * @param attachmentName the name of an image attached to the specified page * @return the height of the specified image */ public int getHeight(String pageName, String attachmentName) { try { return getProtectedPlugin().getHeight(getAttachment(pageName, attachmentName), getXWikiContext()); } catch (Exception e) { LOG.error(String.format("Failed to detect the height of %s attached to %s.", attachmentName, pageName), e); return -1; } }
/** * Detects the width of an image attached to a wiki page. * * @param pageName the name of a wiki page * @param attachmentName the name of an image attached to the specified page * @return the width of the specified image */ public int getWidth(String pageName, String attachmentName) { try { return getProtectedPlugin().getWidth(getAttachment(pageName, attachmentName), getXWikiContext()); } catch (Exception e) { LOG.error(String.format("Failed to detect the width of %s attached to %s.", attachmentName, pageName), e); return -1; } }
int currentHeight = image.getHeight(null); int[] dimensions = reduceImageDimensions(currentWidth, currentHeight, requestedWidth, requestedHeight, keepAspectRatio);
return downloadImage(attachment, width, height, quality, context); } catch (Exception e) { LOG.warn("Failed to transform image attachment.", e);
thumbnail.setContent(new ByteArrayInputStream(data), data.length); } else { thumbnail = shrinkImage(image, width, height, keepAspectRatio, quality, context); this.imageCache.set(key, thumbnail.getContent(context));