@wordpress/vips 是一个用于与 wasm-vips 交互的辅助包,提供图像处理功能,如批量调整大小、压缩、格式转换和旋转。它通过 WASM 内存优化性能,避免重复解码。
// 示例:批量调整图像大小
const result = await batchResizeImage(id, buffer, 'image/jpeg', 'image/webp', resizes, true);
// 示例:旋转图像
const rotated = await rotateImage(id, buffer, 'image/png', 6);Helper package to interact with wasm-vips.
Install the module
npm install @wordpress/vips --save
Resizes an image into multiple sizes in a single pass using copyMemory().
Decodes the source image once, materializes it in WASM memory via copyMemory(), then uses thumbnailImage() for each sub-size. This avoids re-decoding the source for every thumbnail.
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Input mime type.string: Output mime type for all results.BatchResizeConfig[]: Array of resize configurations.Returns
Promise< BatchResizeResult[] >: Array of processed results, one per resize config.Cancels all ongoing image operations for a given item ID.
The onProgress callbacks check for an IDs existence in this list, killing the process if it’s absent.
Parameters
ItemId: Item ID.Returns
Compresses an existing image using vips.
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Mime type.Returns
Promise< ArrayBuffer | ArrayBufferLike >: Compressed file data.Converts an image to a different format using vips.
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Input mime type.string: Output mime type.Determines whether an image has an alpha channel.
Parameters
ArrayBuffer: Original file object.Returns
Promise< boolean >: Whether the image has an alpha channel.Resizes an image using vips.
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Mime type.ImageSizeCrop: Resize options.Returns
Promise< { buffer: ArrayBuffer | ArrayBufferLike; width: number; height: number; originalWidth: number; originalHeight: number; } >: Processed file data plus the old and new dimensions.Rotates an image based on EXIF orientation value.
EXIF orientation values: 1 = Normal (no rotation needed) 2 = Flipped horizontally 3 = Rotated 180° 4 = Flipped vertically 5 = Rotated 90° CCW and flipped horizontally 6 = Rotated 90° CW 7 = Rotated 90° CW and flipped horizontally 8 = Rotated 90° CCW
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Mime type.number: EXIF orientation value (1-8).Returns
Promise< { buffer: ArrayBuffer | ArrayBufferLike; width: number; height: number; } >: Rotated file data plus the new dimensions.Resizes an image into multiple sizes in a single pass using copyMemory().
Decodes the source image once, materializes it in WASM memory via copyMemory(), then uses thumbnailImage() for each sub-size. This avoids re-decoding the source for every thumbnail.
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Input mime type.string: Output mime type for all results.BatchResizeConfig[]: Array of resize configurations.Returns
Promise< BatchResizeResult[] >: Array of processed results, one per resize config.Cancels all ongoing image operations for a given item ID.
The onProgress callbacks check for an IDs existence in this list, killing the process if it’s absent.
Parameters
ItemId: Item ID.Returns
Compresses an existing image using vips.
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Mime type.Returns
Promise< ArrayBuffer | ArrayBufferLike >: Compressed file data.Converts an image to a different format using vips.
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Input mime type.string: Output mime type.Determines whether an image has an alpha channel.
Parameters
ArrayBuffer: Original file object.Returns
Promise< boolean >: Whether the image has an alpha channel.Resizes an image using vips.
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Mime type.ImageSizeCrop: Resize options.Returns
Promise< { buffer: ArrayBuffer | ArrayBufferLike; width: number; height: number; originalWidth: number; originalHeight: number; } >: Processed file data plus the old and new dimensions.Rotates an image based on EXIF orientation value.
EXIF orientation values: 1 = Normal (no rotation needed) 2 = Flipped horizontally 3 = Rotated 180° 4 = Flipped vertically 5 = Rotated 90° CCW and flipped horizontally 6 = Rotated 90° CW 7 = Rotated 90° CW and flipped horizontally 8 = Rotated 90° CCW
Parameters
ItemId: Item ID.ArrayBuffer: Original file buffer.string: Mime type.number: EXIF orientation value (1-8).Returns
Promise< { buffer: ArrayBuffer | ArrayBufferLike; width: number; height: number; } >: Rotated file data plus the new dimensions.