全局属性
versions
versions
包含 Sharp、libvips 的版本号及其依赖(当使用预构建的二进制文件时)的对象。
¥An Object containing the version numbers of sharp, libvips and (when using prebuilt binaries) its dependencies.
示例
¥Example
console.log(sharp.versions);
interpolators
interpolators :
enum
包含可用插值器及其正确值的对象
¥An Object containing the available interpolators and their proper values
只读:true Properties
¥Read only: true
Properties
名称 | 类型 | 默认 | 描述 |
---|---|---|---|
nearest | string | ”nearest” | 最近邻插值。仅适用于图片放大。 |
bilinear | string | ”bilinear” | 双线性插值。比双三次更快,但结果不太平滑。 |
bicubic | string | ”bicubic” | 双三次插值(默认值)。 |
locallyBoundedBicubic | string | ”lbb” | LBB 插值。防止一些“acutance”,但通常会降低性能 2 倍。 |
nohalo | string | ”nohalo” | 无晕插值。防止敏锐,但通常会降低性能 3 倍。 |
vertexSplitQuadraticBasisSpline | string | ”vsqbs” | VSQBS 插值。放大时防止 “staircasing”。 |
format
format ⇒
Object
包含表示可用输入和输出格式/方法的嵌套布尔值的对象。
¥An Object containing nested boolean values representing the available input and output formats/methods.
示例
¥Example
console.log(sharp.format);
queue
queue
当任务处于以下任一状态时,EventEmitter 会触发 change
事件:
¥An EventEmitter that emits a change
event when a task is either:
-
已排队,等待 libuv 提供工作线程
¥queued, waiting for libuv to provide a worker thread
-
complete
示例
¥Example
sharp.queue.on('change', function(queueLength) { console.log('Queue contains ' + queueLength + ' task(s)');});
cache
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] | Object | boolean | true | 具有以下属性的对象,或布尔值,其中 true 使用默认缓存设置, false 删除所有缓存 |
[options.memory] | number | 50 | 是用于此缓存的最大内存(以 MB 为单位) |
[options.files] | number | 20 | 是保持打开的最大文件数 |
[options.items] | number | 100 | 是要缓存的最大操作数 |
示例
¥Example
const stats = sharp.cache();
示例
¥Example
sharp.cache( { items: 200 } );sharp.cache( { files: 0 } );sharp.cache(false);
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 核心数,但使用基于 glibc 且不带 jemalloc 的 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.
Sharp 可以并行处理的最大图片数量由 libuv 的 UV_THREADPOOL_SIZE
环境变量控制,默认为 4。
¥The maximum number of images that sharp can process in parallel
is controlled by libuv’s UV_THREADPOOL_SIZE
environment variable,
which defaults to 4.
https://nodejs.cn/api/cli.html#uv_threadpool_sizesize
例如,默认情况下,具有 8 个 CPU 核心的机器将并行处理 4 个图片,每个图片最多使用 8 个线程,因此最多有 32 个并发线程。
¥For example, by default, a machine with 8 CPU cores will process 4 images in parallel and use up to 8 threads per image, so there will be up to 32 concurrent threads.
返回:number
- concurrency
¥Returns: number
- concurrency
参数 | 类型 |
---|---|
[concurrency] | number |
示例
¥Example
const threads = sharp.concurrency(); // 4sharp.concurrency(2); // 2sharp.concurrency(0); // 4
counters
counters() ⇒
Object
提供对内部任务计数器的访问。
¥Provides access to internal task counters.
-
queue 是该模块已排队等待 libuv 从其池中提供工作线程的任务数。
¥queue is the number of tasks this module has queued waiting for libuv to provide a worker thread from its pool.
-
process 是当前正在处理的调整大小任务的数量。
¥process is the number of resize tasks currently being processed.
示例
¥Example
const counters = sharp.counters(); // { queue: 2, process: 4 }
simd
simd([simd]) ⇒
boolean
获取和设置 SIMD 向量单元指令的使用。要求 libvips 已在高速公路支持下编译。
¥Get and set use of SIMD vector unit instructions. Requires libvips to have been compiled with highway support.
利用 CPU 的 SIMD 向量单元提高 resize
、blur
和 sharpen
操作的性能,例如 英特尔 SSE 和 ARM NEON。
¥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] | boolean | true |
示例
¥Example
const simd = sharp.simd();// simd is `true` if the runtime use of highway is currently enabled
示例
¥Example
const simd = sharp.simd(false);// prevent libvips from using highway at runtime
block
block(options)
在运行时阻止 libvips 操作。
¥Block libvips operations at runtime.
这是对 VIPS_BLOCK_UNTRUSTED
环境变量的补充,设置该变量后将阻止所有 “untrusted” 操作。
¥This is in addition to the VIPS_BLOCK_UNTRUSTED
environment variable,
which when set will block all “untrusted” operations.
自从:0.32.4
¥Since: 0.32.4
参数 | 类型 | 描述 |
---|---|---|
options | Object | |
options.operation | Array.<string> | 要阻止的 libvips 底层操作名称列表。 |
示例(阻止所有 TIFF 输入。)
¥Example (Block all TIFF input.)
sharp.block({ operation: ['VipsForeignLoadTiff']});
unblock
unblock(options)
在运行时解锁 libvips 操作。
¥Unblock libvips operations at runtime.
这对于定义允许的操作列表很有用。
¥This is useful for defining a list of allowed operations.
自从:0.32.4
¥Since: 0.32.4
参数 | 类型 | 描述 |
---|---|---|
options | Object | |
options.operation | Array.<string> | 要解除阻止的 libvips 底层操作名称列表。 |
示例(阻止文件系统中除 WebP 之外的所有输入。)
¥Example (Block all input except WebP from the filesystem.)
sharp.block({ operation: ['VipsForeignLoad']});sharp.unblock({ operation: ['VipsForeignLoadWebpFile']});
示例(阻止缓冲区或流中除 JPEG 和 PNG 之外的所有输入。)
¥Example (Block all input except JPEG and PNG from a Buffer or Stream.)
sharp.block({ operation: ['VipsForeignLoad']});sharp.unblock({ operation: ['VipsForeignLoadJpegBuffer', 'VipsForeignLoadPngBuffer']});