/** * Generate cache key. * * @return */ default String getCacheKey() { StringBuilder builder = new StringBuilder(); if (getRect() != null) { builder.append("rect" + getRect().toString()); } if (getCropMode() != null) { builder.append("crop" + getCropMode()); } if (getWidth() != null) { builder.append("rw" + getWidth()); } if (getHeight() != null) { builder.append("rh" + getHeight()); } if (getFocalPoint() != null) { builder.append("fp" + getFocalPoint().toString()); } if (getFocalPointDebug()) { builder.append("fpdebug"); } if (getFocalPointZoom() != null) { builder.append("fpz" + getFocalPointZoom()); } return builder.toString(); }
/** * Check whether any resize or crop param has been set. * * @return */ default boolean hasResizeParams() { return getHeight() != null || getWidth() != null || getCropMode() != null; }
@Override public MeshRequest<NodeResponse> transformNodeBinaryField(String projectName, String nodeUuid, String languageTag, String version, String fieldKey, ImageManipulationParameters parameters) { Objects.requireNonNull(projectName, "projectName must not be null"); Objects.requireNonNull(nodeUuid, "nodeUuid must not be null"); Objects.requireNonNull(languageTag, "language must not be null"); Objects.requireNonNull(version, "version must not be null"); Objects.requireNonNull(fieldKey, "field key must not be null"); BinaryFieldTransformRequest transformRequest = new BinaryFieldTransformRequest(); transformRequest.setCropRect(parameters.getRect()); transformRequest.setWidth(parameters.getWidth()); transformRequest.setHeight(parameters.getHeight()); transformRequest.setLanguage(languageTag).setVersion(version); return prepareRequest(POST, "/" + encodeSegment(projectName) + "/nodes/" + nodeUuid + "/binaryTransform/" + fieldKey, NodeResponse.class, transformRequest); }
FocalPoint focalPoint = parameters.getFocalPoint(); if (focalPoint == null) { return img; if (parameters.getFocalPointDebug()) { drawFocusPointAxis(img, focalPoint); Point absFocalPoint = parameters.getFocalPoint().convertToAbsolutePoint(imageSize); if (!absFocalPoint.isWithinBoundsOf(imageSize)) { throw error(BAD_REQUEST, "image_error_focalpoint_out_of_bounds", focalPoint.toString(), imageSize.toString()); Point targetSize = parameters.getSize(); Float zoomFactor = parameters.getFocalPointZoom(); BufferedImage zoomedImg = applyZoom(img, zoomFactor, focalPoint, targetSize);
@Test public void testCacheKey() { String cacheKey = new ImageManipulationParametersImpl().getCacheKey(); assertEquals("fp0.5-0.5", cacheKey); cacheKey = new ImageManipulationParametersImpl().setWidth(100).setHeight(200).getCacheKey(); assertEquals("rw100rh200fp0.5-0.5", cacheKey); cacheKey = new ImageManipulationParametersImpl().setWidth(100).setHeight(200).setRect(10, 22, 20, 21).getCacheKey(); assertEquals("rect10,22,21,20rw100rh200fp0.5-0.5", cacheKey); }
/** * Resize the given image with the specified manipulation parameters. * * @param image The image to process * @param parameters The parameters defining cropping and resizing requests * @return The modified image */ private BufferedImage cropAndResize(BufferedImage image, ImageManipulationParameters parameters) { CropMode cropMode = parameters.getCropMode(); boolean omitResize = false; if (cropMode != null) { switch (cropMode) { case RECT: image = crop(image, parameters.getRect()); break; case FOCALPOINT: image = focalPointModifier.apply(image, parameters); // We don't need to resize the image again. The dimensions already match up with the target dimension omitResize = true; break; } } if (!omitResize) { image = resizeIfRequested(image, parameters); } return image; }
/** * Set the crop area. * * @param startX * @param startY * @param height * @param width * @return Fluent API */ default ImageManipulationParameters setRect(int startX, int startY, int height, int width) { ImageRect rect = new ImageRect(startX, startY, height, width); setRect(rect); return this; }
String baseName = "image-" + parameters.getCacheKey(); File[] foundFiles = baseFolder.listFiles((dir, name) -> name.startsWith(baseName)); int numFiles = foundFiles.length; "More than one cache file found:" + System.lineSeparator() + " hash: " + sha512sum + System.lineSeparator() + " key: " + parameters.getCacheKey() + System.lineSeparator() + " files:" + indent
/** * Check whether all required crop parameters have been set. * * @param options * @return Fluent API */ default ImageManipulationParameters validateLimits(ImageManipulatorOptions options) { if (getWidth() != null && options.getMaxWidth() != null && options.getMaxWidth() > 0 && getWidth() > options.getMaxWidth()) { throw error(BAD_REQUEST, "image_error_width_limit_exceeded", String.valueOf(options.getMaxWidth()), String.valueOf(getWidth())); } if (getHeight() != null && options.getMaxHeight() != null && options.getMaxHeight() > 0 && getHeight() > options.getMaxHeight()) { throw error(BAD_REQUEST, "image_error_height_limit_exceeded", String.valueOf(options.getMaxHeight()), String.valueOf(getHeight())); } return this; }