全局属性
versions
Section titled “versions”versions
一个包含 sharp、libvips 版本号的对象,以及(在使用预编译二进制文件时)它的依赖。
🌐 An Object containing the version numbers of sharp, libvips and (when using prebuilt binaries) its dependencies.
示例
console.log(sharp.versions);interpolators
Section titled “interpolators”interpolators :
enum
包含可用插值器及其正确值的对象
🌐 An Object containing the available interpolators and their proper values
仅可读取:true 属性
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| nearest | string |
"nearest" |
最近邻插值。仅适合图片放大。 |
| bilinear | string |
"bilinear" |
双线性插值。比双三次快,但结果不够平滑。 |
| bicubic | string |
"bicubic" |
双三次插值(默认)。 |
| locallyBoundedBicubic | 字符串 |
"lbb" |
LBB 插值。可以防止一些“锐度”,但通常会将性能降低一半。 |
| nohalo | string |
"nohalo" |
Nohalo 插值。防止锐度增强,但通常会使性能降低大约 3 倍。 |
| vertexSplitQuadraticBasisSpline | 字符串 |
"vsqbs" |
VSQBS 插值。在放大时防止“阶梯状”效果。 |
format
Section titled “format”format ⇒
Object
包含表示可用输入和输出格式/方法的嵌套布尔值的对象。
🌐 An Object containing nested boolean values representing the available input and output formats/methods.
示例
console.log(sharp.format);queue
一个 EventEmitter,当任务满足以下任一条件时,会触发 change 事件:
🌐 An EventEmitter that emits a change event when a task is either:
- 排队等待 libuv 提供一个工作线程
- complete
示例
sharp.queue.on('change', function(queueLength) { console.log('Queue contains ' + queueLength + ' task(s)');});cache([options]) ⇒
Object
获取或在提供选项时设置 libvips 操作缓存的限制。缓存中的现有条目在限制更改后会被修剪。此方法总是返回缓存统计信息,对于确定某个特定任务所需的工作内存很有用。
🌐 Gets or, when options are provided, sets the limits of libvips’ operation cache. Existing entries in the cache will be trimmed after any change in limits. This method always returns cache statistics, useful for determining how much working memory is required for a particular task.
| 参数 | 类型 | 默认 | 描述 |
|---|---|---|---|
| [options] | 对象 |布尔 |
真 |
具有以下属性的对象,或布尔,其中 true 使用默认缓存设置,false 移除所有缓存 |
| [options.memory] | 数字 |
50 |
是该缓存可使用的最大内存(MB |
| [options.files] | 数字 |
20 |
是可保留的最大文件数 |
| [options.items] | 数字 |
100 |
是缓存的最大操作次数 |
示例
const stats = sharp.cache();Example
sharp.cache( { items: 200 } );sharp.cache( { files: 0 } );sharp.cache(false);concurrency
Section titled “concurrency”concurrency([concurrency]) ⇒
number
获取,或者在提供并发数的情况下,设置 libvips 用于处理 每张图片 的最大线程数。这些线程来自由 glib 管理的线程池,有助于避免创建新线程的开销。
🌐 Gets or, when a concurrency is provided, sets the maximum number of threads libvips should use to process each image. These are from a thread pool managed by glib, which helps avoid the overhead of creating new threads.
此方法始终返回当前并发数。
🌐 This method always returns the current concurrency.
默认值是 CPU 核心数,除非在不使用 jemalloc 的基于 glibc 的 Linux 上使用,此时默认值是 1,以帮助减少内存碎片。
🌐 The default value is the number of CPU cores,
except when using glibc-based Linux without jemalloc,
where the default is 1 to help reduce memory fragmentation.
0 的值会将其重置为 CPU 核心数。
🌐 A value of 0 will reset this to the number of CPU cores.
一些图片格式库会创建额外的线程,例如,libaom 在编码 AVIF 图片时会管理自己的 4 个线程,这些线程与这里设置的值是独立的。
🌐 Some image format libraries spawn additional threads, e.g. libaom manages its own 4 threads when encoding AVIF images, and these are independent of the value set here.
返回值:number - 并发
| 参数 | 类型 |
|---|---|
| [concurrency] | 数字 |
示例
const threads = sharp.concurrency(); // 4sharp.concurrency(2); // 2sharp.concurrency(0); // 4counters
Section titled “counters”counters() ⇒
Object
提供对内部任务计数器的访问。
🌐 Provides access to internal task counters.
- 队列是这个模块排队等待 libuv 从它的线程池提供工作线程的任务数量。
- process 是当前正在处理的调整大小任务的数量。
示例
const counters = sharp.counters(); // { queue: 2, process: 4 }simd([simd]) ⇒
boolean
获取和设置 SIMD 向量单元指令的使用情况。需要 libvips 已经使用 highway 支持编译。
🌐 Get and set use of SIMD vector unit instructions. Requires libvips to have been compiled with highway support.
通过利用 CPU 的 SIMD 向量单元(例如 Intel SSE 和 ARM NEON)来提高 resize、blur 和 sharpen 操作的性能。
🌐 Improves the performance of resize, blur and sharpen operations
by taking advantage of the SIMD vector unit of the CPU, e.g. Intel SSE and ARM NEON.
| 参数 | 类型 | 默认值 |
|---|---|---|
| [simd] | 布尔值 |
true |
示例
const simd = sharp.simd();// simd is `true` if the runtime use of highway is currently enabledExample
const simd = sharp.simd(false);// prevent libvips from using highway at runtimeblock(options)
在运行时阻止 libvips 操作。
🌐 Block libvips operations at runtime.
这是在 VIPS_BLOCK_UNTRUSTED 环境变量之外的额外操作,设置后会阻止所有“未信任”的操作。
🌐 This is in addition to the VIPS_BLOCK_UNTRUSTED environment variable,
which when set will block all “untrusted” operations.
自: 0.32.4
| 参数 | 类型 | 描述 |
|---|---|---|
| options | 对象 |
|
| options.operation | 数组.<字符串> |
要阻止的 libvips 低级操作名称列表。 |
示例 (阻止所有 TIFF 输入.)
sharp.block({ operation: ['VipsForeignLoadTiff']});unblock
Section titled “unblock”unblock(options)
在运行时解锁 libvips 操作。
🌐 Unblock libvips operations at runtime.
这对于定义允许的操作列表很有用。
🌐 This is useful for defining a list of allowed operations.
自: 0.32.4
| 参数 | 类型 | 描述 |
|---|---|---|
| options | 对象 |
|
| options.operation | 数组.<字符串> |
要解锁的 libvips 低级操作名称列表。 |
示例 (阻止除了文件系统中的 WebP 之外的所有输入。)
sharp.block({ operation: ['VipsForeignLoad']});sharp.unblock({ operation: ['VipsForeignLoadWebpFile']});Example (Block all input except JPEG and PNG from a Buffer or Stream.)
sharp.block({ operation: ['VipsForeignLoad']});sharp.unblock({ operation: ['VipsForeignLoadJpegBuffer', 'VipsForeignLoadPngBuffer']});