构造函数
Sharp
发出: Sharp#event:info
, Sharp#event:warning
new Sharp([input], [options])
构造函数工厂,用于创建 sharp
的实例,随后可以链式调用其他方法。
可以从此对象流出 JPEG、PNG、WebP、GIF、AVIF 或 TIFF 格式的图像数据。当使用基于流的输出时,可以从 info
事件中获得派生属性。
在处理过程中遇到的非关键问题将作为 warning
事件发出。
实现了 stream.Duplex 类。
当加载多个页面/帧的动画图像时,这些将组合成一个垂直堆叠的“厕纸卷”图像,其整体高度为 pageHeight
乘以 pages
的数量。
抛出:
Error
无效参数
参数 | 类型 | 默认 | 描述 |
---|---|---|---|
[input] | Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | Float32Array | Float64Array | string | Array | 若存在,则可以是包含 JPEG、PNG、WebP、AVIF、GIF、SVG 或 TIFF 图像数据的 Buffer / ArrayBuffer / Uint8Array / Uint8ClampedArray,或包含原始像素图像数据的 TypedArray,或包含 JPEG、PNG、WebP、AVIF、GIF、SVG 或 TIFF 图像文件系统路径的字符串。也可以提供输入数组,这些将被合并。当未提供时,可通过流传入 JPEG、PNG、WebP、AVIF、GIF、SVG、TIFF 或原始像素图像数据。 | |
[options] | Object | 若存在,则是带有可选属性的对象。 | |
[options.failOn] | string | “‘warning‘“ | 在何种情况下中止无效像素数据的处理,选项之一(按灵敏度排列,从低到高):‘none’,‘truncated’,‘error’,‘warning’。较高级别含有较低级别的含义。无效元数据将始终中止。 |
[options.limitInputPixels] | number | boolean | 268402689 | 不处理像素数量(宽 x 高)超过此限制的输入图像。假设输入元数据中的图像尺寸是可信的。整数像素数,0 或 false 去除限制,true 使用默认限制 268402689 (0x3FFF x 0x3FFF)。 |
[options.unlimited] | boolean | false | 设置为 true 以移除防止内存耗尽的安全措施(针对 JPEG、PNG、SVG、HEIF)。 |
[options.autoOrient] | boolean | false | 设置为 true 根据 EXIF Orientation 旋转/翻转图像(如果存在)。 |
[options.sequentialRead] | boolean | true | 设置为 false 使用随机访问而非顺序读取。一些操作会自动使用随机访问。 |
[options.density] | number | 72 | 表示矢量图像 DPI 的数值,范围是 1 到 100000。 |
[options.ignoreIcc] | number | false | 是否忽略内嵌的 ICC 配置文件(如果有)。 |
[options.pages] | number | 1 | 多页输入(GIF、WebP、TIFF)要提取的页数,使用 -1 表示所有页。 |
[options.page] | number | 0 | 多页输入(GIF、WebP、TIFF)开始提取的页码,基于 0。 |
[options.animated] | boolean | false | 设置为 true 以读取动画图像(GIF、WebP、TIFF)的所有帧/页,相当于设置 pages 为 -1 。 |
[options.raw] | Object | 描述原始像素输入图像数据。参见 raw() 了解像素排序。 | |
[options.raw.width] | number | 整数宽度(像素)。 | |
[options.raw.height] | number | 整数高度(像素)。 | |
[options.raw.channels] | number | 通道数量,介于 1 到 4 之间的整数。 | |
[options.raw.premultiplied] | boolean | 指定原始输入已经预乘,如果是则设置为 true 以避免 sharp 再次预乘图像。(可选,默认 false ) | |
[options.raw.pageHeight] | number | 动画图像每页/帧的像素高度,必须是 raw.height 的整数倍数。 | |
[options.create] | Object | 描述要创建的新图像。 | |
[options.create.width] | number | 整数宽度(像素)。 | |
[options.create.height] | number | 整数高度(像素)。 | |
[options.create.channels] | number | 通道数,3(RGB)或 4(RGBA)。 | |
[options.create.background] | string | Object | 通过 color 模块解析以获取红、绿、蓝和透明度值。 | |
[options.create.pageHeight] | number | 动画图像每页/帧像素高度,必须是 create.height 的整数倍数。 | |
[options.create.noise] | Object | 描述要创建的噪声。 | |
[options.create.noise.type] | string | 生成的噪声类型,目前仅支持 gaussian 。 | |
[options.create.noise.mean] | number | 128 | 生成噪声中像素的均值。 |
[options.create.noise.sigma] | number | 30 | 生成噪声中像素值的标准差。 |
[options.text] | Object | 描述新文本图像。 | |
[options.text.text] | string | 作为 UTF-8 字符串要渲染的文本。可包含 Pango 标记,如 <i>Le</i>Monde 。 | |
[options.text.font] | string | 渲染所用字体名称。 | |
[options.text.fontfile] | string | 字体文件的绝对文件系统路径,可用于 font 。 | |
[options.text.width] | number | 0 | 整数像素宽度,用于自动换行。超出此宽度的文本行将在词边界处折行。 |
[options.text.height] | number | 0 | 最大整数像素高度。定义后,将忽略 dpi ,文本将自动适应由 width 和 height 定义的像素分辨率。如果未指定或为 0,则忽略该属性。 |
[options.text.align] | string | “‘left‘“ | 多行文本的对齐方式(‘left’、‘centre’、‘center’、‘right’)。 |
[options.text.justify] | boolean | false | 设置为 true 应用文本两端对齐。 |
[options.text.dpi] | number | 72 | 文本渲染分辨率(大小)。指定了 height 时无效。 |
[options.text.rgba] | boolean | false | 设置为 true 以启用 RGBA 输出。适用于彩色表情符号渲染或支持 pango 标记功能,如 <span foreground="red">Red!</span> 。 |
[options.text.spacing] | number | 0 | 文本行高(单位:点)。未指定时使用字体默认行高。 |
[options.text.wrap] | string | “‘word‘“ | 提供宽度时的换行方式,选项为:‘word’、‘char’、‘word-char’(优先词边界,回退字符边界)或 ‘none’。 |
[options.join] | Object | 描述如何合并输入图像数组。 | |
[options.join.across] | number | 1 | 水平合并图像的数量。 |
[options.join.animated] | boolean | false | 设置为 true 将图像合并为动画图像。 |
[options.join.shim] | number | 0 | 合并图像间的间距像素数。 |
[options.join.background] | string | Object | 通过 color 模块解析以获取红、绿、蓝和透明度值。 | |
[options.join.halign] | string | “‘left‘“ | 水平合并图像时的对齐方式(‘left’、‘centre’、‘center’、‘right’)。 |
[options.join.valign] | string | “‘top‘“ | 垂直合并图像时的对齐方式(‘top’、‘centre’、‘center’、‘bottom’)。 |
[options.tiff] | Object | 描述 TIFF 特定选项。 | |
[options.tiff.subifd] | number | -1 | 用于提取 OME-TIFF 子图像文件目录,默认主图像。 |
[options.svg] | Object | 描述 SVG 特定选项。 | |
[options.svg.stylesheet] | string | SVG 输入的自定义 CSS,在 CSS 级联期间以用户来源应用。 | |
[options.svg.highBitdepth] | boolean | false | 设置为 true 以 32 位/通道(128 位)渲染 SVG 输入,而非 8 位/通道(32 位)RGBA。 |
[options.pdf] | Object | 描述 PDF 特定选项。需要全局安装的 libvips,编译时支持 PDFium、Poppler、ImageMagick 或 GraphicsMagick。 | |
[options.pdf.background] | string | Object | PDF 部分透明时使用的背景色。通过 color 模块解析为红绿蓝和透明度值。 | |
[options.openSlide] | Object | 描述 OpenSlide 特定选项。需要全局安装的 libvips,编译时支持 OpenSlide。 | |
[options.openSlide.level] | number | 0 | 多级输入中提取的层级,基于 0。 |
[options.jp2] | Object | 描述 JPEG 2000 特定选项。需要全局安装的 libvips,编译时支持 OpenJPEG。 | |
[options.jp2.oneshot] | boolean | false | 设置为 true 单次操作解码瓦片 JPEG 2000 图像,提高兼容性。 |
示例
sharp('input.jpg') .resize(300, 200) .toFile('output.jpg', function(err) { // output.jpg 是一个宽 300 像素、高 200 像素的图像 // 包含了 input.jpg 的缩放和裁剪版本 });
示例
// 从远程 URL 读取图像数据,// 调整宽度到 300 像素,// 发出一个 'info' 事件,带有计算的维度,// 最后将图像数据写入 writableStreamconst { body } = fetch('https://...');const readableStream = Readable.fromWeb(body);const transformer = sharp() .resize(300) .on('info', ({ height }) => { console.log(`图像高度是 ${height}`); });readableStream.pipe(transformer).pipe(writableStream);
示例
// 创建一个 300x200 的 PNG 图像,像素是半透明的红色sharp({ create: { width: 300, height: 200, channels: 4, background: { r: 255, g: 0, b: 0, alpha: 0.5 } }}).png().toBuffer().then( ... );
示例
// 将动画 GIF 转换为动画 WebPawait sharp('in.gif', { animated: true }).toFile('out.webp');
示例
// 读取原始像素数组并保存为 pngconst input = Uint8Array.from([255, 255, 255, 0, 0, 0]); // 或 Uint8ClampedArrayconst image = sharp(input, { // 因为输入不包含其维度或通道数 // 我们需要在构造函数选项中指定 raw: { width: 2, height: 1, channels: 3 }});await image.toFile('my-two-pixels.png');
示例
// 生成 RGB 高斯噪音await sharp({ create: { width: 300, height: 200, channels: 3, noise: { type: 'gaussian', mean: 128, sigma: 30 } }}).toFile('noise.png');
示例
// 从文本生成图像await sharp({ text: { text: 'Hello, world!', width: 400, // 最大宽度 height: 300 // 最大高度 }}).toFile('text_bw.png');
示例
// 使用 pango 标记和字体从文本生成 rgba 图像await sharp({ text: { text: '<span foreground="red">Red!</span><span background="cyan">blue</span>', font: 'sans', rgba: true, dpi: 300 }}).toFile('text_rgba.png');
示例
// 将四个输入图像作为 2x2 网格连接,间隔 4 像素const data = await sharp( [image1, image2, image3, image4], { join: { across: 2, shim: 4 } }).toBuffer();
示例
// 从表情符号生成一个两帧的动画图像const images = ['😀', '😛'].map(text => ({ text: { text, width: 64, height: 64, channels: 4, rgba: true }}));await sharp(images, { join: { animated: true } }).toFile('out.gif');
clone() ⇒
Sharp
快照 Sharp 实例,返回一个新的实例。克隆的实例继承其父实例的输入。这使得多个输出流,并因此多个处理管道能够共享单个输入流。
示例
const pipeline = sharp().rotate();pipeline.clone().resize(800, 600).pipe(firstWritableStream);pipeline.clone().extract({ left: 20, top: 20, width: 100, height: 100 }).pipe(secondWritableStream);readableStream.pipe(pipeline);// firstWritableStream 接收自动旋转、调整大小的 readableStream// secondWritableStream 接收自动旋转,提取的 readableStream 区域
示例
// 创建一个管道,将会下载图像,调整大小并格式化为不同文件// 使用承诺来知道管道何时完成const fs = require("fs");const got = require("got");const sharpStream = sharp({ failOn: 'none' });
const promises = [];
promises.push( sharpStream .clone() .jpeg({ quality: 100 }) .toFile("originalFile.jpg"));
promises.push( sharpStream .clone() .resize({ width: 500 }) .jpeg({ quality: 80 }) .toFile("optimized-500.jpg"));
promises.push( sharpStream .clone() .resize({ width: 500 }) .webp({ quality: 80 }) .toFile("optimized-500.webp"));
// https://github.com/sindresorhus/got/blob/main/documentation/3-streams.mdgot.stream("https://www.example.com/some-file.jpg").pipe(sharpStream);
Promise.all(promises) .then(res => { console.log("完成!", res); }) .catch(err => { console.error("处理文件时出错,清理一下", err); try { fs.unlinkSync("originalFile.jpg"); fs.unlinkSync("optimized-500.jpg"); fs.unlinkSync("optimized-500.webp"); } catch (e) {} });