Add gravity support to embed feature (#1038)

This commit is contained in:
Kenric D'Souza
2017-12-13 00:59:16 +05:30
committed by Lovell Fuller
parent 1d7a0ea99e
commit 927b77700d
28 changed files with 426 additions and 7 deletions

View File

@@ -136,6 +136,7 @@ const Sharp = function (input, options) {
height: -1,
canvas: 'crop',
crop: 0,
embed: 0,
useExifOrientation: false,
angle: 0,
rotateBeforePreExtract: false,

View File

@@ -179,11 +179,26 @@ function crop (crop) {
* // outputBuffer contains WebP image data of a 200 pixels wide and 300 pixels high
* // containing a scaled version, embedded on a transparent canvas, of input.gif
* });
*
* @param {String} [embed='centre'] - A member of `sharp.gravity` to embed to an edge/corner.
* @returns {Sharp}
* @throws {Error} Invalid parameters
*/
function embed () {
function embed (embed) {
this.options.canvas = 'embed';
if (!is.defined(embed)) {
// Default
this.options.embed = gravity.center;
} else if (is.integer(embed) && is.inRange(embed, 0, 8)) {
// Gravity (numeric)
this.options.embed = embed;
} else if (is.string(embed) && is.integer(gravity[embed])) {
// Gravity (string)
this.options.embed = gravity[embed];
} else {
throw is.invalidParameterError('embed', 'valid embed id/name', embed);
}
return this;
}