diff --git a/cloudinary-core/src/main/java/com/cloudinary/Transformation.java b/cloudinary-core/src/main/java/com/cloudinary/Transformation.java index 7ff5d93c..78330c2f 100644 --- a/cloudinary-core/src/main/java/com/cloudinary/Transformation.java +++ b/cloudinary-core/src/main/java/com/cloudinary/Transformation.java @@ -126,8 +126,70 @@ public T y(Object value) { return param("y", value); } + /** + * Add rounding transformation. + *
+ * Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched. + * + * @param value rounding radius for all four corners + * @return updated transformation instance for chaining + */ public T radius(Object value) { - return param("radius", value); + return radius(new Object[]{value}); + } + + /** + * Add rounding transformation. + *
+ * Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched. + * + * @param topLeftBottomRight rounding radius for top-left and bottom-right corners + * @param topRightBottomLeft rounding radius for top-right and bottom-left corners + * @return updated transformation instance for chaining + */ + public T radius(Object topLeftBottomRight, Object topRightBottomLeft) { + return radius(new Object[]{topLeftBottomRight, topRightBottomLeft}); + } + + /** + * Add rounding transformation. + *
+ * Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched. + * + * @param topLeft rounding radius for top-left corner + * @param topRightBottomLeft rounding radius for top-right and bottom-left corners + * @param bottomRight rounding radius for bottom-right corner + * @return updated transformation instance for chaining + */ + public T radius(Object topLeft, Object topRightBottomLeft, Object bottomRight) { + return radius(new Object[]{topLeft, topRightBottomLeft, bottomRight}); + } + + /** + * Add rounding transformation. + *
+ * Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched. + * + * @param topLeft rounding radius for top-left corner + * @param topRight rounding radius for top-right corner + * @param bottomRight rounding radius for bottom-right corner + * @param bottomLeft rounding radius for bottom-left corner + * @return updated transformation instance for chaining + */ + public T radius(Object topLeft, Object topRight, Object bottomRight, Object bottomLeft) { + return radius(new Object[]{topLeft, topRight, bottomRight, bottomLeft}); + } + + /** + * Add rounding transformation. + *
+ * Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched. + * + * @param cornerRadiuses rounding radiuses for corners as array + * @return updated transformation instance for chaining + */ + public T radius(Object[] cornerRadiuses) { + return param("radius", cornerRadiuses); } public T quality(Object value) { @@ -694,7 +756,7 @@ public String generate(Map options) { params.put("h", Expression.normalize(height)); params.put("o", Expression.normalize(options.get("opacity"))); params.put("q", Expression.normalize(options.get("quality"))); - params.put("r", Expression.normalize(options.get("radius"))); + params.put("r", Expression.normalize(radiusToExpression((Object[]) options.get("radius")))); params.put("so", startOffset); params.put("t", namedTransformation); params.put("vc", videoCodec); @@ -902,4 +964,22 @@ public T customFunction(CustomFunction action) { public T customPreFunction(CustomFunction action) { return param("custom_function", "pre:" + action.toString()); } + + private String radiusToExpression(Object[] radiusOption) { + if (radiusOption == null) { + return null; + } + + if (radiusOption.length == 0 || radiusOption.length > 4) { + throw new IllegalArgumentException("Radius array should contain between 1 and 4 values"); + } + + for (Object o : radiusOption) { + if (o == null) { + throw new IllegalArgumentException("Radius options array should not contain nulls"); + } + } + + return StringUtils.join(radiusOption, ":"); + } } diff --git a/cloudinary-core/src/test/java/com/cloudinary/TransformationTest.java b/cloudinary-core/src/test/java/com/cloudinary/TransformationTest.java index db7ebd36..cadb8a97 100644 --- a/cloudinary-core/src/test/java/com/cloudinary/TransformationTest.java +++ b/cloudinary-core/src/test/java/com/cloudinary/TransformationTest.java @@ -228,4 +228,52 @@ public void testShouldNotChangeVariableNamesWhenTheyNamedAfterKeyword() { assertEquals("$width_10/w_$width_add_10_add_w", t.generate()); } + + @Test + public void testRadiusTwoCornersAsValues() { + Transformation t = new Transformation() + .radius(10, 20); + + assertEquals("r_10:20", t.generate()); + } + + @Test + public void testRadiusTwoCornersAsExpressions() { + Transformation t = new Transformation() + .radius("10", "$v"); + + assertEquals("r_10:$v", t.generate()); + } + + @Test + public void testRadiusThreeCorners() { + Transformation t = new Transformation() + .radius(10, "$v", "30"); + + assertEquals("r_10:$v:30", t.generate()); + } + + @Test + public void testRadiusFourCorners() { + Transformation t = new Transformation() + .radius(10, "$v", "30", 40); + + assertEquals("r_10:$v:30:40", t.generate()); + } + + @Test + public void testRadiusArray1() { + Transformation t = new Transformation() + .radius(new Object[]{10}); + + assertEquals("r_10", t.generate()); + } + + @Test + public void testRadiusArray2() { + Transformation t = new Transformation() + .radius(new Object[]{10, "$v"}); + + assertEquals("r_10:$v", t.generate()); + } } \ No newline at end of file