镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
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
2 changes: 1 addition & 1 deletion docs/src/content/docs/api-resize.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` pro
| [options.background] | <code>string</code> \| <code>Object</code> | <code>&quot;&#x27;top-left pixel&#x27;&quot;</code> | Background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to that of the top-left pixel. |
| [options.threshold] | <code>number</code> | <code>10</code> | Allowed difference from the above colour, a positive number. |
| [options.lineArt] | <code>boolean</code> | <code>false</code> | Does the input more closely resemble line art (e.g. vector) rather than being photographic? |
| [options.margin] | <code>number</code> | <code>0</code> | Leave a margin around trimmed content, value is in pixels. |
| [options.margin] | <code>number</code> | <code>0</code> | Leave a margin around trimmed content, integral number of pixels between 0 and 10000000. |

**Example**
```js
Expand Down
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,7 @@ declare namespace sharp {
threshold?: number | undefined;
/** Does the input more closely resemble line art (e.g. vector) rather than being photographic? (optional, default false) */
lineArt?: boolean | undefined;
/** Leave a margin around trimmed content, value is in pixels. (optional, default 0) */
/** Leave a margin around trimmed content, integral number of pixels between 0 and 10000000. (optional, default 0) */
margin?: number | undefined;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/resize.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ function extract (options) {
* @param {string|Object} [options.background='top-left pixel'] - Background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to that of the top-left pixel.
* @param {number} [options.threshold=10] - Allowed difference from the above colour, a positive number.
* @param {boolean} [options.lineArt=false] - Does the input more closely resemble line art (e.g. vector) rather than being photographic?
* @param {number} [options.margin=0] - Leave a margin around trimmed content, value is in pixels.
* @param {number} [options.margin=0] - Leave a margin around trimmed content, integral number of pixels between 0 and 10000000.
* @returns {Sharp}
* @throws {Error} Invalid parameters
*/
Expand All @@ -580,10 +580,10 @@ function trim (options) {
this._setBooleanOption('trimLineArt', options.lineArt);
}
if (is.defined(options.margin)) {
if (is.integer(options.margin) && options.margin >= 0) {
if (is.integer(options.margin) && is.inRange(options.margin, 0, 10000000)) {
this.options.trimMargin = options.margin;
} else {
throw is.invalidParameterError('margin', 'positive integer', options.margin);
throw is.invalidParameterError('margin', 'integer between 0 and 10000000', options.margin);
}
}
} else {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/trim.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ suite('Trim borders', () => {
},
'Invalid margin': {
margin: -1
},
'Oversized margin': {
margin: 2 ** 30
}
}).forEach(([description, parameter]) => {
test(description, (t) => {
Expand Down