Skip to content

Commit e287225

Browse files
RTLcoilstrausr
authored andcommitted
Support different radius for each corner (containing images and overlays) (#260)
1 parent f88ce96 commit e287225

File tree

4 files changed

+109
-3
lines changed

4 files changed

+109
-3
lines changed

lib-es5/utils/index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,32 @@ function process_layer(layer) {
320320
return result;
321321
}
322322

323+
/**
324+
* Parse radius options
325+
* @private
326+
* @param {Array<string|number>|string|number} radius The radius to parse
327+
* @return {string} radius transformation string
328+
*/
329+
function process_radius(radius) {
330+
if (!radius) {
331+
return radius;
332+
}
333+
if (!isArray(radius)) {
334+
radius = [radius];
335+
}
336+
if (radius.length === 0 || radius.length > 4) {
337+
throw new Error("Radius array should contain between 1 and 4 values");
338+
}
339+
if (radius.findIndex(function (x) {
340+
return x === null;
341+
}) >= 0) {
342+
throw new Error("Corner: Cannot be null");
343+
}
344+
return radius.map(normalize_expression).join(':');
345+
}
346+
323347
function base64EncodeURL(sourceUrl) {
348+
var ignore;
324349
try {
325350
sourceUrl = decodeURI(sourceUrl);
326351
} catch (error) {
@@ -554,6 +579,7 @@ function generate_transformation_string(options) {
554579
options.end_offset = _split_range2[1];
555580
}
556581
var overlay = process_layer(utils.option_consume(options, "overlay"));
582+
var radius = process_radius(utils.option_consume(options, "radius"));
557583
var underlay = process_layer(utils.option_consume(options, "underlay"));
558584
var ifValue = process_if(utils.option_consume(options, "if"));
559585
var custom_function = process_custom_function(utils.option_consume(options, "custom_function"));
@@ -579,7 +605,7 @@ function generate_transformation_string(options) {
579605
l: overlay,
580606
o: normalize_expression(utils.option_consume(options, "opacity")),
581607
q: normalize_expression(utils.option_consume(options, "quality")),
582-
r: normalize_expression(utils.option_consume(options, "radius")),
608+
r: radius,
583609
t: named_transformation,
584610
u: underlay,
585611
w: normalize_expression(width),

lib/utils/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,28 @@ function process_layer(layer) {
312312
return result;
313313
}
314314

315+
/**
316+
* Parse radius options
317+
* @private
318+
* @param {Array<string|number>|string|number} radius The radius to parse
319+
* @return {string} radius transformation string
320+
*/
321+
function process_radius(radius) {
322+
if (!radius) {
323+
return radius;
324+
}
325+
if (!isArray(radius)) {
326+
radius = [radius];
327+
}
328+
if (radius.length === 0 || radius.length > 4) {
329+
throw new Error("Radius array should contain between 1 and 4 values");
330+
}
331+
if (radius.findIndex(x => x === null) >= 0) {
332+
throw new Error("Corner: Cannot be null");
333+
}
334+
return radius.map(normalize_expression).join(':');
335+
}
336+
315337
function base64EncodeURL(sourceUrl) {
316338
try {
317339
sourceUrl = decodeURI(sourceUrl);
@@ -550,6 +572,7 @@ function generate_transformation_string(options) {
550572
[options.start_offset, options.end_offset] = split_range(utils.option_consume(options, "offset"));
551573
}
552574
let overlay = process_layer(utils.option_consume(options, "overlay"));
575+
let radius = process_radius(utils.option_consume(options, "radius"));
553576
let underlay = process_layer(utils.option_consume(options, "underlay"));
554577
let ifValue = process_if(utils.option_consume(options, "if"));
555578
let custom_function = process_custom_function(utils.option_consume(options, "custom_function"));
@@ -575,7 +598,7 @@ function generate_transformation_string(options) {
575598
l: overlay,
576599
o: normalize_expression(utils.option_consume(options, "opacity")),
577600
q: normalize_expression(utils.option_consume(options, "quality")),
578-
r: normalize_expression(utils.option_consume(options, "radius")),
601+
r: radius,
579602
t: named_transformation,
580603
u: underlay,
581604
w: normalize_expression(width),

test/cloudinary_spec.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,44 @@ describe("cloudinary", function () {
180180
}), `${upload_path}/g_center,p_a,q_auto:good,r_3,x_1,y_2/test`);
181181
});
182182
});
183-
it("should support named transformation", function () {
183+
describe(":radius", function() {
184+
const upload_path = 'http://res.cloudinary.com/test123/image/upload';
185+
it("should support a single value", function() {
186+
expect(cloudinary.utils.url("test", {
187+
radius: 10
188+
})).to.eql(`${upload_path}/r_10/test`);
189+
expect(cloudinary.utils.url("test", {
190+
radius: '10'
191+
})).to.eql(`${upload_path}/r_10/test`);
192+
expect(cloudinary.utils.url("test", {
193+
variables: [['$v', 10]],
194+
radius: '$v',
195+
})).to.eql(`${upload_path}/$v_10,r_$v/test`);
196+
});
197+
it("should support an array of values", function() {
198+
expect(cloudinary.utils.url("test", {
199+
radius: [10,20,30]
200+
})).to.eql(`${upload_path}/r_10:20:30/test`);
201+
expect(cloudinary.utils.url("test", {
202+
variables: [['$v', 10]],
203+
radius: [10,20,'$v'],
204+
})).to.eql(`${upload_path}/$v_10,r_10:20:$v/test`);
205+
expect(cloudinary.utils.url("test", {
206+
variables: [['$v', 10]],
207+
radius: [10,20,'$v',40],
208+
})).to.eql(`${upload_path}/$v_10,r_10:20:$v:40/test`);
209+
})
210+
it("should support colon separated values", function() {
211+
expect(cloudinary.utils.url("test", {
212+
radius: "10:20"
213+
})).to.eql(`${upload_path}/r_10:20/test`);
214+
expect(cloudinary.utils.url("test", {
215+
variables: [['$v', 10]],
216+
radius: "10:20:$v:40"
217+
})).to.eql(`${upload_path}/$v_10,r_10:20:$v:40/test`);
218+
})
219+
})
220+
it("should support named transformation", function() {
184221
var options, result;
185222
options = {
186223
transformation: "blip",

test/utils_spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,25 @@ describe("utils", function () {
343343
angle: 'auto',
344344
}, `http://res.cloudinary.com/${cloud_name}/image/upload/a_auto,c_scale,h_100,w_100/test`, {});
345345
});
346+
it("should disallow radius arrays that contain 0 or more than 4 values", function () {
347+
expect(function () {
348+
return utils.url("test", {
349+
radius: [10, 20, 30, 10, 20]
350+
});
351+
}).to.throwError(/Radius array should contain between 1 and 4 values/);
352+
expect(function () {
353+
return utils.url("test", {
354+
radius: []
355+
});
356+
}).to.throwError(/Radius array should contain between 1 and 4 values/);
357+
});
358+
it("should disallow radius arrays containing null values", function () {
359+
expect(function () {
360+
return utils.url("test", {
361+
radius: [null, 20, 30, 10]
362+
});
363+
}).to.throwError(/Corner: Cannot be null/);
364+
});
346365
it("should use x, y, radius, prefix, gravity and quality from options", function () {
347366
test_cloudinary_url("test", {
348367
x: 1,
@@ -1520,3 +1539,4 @@ describe("utils", function () {
15201539
});
15211540
});
15221541
});
1542+

0 commit comments

Comments
 (0)