mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
233 lines
8.5 KiB
Markdown
233 lines
8.5 KiB
Markdown
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
|
|
|
## resize
|
|
|
|
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][1] 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][2] 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][3].
|
|
- `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][4].
|
|
- `cubic`: Use a [Catmull-Rom spline][5].
|
|
- `lanczos2`: Use a [Lanczos kernel][6] with `a=2`.
|
|
- `lanczos3`: Use a Lanczos kernel with `a=3` (the default).
|
|
|
|
### Parameters
|
|
|
|
- `width` **[Number][7]?** pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height.
|
|
- `height` **[Number][7]?** pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width.
|
|
- `options` **[Object][8]?**
|
|
- `options.width` **[String][9]?** alternative means of specifying `width`. If both are present this take priority.
|
|
- `options.height` **[String][9]?** alternative means of specifying `height`. If both are present this take priority.
|
|
- `options.fit` **[String][9]** how the image should be resized to fit both provided dimensions, one of `cover`, `contain`, `fill`, `inside` or `outside`. (optional, default `'cover'`)
|
|
- `options.position` **[String][9]** position, gravity or strategy to use when `fit` is `cover` or `contain`. (optional, default `'centre'`)
|
|
- `options.background` **([String][9] \| [Object][8])** background colour when using a `fit` of `contain`, parsed by the [color][10] module, defaults to black without transparency. (optional, default `{r:0,g:0,b:0,alpha:1}`)
|
|
- `options.kernel` **[String][9]** the kernel to use for image reduction. (optional, default `'lanczos3'`)
|
|
- `options.withoutEnlargement` **[Boolean][11]** do not enlarge if the width _or_ height are already less than the specified dimensions, equivalent to GraphicsMagick's `>` geometry option. (optional, default `false`)
|
|
- `options.fastShrinkOnLoad` **[Boolean][11]** take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images. (optional, default `true`)
|
|
|
|
### Examples
|
|
|
|
```javascript
|
|
sharp(input)
|
|
.resize({ width: 100 })
|
|
.toBuffer()
|
|
.then(data => {
|
|
// 100 pixels wide, auto-scaled height
|
|
});
|
|
```
|
|
|
|
```javascript
|
|
sharp(input)
|
|
.resize({ height: 100 })
|
|
.toBuffer()
|
|
.then(data => {
|
|
// 100 pixels high, auto-scaled width
|
|
});
|
|
```
|
|
|
|
```javascript
|
|
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
|
|
});
|
|
```
|
|
|
|
```javascript
|
|
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);
|
|
```
|
|
|
|
```javascript
|
|
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
|
|
});
|
|
```
|
|
|
|
- Throws **[Error][12]** Invalid parameters
|
|
|
|
Returns **Sharp**
|
|
|
|
## extend
|
|
|
|
Extends/pads the edges of the image with the provided background colour.
|
|
This operation will always occur after resizing and extraction, if any.
|
|
|
|
### Parameters
|
|
|
|
- `extend` **([Number][7] \| [Object][8])** single pixel count to add to all edges or an Object with per-edge counts
|
|
- `extend.top` **[Number][7]?**
|
|
- `extend.left` **[Number][7]?**
|
|
- `extend.bottom` **[Number][7]?**
|
|
- `extend.right` **[Number][7]?**
|
|
- `extend.background` **([String][9] \| [Object][8])** background colour, parsed by the [color][10] module, defaults to black without transparency. (optional, default `{r:0,g:0,b:0,alpha:1}`)
|
|
|
|
### Examples
|
|
|
|
```javascript
|
|
// 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 }
|
|
})
|
|
...
|
|
```
|
|
|
|
- Throws **[Error][12]** Invalid parameters
|
|
|
|
Returns **Sharp**
|
|
|
|
## extract
|
|
|
|
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.
|
|
|
|
### Parameters
|
|
|
|
- `options` **[Object][8]**
|
|
- `options.left` **[Number][7]** zero-indexed offset from left edge
|
|
- `options.top` **[Number][7]** zero-indexed offset from top edge
|
|
- `options.width` **[Number][7]** dimension of extracted image
|
|
- `options.height` **[Number][7]** dimension of extracted image
|
|
|
|
### Examples
|
|
|
|
```javascript
|
|
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.
|
|
});
|
|
```
|
|
|
|
```javascript
|
|
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
|
|
});
|
|
```
|
|
|
|
- Throws **[Error][12]** Invalid parameters
|
|
|
|
Returns **Sharp**
|
|
|
|
## trim
|
|
|
|
Trim "boring" pixels from all edges that contain values similar to the top-left pixel.
|
|
The `info` response Object will contain `trimOffsetLeft` and `trimOffsetTop` properties.
|
|
|
|
### Parameters
|
|
|
|
- `threshold` **[Number][7]** the allowed difference from the top-left pixel, a number greater than zero. (optional, default `10`)
|
|
|
|
|
|
- Throws **[Error][12]** Invalid parameters
|
|
|
|
Returns **Sharp**
|
|
|
|
[1]: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
|
|
|
|
[2]: https://developer.mozilla.org/en-US/docs/Web/CSS/object-position
|
|
|
|
[3]: https://en.wikipedia.org/wiki/Entropy_%28information_theory%29
|
|
|
|
[4]: http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
|
|
|
|
[5]: https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
|
|
|
|
[6]: https://en.wikipedia.org/wiki/Lanczos_resampling#Lanczos_kernel
|
|
|
|
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
|
|
|
|
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
|
|
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
|
|
[10]: https://www.npmjs.org/package/color
|
|
|
|
[11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
|
|
[12]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
|