Skip to main content

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:

Ts
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>

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

propertynumber
Default:1600

The max height in pixels of a generated preview image.

maxWidth

propertynumber
Default:1600

The max width in pixels of a generated preview image.

jpegOptions

propertysharp.JpegOptionsv1.7.0

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

pngOptions

propertysharp.PngOptionsv1.7.0

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

webpOptions

propertysharp.WebpOptionsv1.7.0

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

gifOptions

propertysharp.GifOptionsv1.7.0

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

avifOptions

propertysharp.AvifOptionsv1.7.0

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

Was this chapter helpful?
Report Issue
Edited Feb 2, 2026·Edit this page