Ensure deep copies when using clone()
mainclone() method now performs a deep copy of existing options. This ensures that modifications to the cloned instance do not inadvertently affect the original instance's configuration.repository·main·Indexed 12 days ago
https://github.com/lovell/sharpHigh-performance Node-API module for resizing and converting images in JPEG, PNG, WebP, GIF, AVIF, and TIFF formats. Leveraging libvips, it provides fast image processing capabilities including resizing, compositing, color space conversion, and channel manipulation for Node.js applications. Version 0.35.3.
clone() method now performs a deep copy of existing options. This ensures that modifications to the cloned instance do not inadvertently affect the original instance's configuration.Starting from version 0.34.0, sharp supports passing an array of input images to create joined images or animations. This replaces previous patterns for combining multiple image buffers or files into a single output.
// Example pattern (conceptual based on v0.34.0 breaking change)
// sharp([image1, image2, image3]).toFile('output.gif');sharp supports AVIF output in its prebuilt binaries. The heif output format is no longer considered experimental and defaults to being AVIF-centric.Sharp's performance depends on two levels of concurrency: the number of images processed in parallel by Node.js, and the number of threads used to process each individual image via libvips.
Node.js uses a libuv thread pool for asynchronous calls to native modules. By default, this pool size is 4. If you are using a machine with more than 4 physical CPU cores, you should increase UV_THREADPOOL_SIZE before starting your Node.js process to allow more images to be processed simultaneously.
libvips uses a shared thread pool that grows and shrinks on demand. By default, it uses one thread per CPU core. You can manually control this behavior using sharp.concurrency().
When using the default glibc memory allocator on Linux, you may experience memory fragmentation. To mitigate this, set the MALLOC_ARENA_MAX environment variable to 2 or 4 before starting the Node.js process.
# Example: Set thread pool size to match CPU cores
export UV_THREADPOOL_SIZE="$(lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l)"
# Example: Reduce memory fragmentation on glibc Linux
export MALLOC_ARENA_MAX="2"To run the official performance benchmarks comparing sharp against other libraries (like jimp, imagemagick, and gm), you must use Docker. The benchmark tests tasks such as JPEG decompression/resizing/compression and PNG processing.
Follow these steps:
git clone https://github.com/lovell/sharp.git
cd sharp/test/bench
./run-with-docker.shIn versions prior to v0.17.0, certain output format options were available as standalone functions. These have been deprecated in favor of passing options directly into the specific format function.
Deprecated functions:
quality(n)progressive(n)compressionLevel(n)withoutAdaptiveFiltering(n)withoutChromaSubsampling(n)trellisQuantisation(n) / trellisQuantization(n)overshootDeringing(n)optimiseScans(n) / optimizeScans(n)New Pattern:
Instead of calling the option function directly, pass the option within the object argument of the format function (e.g., jpeg() or webp()).
// Old way (deprecated in v0.17.0)
sharp(input).quality(80).toFile('output.jpg');
// New way
sharp(input).jpeg({ quality: 80 }).toFile('output.jpg');TypeScript definitions are included in the sharp package (as of v0.32.0). The @types/sharp package is deprecated.
When using TypeScript, ensure @types/node is included in your devDependencies.
Because sharp uses native binaries, it must be excluded from your bundler's output.
Use the externals configuration:
externals: {
'sharp': 'commonjs sharp'
}Use the --external flag or the external option in the API:
esbuild app.js --bundle --platform=node --external:sharpFor serverless-esbuild, configure packagerOptions in serverless.yml:
custom:
esbuild:
external:
- sharp
packagerOptions:
scripts:
- npm install --os=linux --libc=glibc --cpu=x64 sharpUse build.rollupOptions.external:
import { defineConfig } from 'vite';
export default defineConfig({
build: {
rollupOptions: {
external: ['sharp']
}
}
});You can install sharp using any major JavaScript package manager. Ensure your package manager is configured to install optional dependencies, as this is how prebuilt binaries are fetched.
npm install sharp
pnpm add sharp
yarn add sharp
bun add sharp
deno add --quiet npm:sharpnpm install sharpsharp dropped support for Node.js 14 and 16. To use this version or later, your environment must run Node.js ^18.17.0 or >= 20.3.0.The node_modules directory in your Lambda deployment package must include binaries for either linux-x64 or linux-arm64 depending on your chosen architecture.
Recommendations:
cbschuld/sharp-aws-lambda-layer or pH200/sharp-layer).background() function is deprecated. Instead, pass background options directly to the resize, extend, or flatten operations.