调整图片大小
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:
使用fit为cover 或 contain,默认位置为 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,centeror 或centre.sharp.strategy: 仅cover时,使用,使用entropy或attention策略动态裁剪。
Some of these values are based on the object-position CSS property.
其中一些值是基于适合对象适合对象object-fit的 CSS 属性的。
基于实验策略的方法将调整大小,以使一维处于其目标长度,然后反复对边缘区域进行排名,并根据所选策略丢弃得分最低的边缘。
entropy: 集中在Shannon entropy 香农熵 最高的区域。.attention: f专注于亮度频率最高,色彩饱和度高和肤色存在的区域。
可能的插值内核有:
nearest: 使用最近邻插值 最近邻插值 .cubic: 使用 Catmull-Rom样条线 .mitchell: 使用 Mitchell-Netravali样条曲线 .lanczos2: 使用 Lanczos内核 与a=2一起使用.lanczos3: 使用a=3的 Lanczos 内核 (默认设置).
参数
widthnumber ? 指定宽度像素. 使用null或undefined自动缩放宽度以匹配高度。heightnumber ? 指定高度像素. 使用null或undefined自动缩放高度以匹配宽度。optionsObject ?options.widthString ? 指定width的另一种方法。如果两者都存在,则优先。options.heightString ? 指定height的另一种方法。如果两者都存在,则优先。options.fitString string 图像应如何调整大小以适合既定提供尺寸之一cover,contain,fill,inside或outside。(可选,默认’cover’)options.positionString 指定位置,fit为coverorcontain时的位置、重力或使用的策略。(可选,默认'centre')options.background(String | Object )fit使用contain时的背景颜色, 由所述color 模块解析, 默认为黑色无透明度。(可选,默认{r:0,g:0,b:0,alpha:1})options.kernelString 内核以用于图像缩小。(可选,默认'lanczos3')options.withoutEnlargementBoolean如果宽度或高度已经小于指定的尺寸(相当于GraphicsMagick的>geometry选项),则不放大。(可选,默认false)options.withoutReductionBoolean do not reduce if the width or height are already greater than the specified dimensions, equivalent to GraphicsMagick’s<geometry option. (可选,默认false)options.fastShrinkOnLoadBoolean 充分利用了JPEG和WebP加载时收缩功能,这会导致某些图像上出现轻微的莫尔纹。(可选,默认true)
例子
sharp(input).resize({ width: 100 }).toBuffer().then(data => {// 100 pixels wide, auto-scaled height});
sharp(input).resize({ height: 100 }).toBuffer().then(data => {// 100 pixels high, auto-scaled width});
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});
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 writableStreamreadableStream.pipe(transformer).pipe(writableStream);
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});
sharp(input).resize(200, 200, {fit: sharp.fit.outside,withoutReduction: true}).toFormat('jpeg').toBuffer().then(function(outputBuffer) {// outputBuffer contains JPEG image data// of at least 200 pixels wide and 200 pixels high while maintaining aspect ratio// and no smaller than the input image});
const scaleByHalf = await sharp(input).metadata().then(({ width }) => sharp(input).resize(Math.round(width * 0.5)).toBuffer());
- 无效参数时抛出错误 Error
返回 Sharp 对象
extend
用提供的背景色扩展/填充图像的边缘。调整大小和提取后(如果有)将始终执行此操作。
参数
例子
// Resize to 140 pixels wide, then add 10 transparent pixels// to the top, left and right edges and 20 to the bottom edgesharp(input).resize(140).extend({top: 10,bottom: 20,left: 10,right: 10,background: { r: 0, g: 0, b: 0, alpha: 0 }})...
// Add a row of 10 red pixels to the bottomsharp(input).extend({bottom: 10,background: 'red'})...
- 无效参数时抛出错误 Error
返回 Sharp 对象
extract
提取/裁剪图像区域。
- 使用
resize前使用extract预调整大小的提取。 - 使用
resize后使用extract后期调整大小的提取。 - 在
resize前后都使用extract。
参数
optionsObject 使用整数像素值描述要提取的区域
例子
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.});
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});
- 无效参数时抛出错误 Error
返回 Sharp 对象
trim
从所有边缘修剪“boring”像素,这些像素包含与左上像素相似的值。完全由单一颜色组成的图像将使用alpha通道(如果有)来计算“boring”。
从.toFile()或.toBuffer()回调获得的响应对象info将包含trimOffsetLeft和trimOffsetTop属性。
参数
返回 Sharp 对象
