Add support to extend for extendWith, allows copy/mirror/repeat (#3556)

This commit is contained in:
Tomasz Janowski
2023-02-18 01:01:24 +11:00
committed by GitHub
parent ebf4ccd124
commit 6f0e6f2e65
25 changed files with 134 additions and 65 deletions

View File

@@ -197,6 +197,7 @@ const Sharp = function (input, options) {
extendLeft: 0,
extendRight: 0,
extendBackground: [0, 0, 0, 255],
extendWith: 'background',
withoutEnlargement: false,
withoutReduction: false,
affineMatrix: [],

6
lib/index.d.ts vendored
View File

@@ -755,7 +755,7 @@ declare namespace sharp {
resize(options: ResizeOptions): Sharp;
/**
* Extends/pads the edges of the image with the provided background colour.
* Extends/pads the edges of the image with either the provided background colour or pixels derived from the image.
* This operation will always occur after resizing and extraction, if any.
* @param extend single pixel count to add to all edges or an Object with per-edge counts
* @throws {Error} Invalid parameters
@@ -1245,6 +1245,8 @@ declare namespace sharp {
sigma?: number | undefined;
}
type ExtendWith = 'background' | 'copy' | 'repeat' | 'mirror';
interface ExtendOptions {
/** single pixel count to top edge (optional, default 0) */
top?: number | undefined;
@@ -1256,6 +1258,8 @@ declare namespace sharp {
right?: number | undefined;
/** background colour, parsed by the color module, defaults to black without transparency. (optional, default {r:0,g:0,b:0,alpha:1}) */
background?: Color | undefined;
/** how the extension is done, one of: "background", "copy", "repeat", "mirror" (optional, default `'background'`) */
extendWith?: ExtendWith | undefined;
}
interface TrimOptions {

View File

@@ -36,6 +36,18 @@ const position = {
'left top': 8
};
/**
* How to extend the image.
* @member
* @private
*/
const extendWith = {
background: 'background',
copy: 'copy',
repeat: 'repeat',
mirror: 'mirror'
};
/**
* Strategies for automagic cover behaviour.
* @member
@@ -393,6 +405,13 @@ function extend (extend) {
}
}
this._setBackgroundColourOption('extendBackground', extend.background);
if (is.defined(extend.extendWith)) {
if (is.string(extendWith[extend.extendWith])) {
this.options.extendWith = extendWith[extend.extendWith];
} else {
throw is.invalidParameterError('extendWith', 'valid value', extend.extendWith);
}
}
} else {
throw is.invalidParameterError('extend', 'integer or object', extend);
}