调整图片大小

resize

将图像调整为宽度、高度或 宽度 x 高度。

当同时提供宽度和高度时,图像应该采用以下可能的方法:

  • cover: (默认值)保持长宽比,确保图像通过 裁剪/剪切 来适应所提供的尺寸
  • contain: 保留长宽比,必要时使用letterboxing在两个提供的尺寸之内。
  • fill: 忽略输入的宽高比,并拉伸到两个提供的尺寸。
  • inside: 保留纵横比,调整图像的大小,尽可能大,同时确保其尺寸小于或等于两者规定
  • outside: 保留纵横比,调整图像的大小尽可能小,同时确保其尺寸大于或等于两者规定

其中一些值是基于适合对象适合对象object-fit的 CSS 属性的。

When using a fit of cover or contain, the default position is centre. Other options are:

使用fitcovercontain,默认位置为 centre。其他选项包括:

  • 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时,使用,使用entropyattention 策略动态裁剪。

Some of these values are based on the object-position CSS property.

其中一些值是基于适合对象适合对象object-fit的 CSS 属性的。

基于实验策略的方法将调整大小,以使一维处于其目标长度,然后反复对边缘区域进行排名,并根据所选策略丢弃得分最低的边缘。

  • entropy: 集中在Shannon entropy 香农熵 最高的区域。.
  • attention: f专注于亮度频率最高,色彩饱和度高和肤色存在的区域。

可能的插值内核有:

参数

  • width number ? 指定宽度像素. 使用 nullundefined 自动缩放宽度以匹配高度。
  • height number ? 指定高度像素. 使用 nullundefined 自动缩放高度以匹配宽度。
  • options Object ?
    • options.width String ? 指定width的另一种方法。如果两者都存在,则优先。
    • options.height String ? 指定height的另一种方法。如果两者都存在,则优先。
    • options.fit String string 图像应如何调整大小以适合既定提供尺寸之一cover, contain, fill, insideoutside。(可选,默认’cover’)
    • options.position String 指定位置, fitcover or contain时的位置、重力或使用的策略。(可选,默认 'centre')
    • options.background (String | Object ) fit 使用 contain时的背景颜色, 由所述color 模块解析, 默认为黑色无透明度。(可选,默认{r:0,g:0,b:0,alpha:1})
    • options.kernel String 内核以用于图像缩小。(可选,默认 'lanczos3')
    • options.withoutEnlargement Boolean如果宽度或高度已经小于指定的尺寸(相当于GraphicsMagick的 > geometry选项),则不放大。(可选,默认false)
    • options.withoutReduction Boolean do not reduce if the width or height are already greater than the specified dimensions, equivalent to GraphicsMagick’s < geometry option. (可选,默认 false)
    • options.fastShrinkOnLoad Boolean 充分利用了JPEG和WebP加载时收缩功能,这会导致某些图像上出现轻微的莫尔纹。(可选,默认true)

例子

  1. sharp(input)
  2. .resize({ width: 100 })
  3. .toBuffer()
  4. .then(data => {
  5. // 100 pixels wide, auto-scaled height
  6. });
  1. sharp(input)
  2. .resize({ height: 100 })
  3. .toBuffer()
  4. .then(data => {
  5. // 100 pixels high, auto-scaled width
  6. });
  1. sharp(input)
  2. .resize(200, 300, {
  3. kernel: sharp.kernel.nearest,
  4. fit: 'contain',
  5. position: 'right top',
  6. background: { r: 255, g: 255, b: 255, alpha: 0.5 }
  7. })
  8. .toFile('output.png')
  9. .then(() => {
  10. // output.png is a 200 pixels wide and 300 pixels high image
  11. // containing a nearest-neighbour scaled version
  12. // contained within the north-east corner of a semi-transparent white canvas
  13. });
  1. const transformer = sharp()
  2. .resize({
  3. width: 200,
  4. height: 200,
  5. fit: sharp.fit.cover,
  6. position: sharp.strategy.entropy
  7. });
  8. // Read image data from readableStream
  9. // Write 200px square auto-cropped image data to writableStream
  10. readableStream
  11. .pipe(transformer)
  12. .pipe(writableStream);
  1. sharp(input)
  2. .resize(200, 200, {
  3. fit: sharp.fit.inside,
  4. withoutEnlargement: true
  5. })
  6. .toFormat('jpeg')
  7. .toBuffer()
  8. .then(function(outputBuffer) {
  9. // outputBuffer contains JPEG image data
  10. // no wider and no higher than 200 pixels
  11. // and no larger than the input image
  12. });
  1. sharp(input)
  2. .resize(200, 200, {
  3. fit: sharp.fit.outside,
  4. withoutReduction: true
  5. })
  6. .toFormat('jpeg')
  7. .toBuffer()
  8. .then(function(outputBuffer) {
  9. // outputBuffer contains JPEG image data
  10. // of at least 200 pixels wide and 200 pixels high while maintaining aspect ratio
  11. // and no smaller than the input image
  12. });
  1. const scaleByHalf = await sharp(input)
  2. .metadata()
  3. .then(({ width }) => sharp(input)
  4. .resize(Math.round(width * 0.5))
  5. .toBuffer()
  6. );
  • 无效参数时抛出错误 Error

返回 Sharp 对象

extend

用提供的背景色扩展/填充图像的边缘。调整大小和提取后(如果有)将始终执行此操作。

参数

  • extend (number | Object ) 每个要添加到所有边缘的像素数或一个具有每个边缘数的Object
    • extend.top number (可选,默认 0)
    • extend.left number (可选,默认 0)
    • extend.bottom number (可选,默认 0)
    • extend.right number (可选,默认 0)
    • extend.background (String | Object ) 背景颜色,由color 模块解析, 默认为黑色,不透明。 (可选,默认 {r:0,g:0,b:0,alpha:1})

例子

  1. // Resize to 140 pixels wide, then add 10 transparent pixels
  2. // to the top, left and right edges and 20 to the bottom edge
  3. sharp(input)
  4. .resize(140)
  5. .extend({
  6. top: 10,
  7. bottom: 20,
  8. left: 10,
  9. right: 10,
  10. background: { r: 0, g: 0, b: 0, alpha: 0 }
  11. })
  12. ...
  1. // Add a row of 10 red pixels to the bottom
  2. sharp(input)
  3. .extend({
  4. bottom: 10,
  5. background: 'red'
  6. })
  7. ...
  • 无效参数时抛出错误 Error

返回 Sharp 对象

extract

提取/裁剪图像区域。

  • 使用resize前使用extract预调整大小的提取。
  • 使用resize后使用extract后期调整大小的提取。
  • resize前后都使用extract

参数

  • options Object 使用整数像素值描述要提取的区域
    • options.left number 从左边缘开始的零索引偏移
    • options.top number 距上边缘的零索引偏移
    • options.width number 要提取的区域的宽度
    • options.height number 要提取的区域的高度

例子

  1. sharp(input)
  2. .extract({ left: left, top: top, width: width, height: height })
  3. .toFile(output, function(err) {
  4. // Extract a region of the input image, saving in the same format.
  5. });
  1. sharp(input)
  2. .extract({ left: leftOffsetPre, top: topOffsetPre, width: widthPre, height: heightPre })
  3. .resize(width, height)
  4. .extract({ left: leftOffsetPost, top: topOffsetPost, width: widthPost, height: heightPost })
  5. .toFile(output, function(err) {
  6. // Extract a region, resize, then extract from the resized image
  7. });
  • 无效参数时抛出错误 Error

返回 Sharp 对象

trim

从所有边缘修剪“boring”像素,这些像素包含与左上像素相似的值。完全由单一颜色组成的图像将使用alpha通道(如果有)来计算“boring”。

.toFile().toBuffer()回调获得的响应对象info将包含trimOffsetLefttrimOffsetTop属性。

参数

  • threshold number 从左上角的像素,一个大于零允许的差异。 (可选,默认 10)
  • 无效参数时抛出错误 Error

返回 Sharp 对象