mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 02:30:12 +02:00
492 lines
16 KiB
JavaScript
492 lines
16 KiB
JavaScript
'use strict';
|
|
|
|
const deprecate = require('util').deprecate;
|
|
const is = require('./is');
|
|
|
|
/**
|
|
* Weighting to apply when using contain/cover fit.
|
|
* @member
|
|
* @private
|
|
*/
|
|
const gravity = {
|
|
center: 0,
|
|
centre: 0,
|
|
north: 1,
|
|
east: 2,
|
|
south: 3,
|
|
west: 4,
|
|
northeast: 5,
|
|
southeast: 6,
|
|
southwest: 7,
|
|
northwest: 8
|
|
};
|
|
|
|
/**
|
|
* Position to apply when using contain/cover fit.
|
|
* @member
|
|
* @private
|
|
*/
|
|
const position = {
|
|
top: 1,
|
|
right: 2,
|
|
bottom: 3,
|
|
left: 4,
|
|
'right top': 5,
|
|
'right bottom': 6,
|
|
'left bottom': 7,
|
|
'left top': 8
|
|
};
|
|
|
|
/**
|
|
* Strategies for automagic cover behaviour.
|
|
* @member
|
|
* @private
|
|
*/
|
|
const strategy = {
|
|
entropy: 16,
|
|
attention: 17
|
|
};
|
|
|
|
/**
|
|
* Reduction kernels.
|
|
* @member
|
|
* @private
|
|
*/
|
|
const kernel = {
|
|
nearest: 'nearest',
|
|
cubic: 'cubic',
|
|
lanczos2: 'lanczos2',
|
|
lanczos3: 'lanczos3'
|
|
};
|
|
|
|
/**
|
|
* Methods by which an image can be resized to fit the provided dimensions.
|
|
* @member
|
|
* @private
|
|
*/
|
|
const fit = {
|
|
contain: 'contain',
|
|
cover: 'cover',
|
|
fill: 'fill',
|
|
inside: 'inside',
|
|
outside: 'outside'
|
|
};
|
|
|
|
/**
|
|
* Map external fit property to internal canvas property.
|
|
* @member
|
|
* @private
|
|
*/
|
|
const mapFitToCanvas = {
|
|
contain: 'embed',
|
|
cover: 'crop',
|
|
fill: 'ignore_aspect',
|
|
inside: 'max',
|
|
outside: 'min'
|
|
};
|
|
|
|
/**
|
|
* Resize image to `width`, `height` or `width x height`.
|
|
*
|
|
* When both a `width` and `height` are provided, the possible methods by which the image should **fit** these are:
|
|
* - `cover`: Crop to cover both provided dimensions (the default).
|
|
* - `contain`: Embed within both provided dimensions.
|
|
* - `fill`: Ignore the aspect ratio of the input and stretch to both provided dimensions.
|
|
* - `inside`: Preserving aspect ratio, resize the image to be as large as possible while ensuring its dimensions are less than or equal to both those specified.
|
|
* - `outside`: Preserving aspect ratio, resize the image to be as small as possible while ensuring its dimensions are greater than or equal to both those specified.
|
|
* Some of these values are based on the [object-fit](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit) CSS property.
|
|
*
|
|
* When using a `fit` of `cover` or `contain`, the default **position** is `centre`. Other options are:
|
|
* - `sharp.position`: `top`, `right top`, `right`, `right bottom`, `bottom`, `left bottom`, `left`, `left top`.
|
|
* - `sharp.gravity`: `north`, `northeast`, `east`, `southeast`, `south`, `southwest`, `west`, `northwest`, `center` or `centre`.
|
|
* - `sharp.strategy`: `cover` only, dynamically crop using either the `entropy` or `attention` strategy.
|
|
* Some of these values are based on the [object-position](https://developer.mozilla.org/en-US/docs/Web/CSS/object-position) CSS property.
|
|
*
|
|
* The experimental strategy-based approach resizes so one dimension is at its target length
|
|
* then repeatedly ranks edge regions, discarding the edge with the lowest score based on the selected strategy.
|
|
* - `entropy`: focus on the region with the highest [Shannon entropy](https://en.wikipedia.org/wiki/Entropy_%28information_theory%29).
|
|
* - `attention`: focus on the region with the highest luminance frequency, colour saturation and presence of skin tones.
|
|
*
|
|
* Possible interpolation kernels are:
|
|
* - `nearest`: Use [nearest neighbour interpolation](http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation).
|
|
* - `cubic`: Use a [Catmull-Rom spline](https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline).
|
|
* - `lanczos2`: Use a [Lanczos kernel](https://en.wikipedia.org/wiki/Lanczos_resampling#Lanczos_kernel) with `a=2`.
|
|
* - `lanczos3`: Use a Lanczos kernel with `a=3` (the default).
|
|
*
|
|
* @example
|
|
* sharp(input)
|
|
* .resize({ width: 100 })
|
|
* .toBuffer()
|
|
* .then(data => {
|
|
* // 100 pixels wide, auto-scaled height
|
|
* });
|
|
*
|
|
* @example
|
|
* sharp(input)
|
|
* .resize({ height: 100 })
|
|
* .toBuffer()
|
|
* .then(data => {
|
|
* // 100 pixels high, auto-scaled width
|
|
* });
|
|
*
|
|
* @example
|
|
* sharp(input)
|
|
* .resize(200, 300, {
|
|
* kernel: sharp.kernel.nearest,
|
|
* fit: 'contain',
|
|
* position: 'right top',
|
|
* background: { r: 255, g: 255, b: 255, alpha: 0.5 }
|
|
* })
|
|
* .toFile('output.png')
|
|
* .then(() => {
|
|
* // output.png is a 200 pixels wide and 300 pixels high image
|
|
* // containing a nearest-neighbour scaled version
|
|
* // contained within the north-east corner of a semi-transparent white canvas
|
|
* });
|
|
*
|
|
* @example
|
|
* const transformer = sharp()
|
|
* .resize({
|
|
* width: 200,
|
|
* height: 200,
|
|
* fit: sharp.fit.cover,
|
|
* position: sharp.strategy.entropy
|
|
* });
|
|
* // Read image data from readableStream
|
|
* // Write 200px square auto-cropped image data to writableStream
|
|
* readableStream
|
|
* .pipe(transformer)
|
|
* .pipe(writableStream);
|
|
*
|
|
* @example
|
|
* sharp(input)
|
|
* .resize(200, 200, {
|
|
* fit: sharp.fit.inside,
|
|
* withoutEnlargement: true
|
|
* })
|
|
* .toFormat('jpeg')
|
|
* .toBuffer()
|
|
* .then(function(outputBuffer) {
|
|
* // outputBuffer contains JPEG image data
|
|
* // no wider and no higher than 200 pixels
|
|
* // and no larger than the input image
|
|
* });
|
|
*
|
|
* @param {Number} [width] - pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height.
|
|
* @param {Number} [height] - pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width.
|
|
* @param {Object} [options]
|
|
* @param {String} [options.width] - alternative means of specifying `width`. If both are present this take priority.
|
|
* @param {String} [options.height] - alternative means of specifying `height`. If both are present this take priority.
|
|
* @param {String} [options.fit='cover'] - how the image should be resized to fit both provided dimensions, one of `cover`, `contain`, `fill`, `inside` or `outside`.
|
|
* @param {String} [options.position='centre'] - position, gravity or strategy to use when `fit` is `cover` or `contain`.
|
|
* @param {String|Object} [options.background={r: 0, g: 0, b: 0, alpha: 1}] - background colour when using a `fit` of `contain`, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black without transparency.
|
|
* @param {String} [options.kernel='lanczos3'] - the kernel to use for image reduction.
|
|
* @param {Boolean} [options.withoutEnlargement=false] - do not enlarge if the width *or* height are already less than the specified dimensions, equivalent to GraphicsMagick's `>` geometry option.
|
|
* @param {Boolean} [options.fastShrinkOnLoad=true] - take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images.
|
|
* @returns {Sharp}
|
|
* @throws {Error} Invalid parameters
|
|
*/
|
|
function resize (width, height, options) {
|
|
if (is.defined(width)) {
|
|
if (is.object(width) && !is.defined(options)) {
|
|
options = width;
|
|
} else if (is.integer(width) && width > 0) {
|
|
this.options.width = width;
|
|
} else {
|
|
throw is.invalidParameterError('width', 'positive integer', width);
|
|
}
|
|
} else {
|
|
this.options.width = -1;
|
|
}
|
|
if (is.defined(height)) {
|
|
if (is.integer(height) && height > 0) {
|
|
this.options.height = height;
|
|
} else {
|
|
throw is.invalidParameterError('height', 'positive integer', height);
|
|
}
|
|
} else {
|
|
this.options.height = -1;
|
|
}
|
|
if (is.object(options)) {
|
|
// Width
|
|
if (is.integer(options.width) && options.width > 0) {
|
|
this.options.width = options.width;
|
|
}
|
|
// Height
|
|
if (is.integer(options.height) && options.height > 0) {
|
|
this.options.height = options.height;
|
|
}
|
|
// Fit
|
|
if (is.defined(options.fit)) {
|
|
const canvas = mapFitToCanvas[options.fit];
|
|
if (is.string(canvas)) {
|
|
this.options.canvas = canvas;
|
|
} else {
|
|
throw is.invalidParameterError('fit', 'valid fit', options.fit);
|
|
}
|
|
}
|
|
// Position
|
|
if (is.defined(options.position)) {
|
|
const pos = is.integer(options.position)
|
|
? options.position
|
|
: strategy[options.position] || position[options.position] || gravity[options.position];
|
|
if (is.integer(pos) && (is.inRange(pos, 0, 8) || is.inRange(pos, 16, 17))) {
|
|
this.options.position = pos;
|
|
} else {
|
|
throw is.invalidParameterError('position', 'valid position/gravity/strategy', options.position);
|
|
}
|
|
}
|
|
// Background
|
|
if (is.defined(options.background)) {
|
|
this._setColourOption('resizeBackground', options.background);
|
|
}
|
|
// Kernel
|
|
if (is.defined(options.kernel)) {
|
|
if (is.string(kernel[options.kernel])) {
|
|
this.options.kernel = kernel[options.kernel];
|
|
} else {
|
|
throw is.invalidParameterError('kernel', 'valid kernel name', options.kernel);
|
|
}
|
|
}
|
|
// Without enlargement
|
|
if (is.defined(options.withoutEnlargement)) {
|
|
this._setBooleanOption('withoutEnlargement', options.withoutEnlargement);
|
|
}
|
|
// Shrink on load
|
|
if (is.defined(options.fastShrinkOnLoad)) {
|
|
this._setBooleanOption('fastShrinkOnLoad', options.fastShrinkOnLoad);
|
|
}
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Extends/pads the edges of the image with the provided background colour.
|
|
* This operation will always occur after resizing and extraction, if any.
|
|
*
|
|
* @example
|
|
* // Resize to 140 pixels wide, then add 10 transparent pixels
|
|
* // to the top, left and right edges and 20 to the bottom edge
|
|
* sharp(input)
|
|
* .resize(140)
|
|
* .)
|
|
* .extend({
|
|
* top: 10,
|
|
* bottom: 20,
|
|
* left: 10,
|
|
* right: 10
|
|
* background: { r: 0, g: 0, b: 0, alpha: 0 }
|
|
* })
|
|
* ...
|
|
*
|
|
* @param {(Number|Object)} extend - single pixel count to add to all edges or an Object with per-edge counts
|
|
* @param {Number} [extend.top]
|
|
* @param {Number} [extend.left]
|
|
* @param {Number} [extend.bottom]
|
|
* @param {Number} [extend.right]
|
|
* @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.
|
|
* @returns {Sharp}
|
|
* @throws {Error} Invalid parameters
|
|
*/
|
|
function extend (extend) {
|
|
if (is.integer(extend) && extend > 0) {
|
|
this.options.extendTop = extend;
|
|
this.options.extendBottom = extend;
|
|
this.options.extendLeft = extend;
|
|
this.options.extendRight = extend;
|
|
} else if (
|
|
is.object(extend) &&
|
|
is.integer(extend.top) && extend.top >= 0 &&
|
|
is.integer(extend.bottom) && extend.bottom >= 0 &&
|
|
is.integer(extend.left) && extend.left >= 0 &&
|
|
is.integer(extend.right) && extend.right >= 0
|
|
) {
|
|
this.options.extendTop = extend.top;
|
|
this.options.extendBottom = extend.bottom;
|
|
this.options.extendLeft = extend.left;
|
|
this.options.extendRight = extend.right;
|
|
this._setColourOption('extendBackground', extend.background);
|
|
} else {
|
|
throw new Error('Invalid edge extension ' + extend);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Extract a region of the image.
|
|
*
|
|
* - Use `extract` before `resize` for pre-resize extraction.
|
|
* - Use `extract` after `resize` for post-resize extraction.
|
|
* - Use `extract` before and after for both.
|
|
*
|
|
* @example
|
|
* sharp(input)
|
|
* .extract({ left: left, top: top, width: width, height: height })
|
|
* .toFile(output, function(err) {
|
|
* // Extract a region of the input image, saving in the same format.
|
|
* });
|
|
* @example
|
|
* sharp(input)
|
|
* .extract({ left: leftOffsetPre, top: topOffsetPre, width: widthPre, height: heightPre })
|
|
* .resize(width, height)
|
|
* .extract({ left: leftOffsetPost, top: topOffsetPost, width: widthPost, height: heightPost })
|
|
* .toFile(output, function(err) {
|
|
* // Extract a region, resize, then extract from the resized image
|
|
* });
|
|
*
|
|
* @param {Object} options
|
|
* @param {Number} options.left - zero-indexed offset from left edge
|
|
* @param {Number} options.top - zero-indexed offset from top edge
|
|
* @param {Number} options.width - dimension of extracted image
|
|
* @param {Number} options.height - dimension of extracted image
|
|
* @returns {Sharp}
|
|
* @throws {Error} Invalid parameters
|
|
*/
|
|
function extract (options) {
|
|
const suffix = this.options.width === -1 && this.options.height === -1 ? 'Pre' : 'Post';
|
|
['left', 'top', 'width', 'height'].forEach(function (name) {
|
|
const value = options[name];
|
|
if (is.integer(value) && value >= 0) {
|
|
this.options[name + (name === 'left' || name === 'top' ? 'Offset' : '') + suffix] = value;
|
|
} else {
|
|
throw new Error('Non-integer value for ' + name + ' of ' + value);
|
|
}
|
|
}, this);
|
|
// Ensure existing rotation occurs before pre-resize extraction
|
|
if (suffix === 'Pre' && ((this.options.angle % 360) !== 0 || this.options.useExifOrientation === true)) {
|
|
this.options.rotateBeforePreExtract = true;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Trim "boring" pixels from all edges that contain values similar to the top-left pixel.
|
|
* @param {Number} [threshold=10] the allowed difference from the top-left pixel, a number greater than zero.
|
|
* @returns {Sharp}
|
|
* @throws {Error} Invalid parameters
|
|
*/
|
|
function trim (threshold) {
|
|
if (!is.defined(threshold)) {
|
|
this.options.trimThreshold = 10;
|
|
} else if (is.number(threshold) && threshold > 0) {
|
|
this.options.trimThreshold = threshold;
|
|
} else {
|
|
throw is.invalidParameterError('threshold', 'number greater than zero', threshold);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
// Deprecated functions
|
|
|
|
/**
|
|
* @deprecated
|
|
* @private
|
|
*/
|
|
function crop (crop) {
|
|
this.options.canvas = 'crop';
|
|
if (!is.defined(crop)) {
|
|
// Default
|
|
this.options.position = gravity.center;
|
|
} else if (is.integer(crop) && is.inRange(crop, 0, 8)) {
|
|
// Gravity (numeric)
|
|
this.options.position = crop;
|
|
} else if (is.string(crop) && is.integer(gravity[crop])) {
|
|
// Gravity (string)
|
|
this.options.position = gravity[crop];
|
|
} else if (is.integer(crop) && crop >= strategy.entropy) {
|
|
// Strategy
|
|
this.options.position = crop;
|
|
} else if (is.string(crop) && is.integer(strategy[crop])) {
|
|
// Strategy (string)
|
|
this.options.position = strategy[crop];
|
|
} else {
|
|
throw is.invalidParameterError('crop', 'valid crop id/name/strategy', crop);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* @deprecated
|
|
* @private
|
|
*/
|
|
function embed (embed) {
|
|
this.options.canvas = 'embed';
|
|
if (!is.defined(embed)) {
|
|
// Default
|
|
this.options.position = gravity.center;
|
|
} else if (is.integer(embed) && is.inRange(embed, 0, 8)) {
|
|
// Gravity (numeric)
|
|
this.options.position = embed;
|
|
} else if (is.string(embed) && is.integer(gravity[embed])) {
|
|
// Gravity (string)
|
|
this.options.position = gravity[embed];
|
|
} else {
|
|
throw is.invalidParameterError('embed', 'valid embed id/name', embed);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* @deprecated
|
|
* @private
|
|
*/
|
|
function max () {
|
|
this.options.canvas = 'max';
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* @deprecated
|
|
* @private
|
|
*/
|
|
function min () {
|
|
this.options.canvas = 'min';
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* @deprecated
|
|
* @private
|
|
*/
|
|
function ignoreAspectRatio () {
|
|
this.options.canvas = 'ignore_aspect';
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* @deprecated
|
|
* @private
|
|
*/
|
|
function withoutEnlargement (withoutEnlargement) {
|
|
this.options.withoutEnlargement = is.bool(withoutEnlargement) ? withoutEnlargement : true;
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Decorate the Sharp prototype with resize-related functions.
|
|
* @private
|
|
*/
|
|
module.exports = function (Sharp) {
|
|
[
|
|
resize,
|
|
extend,
|
|
extract,
|
|
trim
|
|
].forEach(function (f) {
|
|
Sharp.prototype[f.name] = f;
|
|
});
|
|
// Class attributes
|
|
Sharp.gravity = gravity;
|
|
Sharp.strategy = strategy;
|
|
Sharp.kernel = kernel;
|
|
Sharp.fit = fit;
|
|
Sharp.position = position;
|
|
// Deprecated functions, to be removed in v0.22.0
|
|
Sharp.prototype.crop = deprecate(crop, 'crop(position) is deprecated, use resize({ fit: "cover", position }) instead');
|
|
Sharp.prototype.embed = deprecate(embed, 'embed(position) is deprecated, use resize({ fit: "contain", position }) instead');
|
|
Sharp.prototype.max = deprecate(max, 'max() is deprecated, use resize({ fit: "inside" }) instead');
|
|
Sharp.prototype.min = deprecate(min, 'min() is deprecated, use resize({ fit: "outside" }) instead');
|
|
Sharp.prototype.ignoreAspectRatio = deprecate(ignoreAspectRatio, 'ignoreAspectRatio() is deprecated, use resize({ fit: "fill" }) instead');
|
|
Sharp.prototype.withoutEnlargement = deprecate(withoutEnlargement, 'withoutEnlargement() is deprecated, use resize({ withoutEnlargement: true }) instead');
|
|
};
|