packages/image-server/README.md
Server-side image processing utilities for Onlook using Sharp.
This package contains Node.js-only dependencies (Sharp) and MUST NOT be imported in browser or preload scripts.
This package is part of the Onlook monorepo and should be added as a dependency to packages that need server-side image processing.
{
"dependencies": {
"@onlook/image-server": "*"
}
}
import { compressImageServer } from '@onlook/image-server';
// Compress and save to file
const result = await compressImageServer('input.jpg', 'output.webp', {
quality: 80,
format: 'webp',
});
// Compress to buffer
const result = await compressImageServer('input.jpg', undefined, { quality: 70 });
import { batchCompressImagesServer } from '@onlook/image-server';
const results = await batchCompressImagesServer(
['image1.jpg', 'image2.png'],
'./output-directory',
{ format: 'webp', quality: 85 },
);
import { compressImageServer } from '@onlook/image-server';
import { COMPRESSION_IMAGE_PRESETS } from '@onlook/constants';
// Use predefined presets
const result = await compressImageServer('input.jpg', 'output.webp', COMPRESSION_IMAGE_PRESETS.web);
The following formats are automatically skipped and return an error:
// These will return { success: false, error: "Skipping ICO/SVG file..." }
await compressImageServer('favicon.ico', 'output.webp'); // ❌ Skipped
await compressImageServer('logo.svg', 'output.png'); // ❌ Skipped
Why skip ICO and SVG?
compressImageServer(input, outputPath?, options?)Compresses a single image.
Parameters:
input: string | Buffer - File path or image bufferoutputPath: string (optional) - Output file path. If not provided, returns bufferoptions: CompressionOptions (optional) - Compression settingsReturns: Promise<CompressionResult>
batchCompressImagesServer(inputPaths, outputDir, options?)Compresses multiple images in batch. ICO and SVG files are automatically skipped.
Parameters:
inputPaths: string[] - Array of input file pathsoutputDir: string - Output directoryoptions: CompressionOptions (optional) - Compression settingsReturns: Promise<CompressionResult[]> - Results include both successful compressions and skipped files
web: Optimized for web delivery (WebP, 80% quality)thumbnail: Small thumbnails (300x300, WebP, 70% quality)highQuality: High quality output (JPEG, 95% quality)lowFileSize: Minimal file size (WebP, 60% quality)The package gracefully handles various error conditions:
const result = await compressImageServer('input.jpg');
if (!result.success) {
console.error('Compression failed:', result.error);
// Common error cases:
// - "Skipping ICO file - format not supported for compression"
// - "Skipping SVG format - not supported for compression"
// - File not found, permission errors, corrupted files, etc.
}
Apache-2.0