if (!StringUtils.equals(cropString, "-")) { try { cropDimension = CropDimension.fromCropString(cropString); layer.crop(cropDimension.getRectangle());
@Override public String getMediaPath(boolean contentDispositionAttachment) { return RenditionMetadata.buildMediaPath(getRendition().getPath() + "." + ImageFileServlet.SELECTOR + "." + getWidth() + "." + getHeight() + "." + (this.cropDimension != null ? this.cropDimension.getCropString() : "-") + (this.rotation != null ? "." + this.rotation : "") + (contentDispositionAttachment ? "." + MediaFileServlet.SELECTOR_DOWNLOAD : "") + "." + MediaFileServlet.EXTENSION, getFileName()); }
throw new IllegalArgumentException("Invalid crop string: '" + cropString + "'."); return new CropDimension(x1, y1, width, height);
/** * Get (optional) crop dimensions from resource * @param mediaRequest Media request * @param mediaHandlerConfig Media handler config (can be null, but should not be null) * @return Crop dimension or null if not set or invalid */ @SuppressWarnings("null") protected final @Nullable CropDimension getMediaCropDimension(@NotNull MediaRequest mediaRequest, @Nullable MediaHandlerConfig mediaHandlerConfig) { if (mediaRequest.getResource() != null) { String cropProperty = getMediaCropProperty(mediaRequest, mediaHandlerConfig); String cropString = mediaRequest.getResource().getValueMap().get(cropProperty, String.class); if (StringUtils.isNotEmpty(cropString)) { try { return CropDimension.fromCropString(cropString); } catch (IllegalArgumentException ex) { // ignore } } } return null; }
@Override protected Layer getLayer() { Layer layer = super.getLayer(); if (layer != null) { if (cropDimension != null) { layer.crop(cropDimension.getRectangle()); if (width <= layer.getWidth() && height <= layer.getHeight()) { layer.resize((int)width, (int)height); } } if (rotation != null) { layer.rotate(rotation); } } return layer; }
/** * Builds URL to rescaled version of the binary image. * @return Media URL */ private String buildScaledMediaUrl(Dimension dimension, CropDimension cropDimension) { String resourcePath = this.resource.getPath(); // if parent resource is a nt:file resource, use this one as path for scaled image Resource parentResource = this.resource.getParent(); if (JcrBinary.isNtFile(parentResource)) { resourcePath = parentResource.getPath(); } // URL to render scaled image via {@link InlineRenditionServlet} String path = resourcePath + "." + ImageFileServlet.SELECTOR + "." + dimension.getWidth() + "." + dimension.getHeight() + (cropDimension != null ? "." + cropDimension.getCropString() : "") + (this.mediaArgs.isContentDispositionAttachment() ? "." + MediaFileServlet.SELECTOR_DOWNLOAD : "") + "." + MediaFileServlet.EXTENSION + "/" // replace extension based on the format supported by ImageFileServlet for rendering for this rendition + ImageFileServlet.getImageFileName(getFileName()); // build externalized URL UrlHandler urlHandler = AdaptTo.notNull(this.adaptable, UrlHandler.class); return urlHandler.get(path).urlMode(this.mediaArgs.getUrlMode()).buildExternalResourceUrl(this.resource); }
/** * Searches for the biggest web-enabled rendition that matches the crop dimensions width and height or is bigger. * @param candidates * @return Rendition or null if no match found */ private VirtualTransformedRenditionMetadata getCropRendition(NavigableSet<RenditionMetadata> candidates) { RenditionMetadata original = getOriginalRendition(); if (original == null) { return null; } Double scaleFactor = getCropScaleFactor(candidates); CropDimension scaledCropDimension = new CropDimension( Math.round(cropDimension.getLeft() * scaleFactor), Math.round(cropDimension.getTop() * scaleFactor), Math.round(cropDimension.getWidth() * scaleFactor), Math.round(cropDimension.getHeight() * scaleFactor)); return new VirtualTransformedRenditionMetadata(original.getRendition(), rotateMapWidth(scaledCropDimension.getWidth(), scaledCropDimension.getHeight()), rotateMapHeight(scaledCropDimension.getWidth(), scaledCropDimension.getHeight()), scaledCropDimension, rotation); }