Skip to content

Commit 6147491

Browse files
committedMar 25, 2021
Extend: default missing edge props to zero #2578
1 parent f1f18fb commit 6147491

File tree

4 files changed

+85
-28
lines changed

4 files changed

+85
-28
lines changed
 

‎docs/api-resize.md

+14-4
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ This operation will always occur after resizing and extraction, if any.
137137
### Parameters
138138

139139
- `extend` **([number][8] \| [Object][9])** single pixel count to add to all edges or an Object with per-edge counts
140-
- `extend.top` **[number][8]?**
141-
- `extend.left` **[number][8]?**
142-
- `extend.bottom` **[number][8]?**
143-
- `extend.right` **[number][8]?**
140+
- `extend.top` **[number][8]** (optional, default `0`)
141+
- `extend.left` **[number][8]** (optional, default `0`)
142+
- `extend.bottom` **[number][8]** (optional, default `0`)
143+
- `extend.right` **[number][8]** (optional, default `0`)
144144
- `extend.background` **([String][10] \| [Object][9])** background colour, parsed by the [color][11] module, defaults to black without transparency. (optional, default `{r:0,g:0,b:0,alpha:1}`)
145145

146146
### Examples
@@ -160,6 +160,16 @@ sharp(input)
160160
...
161161
```
162162

163+
```javascript
164+
// Add a row of 10 red pixels to the bottom
165+
sharp(input)
166+
.extend({
167+
bottom: 10,
168+
background: 'red'
169+
})
170+
...
171+
```
172+
163173
- Throws **[Error][13]** Invalid parameters
164174

165175
Returns **Sharp**

‎docs/changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Requires libvips v8.10.6
1616

1717
* Reduce concurrency on glibc-based Linux when using the default memory allocator to help prevent fragmentation.
1818

19+
* Default missing edge properties of extend operation to zero.
20+
[#2578](https://github.com/lovell/sharp/issues/2578)
21+
1922
* Ensure composite does not clip top and left offsets.
2023
[#2594](https://github.com/lovell/sharp/pull/2594)
2124
[@SHG42](https://github.com/SHG42)

‎lib/resize.js

+42-15
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,20 @@ function resize (width, height, options) {
302302
* })
303303
* ...
304304
*
305+
* @example
306+
* // Add a row of 10 red pixels to the bottom
307+
* sharp(input)
308+
* .extend({
309+
* bottom: 10,
310+
* background: 'red'
311+
* })
312+
* ...
313+
*
305314
* @param {(number|Object)} extend - single pixel count to add to all edges or an Object with per-edge counts
306-
* @param {number} [extend.top]
307-
* @param {number} [extend.left]
308-
* @param {number} [extend.bottom]
309-
* @param {number} [extend.right]
315+
* @param {number} [extend.top=0]
316+
* @param {number} [extend.left=0]
317+
* @param {number} [extend.bottom=0]
318+
* @param {number} [extend.right=0]
310319
* @param {String|Object} [extend.background={r: 0, g: 0, b: 0, alpha: 1}] - background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black without transparency.
311320
* @returns {Sharp}
312321
* @throws {Error} Invalid parameters
@@ -317,17 +326,35 @@ function extend (extend) {
317326
this.options.extendBottom = extend;
318327
this.options.extendLeft = extend;
319328
this.options.extendRight = extend;
320-
} else if (
321-
is.object(extend) &&
322-
is.integer(extend.top) && extend.top >= 0 &&
323-
is.integer(extend.bottom) && extend.bottom >= 0 &&
324-
is.integer(extend.left) && extend.left >= 0 &&
325-
is.integer(extend.right) && extend.right >= 0
326-
) {
327-
this.options.extendTop = extend.top;
328-
this.options.extendBottom = extend.bottom;
329-
this.options.extendLeft = extend.left;
330-
this.options.extendRight = extend.right;
329+
} else if (is.object(extend)) {
330+
if (is.defined(extend.top)) {
331+
if (is.integer(extend.top) && extend.top >= 0) {
332+
this.options.extendTop = extend.top;
333+
} else {
334+
throw is.invalidParameterError('top', 'positive integer', extend.top);
335+
}
336+
}
337+
if (is.defined(extend.bottom)) {
338+
if (is.integer(extend.bottom) && extend.bottom >= 0) {
339+
this.options.extendBottom = extend.bottom;
340+
} else {
341+
throw is.invalidParameterError('bottom', 'positive integer', extend.bottom);
342+
}
343+
}
344+
if (is.defined(extend.left)) {
345+
if (is.integer(extend.left) && extend.left >= 0) {
346+
this.options.extendLeft = extend.left;
347+
} else {
348+
throw is.invalidParameterError('left', 'positive integer', extend.left);
349+
}
350+
}
351+
if (is.defined(extend.right)) {
352+
if (is.integer(extend.right) && extend.right >= 0) {
353+
this.options.extendRight = extend.right;
354+
} else {
355+
throw is.invalidParameterError('right', 'positive integer', extend.right);
356+
}
357+
}
331358
this._setBackgroundColourOption('extendBackground', extend.background);
332359
} else {
333360
throw is.invalidParameterError('extend', 'integer or object', extend);

‎test/unit/extend.js

+26-9
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ describe('Extend', function () {
4141
.resize(120)
4242
.extend({
4343
top: 50,
44-
bottom: 0,
4544
left: 10,
4645
right: 35,
4746
background: { r: 0, g: 0, b: 0, alpha: 0 }
@@ -64,18 +63,38 @@ describe('Extend', function () {
6463
sharp().extend(-1);
6564
});
6665
});
67-
it('partial object fails', function () {
68-
assert.throws(function () {
69-
sharp().extend({ top: 1 });
70-
});
66+
it('invalid top fails', () => {
67+
assert.throws(
68+
() => sharp().extend({ top: 'fail' }),
69+
/Expected positive integer for top but received fail of type string/
70+
);
71+
});
72+
it('invalid bottom fails', () => {
73+
assert.throws(
74+
() => sharp().extend({ bottom: -1 }),
75+
/Expected positive integer for bottom but received -1 of type number/
76+
);
77+
});
78+
it('invalid left fails', () => {
79+
assert.throws(
80+
() => sharp().extend({ left: 0.1 }),
81+
/Expected positive integer for left but received 0.1 of type number/
82+
);
83+
});
84+
it('invalid right fails', () => {
85+
assert.throws(
86+
() => sharp().extend({ right: {} }),
87+
/Expected positive integer for right but received \[object Object\] of type object/
88+
);
89+
});
90+
it('can set all edges apart from right', () => {
91+
assert.doesNotThrow(() => sharp().extend({ top: 1, left: 2, bottom: 3 }));
7192
});
7293

7394
it('should add alpha channel before extending with a transparent Background', function (done) {
7495
sharp(fixtures.inputJpgWithLandscapeExif1)
7596
.extend({
76-
top: 0,
7797
bottom: 10,
78-
left: 0,
7998
right: 10,
8099
background: { r: 0, g: 0, b: 0, alpha: 0 }
81100
})
@@ -91,9 +110,7 @@ describe('Extend', function () {
91110
it('PNG with 2 channels', function (done) {
92111
sharp(fixtures.inputPngWithGreyAlpha)
93112
.extend({
94-
top: 0,
95113
bottom: 20,
96-
left: 0,
97114
right: 20,
98115
background: 'transparent'
99116
})

0 commit comments

Comments
 (0)
Please sign in to comment.