/** * 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); }
/** * {@inheritDoc} * * @see XWikiPluginInterface#getPluginApi(XWikiPluginInterface, XWikiContext) */ @Override public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) { return new ImagePluginAPI((ImagePlugin) plugin, 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; } }
boolean keepAspectRatio, float requestedQuality, XWikiContext context) throws Exception Image image = this.imageProcessor.readImage(attachment.getContentInputStream(context)); int currentHeight = image.getHeight(null); int[] dimensions = reduceImageDimensions(currentWidth, currentHeight, requestedWidth, requestedHeight, keepAspectRatio); RenderedImage shrunkImage = this.imageProcessor.scaleImage(image, dimensions[0], dimensions[1]); this.imageProcessor.writeImage(shrunkImage, attachment.getMimeType(context), quality, bout);
private void resizeImageToAttachment(InputStream imageFileInputStream, int width, int height, XWikiAttachment outputAttachment) throws IOException { OutputStream attachmentOutputStream = null; try { Image originalImage = imageProcessor.readImage(imageFileInputStream); RenderedImage resizedImage = imageProcessor.scaleImage(originalImage, width, height); attachmentOutputStream = outputAttachment.getAttachment_content().getContentOutputStream(); imageProcessor.writeImage(resizedImage, "image/png", 1.0f, attachmentOutputStream); } finally { // Close the attachment output stream since we are done writing to it. if (attachmentOutputStream != null) { IOUtils.closeQuietly(attachmentOutputStream); } } } }
/** * 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; } }
initCache(context); this.imageCache == null ? shrinkImage(image, width, height, keepAspectRatio, quality, context) : downloadImageFromCache(image, width, height, keepAspectRatio, quality, context);
/** * @param attachment an image attachment * @param context the XWiki context * @return the width of the specified image * @throws IOException if reading the image from the attachment content fails * @throws XWikiException if reading the attachment content fails */ public int getWidth(XWikiAttachment attachment, XWikiContext context) throws IOException, XWikiException { return this.imageProcessor.readImage(attachment.getContentInputStream(context)).getWidth(null); }
public XWikiAttachment downloadAttachment(XWikiAttachment attachment, XWikiContext context) if (!this.imageProcessor.isMimeTypeSupported(attachment.getMimeType(context))) { return attachment; return downloadImage(attachment, width, height, quality, context); } catch (Exception e) { LOG.warn("Failed to transform image attachment.", e);
/** * Output a BufferedImage object into the response outputstream. Once this method has been called, no further action * is possible. Users should set {@code $context.setFinished(true)} to avoid template output. The image is served as * image/jpeg. * * @param image the BufferedImage to output * @throws java.io.IOException if the output fails * @since 1.3 Milestone 2 */ public void outputImage(BufferedImage image) throws IOException { OutputStream ostream = getXWikiContext().getResponse().getOutputStream(); Utils.getComponent(ImageProcessor.class).writeImage(image, "image/jpeg", (float) 0.8, ostream); ostream.flush(); }
/** * @param pageName the name of a wiki page * @param attachmentName the name of an attachment of the specified page * @return the specified attachment * @throws XWikiException if retrieving the attachment fails */ private XWikiAttachment getAttachment(String pageName, String attachmentName) throws XWikiException { DocumentReference documentReference = currentMixedDocumentReferenceResolver.resolve(pageName); XWikiDocument document = getXWikiContext().getWiki().getDocument(documentReference, getXWikiContext()); return document.getAttachment(attachmentName); } }
@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); } } }
thumbnail.setContent(new ByteArrayInputStream(data), data.length); } else { thumbnail = shrinkImage(image, width, height, keepAspectRatio, quality, context); this.imageCache.set(key, thumbnail.getContent(context));
/** * @param attachment an image attachment * @param context the XWiki context * @return the height of the specified image * @throws IOException if reading the image from the attachment content fails * @throws XWikiException if reading the attachment content fails */ public int getHeight(XWikiAttachment attachment, XWikiContext context) throws IOException, XWikiException { return this.imageProcessor.readImage(attachment.getContentInputStream(context)).getHeight(null); } }