Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 82 additions & 2 deletions cloudinary-core/src/main/java/com/cloudinary/Transformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,70 @@ public T y(Object value) {
return param("y", value);
}

/**
* Add rounding transformation.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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, ":");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}