Skip to main content

SharpAssetPreviewStrategy

SharpAssetPreviewStrategy

This AssetPreviewStrategy uses the Sharp library to generate preview images of uploaded binary files. For non-image binaries, a generic "file" icon with the mime type overlay will be generated.

By default, this strategy will produce previews up to maximum dimensions of 1600 x 1600 pixels. The created preview images will match the input format - so a source file in jpeg format will output a jpeg preview, a webp source file will output a webp preview, and so on.

The settings for the outputs will default to Sharp's defaults (https://sharp.pixelplumbing.com/api-output). However, it is possible to pass your own configurations to control the output of each format:

AssetServerPlugin.init({
previewStrategy: new SharpAssetPreviewStrategy({
jpegOptions: { quality: 95 },
webpOptions: { quality: 95 },
}),
}),
Signature
class SharpAssetPreviewStrategy implements AssetPreviewStrategy {
constructor(config?: SharpAssetPreviewConfig)
generatePreviewImage(ctx: RequestContext, mimeType: string, data: Buffer) => Promise<Buffer>;
}

constructor

method
(config?: SharpAssetPreviewConfig) => SharpAssetPreviewStrategy

generatePreviewImage

method
(ctx: RequestContext, mimeType: string, data: Buffer) => Promise<Buffer>

SharpAssetPreviewConfig

This AssetPreviewStrategy uses the Sharp library to generate preview images of uploaded binary files. For non-image binaries, a generic "file" icon with the mime type overlay will be generated.

Signature
interface SharpAssetPreviewConfig {
maxHeight?: number;
maxWidth?: number;
jpegOptions?: sharp.JpegOptions;
pngOptions?: sharp.PngOptions;
webpOptions?: sharp.WebpOptions;
gifOptions?: sharp.GifOptions;
avifOptions?: sharp.AvifOptions;
}

maxHeight

property
number
default:
1600

The max height in pixels of a generated preview image.

maxWidth

property
number
default:
1600

The max width in pixels of a generated preview image.

jpegOptions

property
v1.7.0
sharp.JpegOptions

Set Sharp's options for encoding jpeg files: https://sharp.pixelplumbing.com/api-output#jpeg

pngOptions

property
v1.7.0
sharp.PngOptions

Set Sharp's options for encoding png files: https://sharp.pixelplumbing.com/api-output#png

webpOptions

property
v1.7.0
sharp.WebpOptions

Set Sharp's options for encoding webp files: https://sharp.pixelplumbing.com/api-output#webp

gifOptions

property
v1.7.0
sharp.GifOptions

Set Sharp's options for encoding gif files: https://sharp.pixelplumbing.com/api-output#gif

avifOptions

property
v1.7.0
sharp.AvifOptions

Set Sharp's options for encoding avif files: https://sharp.pixelplumbing.com/api-output#avif