Skip to content

Commit 6a7fa23

Browse files
author
RTLcoil
authored
Support different radius for each corner (#212)
1 parent 4b98fd0 commit 6a7fa23

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

cloudinary-core/src/main/java/com/cloudinary/Transformation.java

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,70 @@ public T y(Object value) {
126126
return param("y", value);
127127
}
128128

129+
/**
130+
* Add rounding transformation.
131+
* <p>
132+
* Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched.
133+
*
134+
* @param value rounding radius for all four corners
135+
* @return updated transformation instance for chaining
136+
*/
129137
public T radius(Object value) {
130-
return param("radius", value);
138+
return radius(new Object[]{value});
139+
}
140+
141+
/**
142+
* Add rounding transformation.
143+
* <p>
144+
* Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched.
145+
*
146+
* @param topLeftBottomRight rounding radius for top-left and bottom-right corners
147+
* @param topRightBottomLeft rounding radius for top-right and bottom-left corners
148+
* @return updated transformation instance for chaining
149+
*/
150+
public T radius(Object topLeftBottomRight, Object topRightBottomLeft) {
151+
return radius(new Object[]{topLeftBottomRight, topRightBottomLeft});
152+
}
153+
154+
/**
155+
* Add rounding transformation.
156+
* <p>
157+
* Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched.
158+
*
159+
* @param topLeft rounding radius for top-left corner
160+
* @param topRightBottomLeft rounding radius for top-right and bottom-left corners
161+
* @param bottomRight rounding radius for bottom-right corner
162+
* @return updated transformation instance for chaining
163+
*/
164+
public T radius(Object topLeft, Object topRightBottomLeft, Object bottomRight) {
165+
return radius(new Object[]{topLeft, topRightBottomLeft, bottomRight});
166+
}
167+
168+
/**
169+
* Add rounding transformation.
170+
* <p>
171+
* Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched.
172+
*
173+
* @param topLeft rounding radius for top-left corner
174+
* @param topRight rounding radius for top-right corner
175+
* @param bottomRight rounding radius for bottom-right corner
176+
* @param bottomLeft rounding radius for bottom-left corner
177+
* @return updated transformation instance for chaining
178+
*/
179+
public T radius(Object topLeft, Object topRight, Object bottomRight, Object bottomLeft) {
180+
return radius(new Object[]{topLeft, topRight, bottomRight, bottomLeft});
181+
}
182+
183+
/**
184+
* Add rounding transformation.
185+
* <p>
186+
* Radius can be specified either as value in pixels or expression. Specify 0 to keep corner untouched.
187+
*
188+
* @param cornerRadiuses rounding radiuses for corners as array
189+
* @return updated transformation instance for chaining
190+
*/
191+
public T radius(Object[] cornerRadiuses) {
192+
return param("radius", cornerRadiuses);
131193
}
132194

133195
public T quality(Object value) {
@@ -694,7 +756,7 @@ public String generate(Map options) {
694756
params.put("h", Expression.normalize(height));
695757
params.put("o", Expression.normalize(options.get("opacity")));
696758
params.put("q", Expression.normalize(options.get("quality")));
697-
params.put("r", Expression.normalize(options.get("radius")));
759+
params.put("r", Expression.normalize(radiusToExpression((Object[]) options.get("radius"))));
698760
params.put("so", startOffset);
699761
params.put("t", namedTransformation);
700762
params.put("vc", videoCodec);
@@ -902,4 +964,22 @@ public T customFunction(CustomFunction action) {
902964
public T customPreFunction(CustomFunction action) {
903965
return param("custom_function", "pre:" + action.toString());
904966
}
967+
968+
private String radiusToExpression(Object[] radiusOption) {
969+
if (radiusOption == null) {
970+
return null;
971+
}
972+
973+
if (radiusOption.length == 0 || radiusOption.length > 4) {
974+
throw new IllegalArgumentException("Radius array should contain between 1 and 4 values");
975+
}
976+
977+
for (Object o : radiusOption) {
978+
if (o == null) {
979+
throw new IllegalArgumentException("Radius options array should not contain nulls");
980+
}
981+
}
982+
983+
return StringUtils.join(radiusOption, ":");
984+
}
905985
}

cloudinary-core/src/test/java/com/cloudinary/TransformationTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,52 @@ public void testShouldNotChangeVariableNamesWhenTheyNamedAfterKeyword() {
228228

229229
assertEquals("$width_10/w_$width_add_10_add_w", t.generate());
230230
}
231+
232+
@Test
233+
public void testRadiusTwoCornersAsValues() {
234+
Transformation t = new Transformation()
235+
.radius(10, 20);
236+
237+
assertEquals("r_10:20", t.generate());
238+
}
239+
240+
@Test
241+
public void testRadiusTwoCornersAsExpressions() {
242+
Transformation t = new Transformation()
243+
.radius("10", "$v");
244+
245+
assertEquals("r_10:$v", t.generate());
246+
}
247+
248+
@Test
249+
public void testRadiusThreeCorners() {
250+
Transformation t = new Transformation()
251+
.radius(10, "$v", "30");
252+
253+
assertEquals("r_10:$v:30", t.generate());
254+
}
255+
256+
@Test
257+
public void testRadiusFourCorners() {
258+
Transformation t = new Transformation()
259+
.radius(10, "$v", "30", 40);
260+
261+
assertEquals("r_10:$v:30:40", t.generate());
262+
}
263+
264+
@Test
265+
public void testRadiusArray1() {
266+
Transformation t = new Transformation()
267+
.radius(new Object[]{10});
268+
269+
assertEquals("r_10", t.generate());
270+
}
271+
272+
@Test
273+
public void testRadiusArray2() {
274+
Transformation t = new Transformation()
275+
.radius(new Object[]{10, "$v"});
276+
277+
assertEquals("r_10:$v", t.generate());
278+
}
231279
}

0 commit comments

Comments
 (0)