@stacksjs/stx
Version:
A performant UI Framework. Powered by Bun.
112 lines • 3.8 kB
TypeScript
import path from 'node:path';
/**
* Check if sharp is available
*/
export declare function isSharpAvailable(): Promise<boolean>;
/**
* Process an image and generate responsive variants
*/
export declare function processImage(input: string | Buffer, options?: ImageOptions): Promise<ProcessedImage>;
/**
* Get image metadata
*/
export declare function getImageMetadata(input: Buffer | string): Promise<ImageMetadata>;
/**
* Generate srcset attribute from variants
*/
export declare function generateSrcSet(variants: ImageVariant[], format?: ImageFormat): string;
/**
* Generate sizes attribute
*/
export declare function generateSizes(breakpoints?: Record<string, string>): string;
/**
* Get best fallback variant (middle resolution JPEG)
*/
export declare function getFallbackVariant(variants: ImageVariant[]): ImageVariant | undefined;
/**
* Group variants by format
*/
export declare function groupVariantsByFormat(variants: ImageVariant[]): Map<ImageFormat, ImageVariant[]>;
/**
* Check if file is an image
*/
export declare function isImageFile(filePath: string): boolean;
/**
* Get MIME type for format
*/
export declare function getMimeType(format: ImageFormat): string;
/**
* Calculate total size of variants
*/
export declare function getTotalSize(variants: ImageVariant[]): number;
/**
* Format file size for display
*/
export declare function formatSize(bytes: number): string;
// ============================================================================
// Constants
// ============================================================================
export declare const DEFAULT_WIDTHS: readonly [320, 640, 768, 1024, 1280, 1536, 1920];
export declare const DEFAULT_FORMATS: ImageFormat[];
export declare const DEFAULT_QUALITY: 80;
// ============================================================================
// Types
// ============================================================================
export declare interface ImageOptions {
widths?: number[]
formats?: ImageFormat[]
quality?: number
fit?: 'cover' | 'contain' | 'fill' | 'inside' | 'outside'
placeholder?: 'blur' | 'dominant-color' | 'none'
outputDir?: string
baseUrl?: string
}
export declare interface ProcessedImage {
src: string
variants: ImageVariant[]
placeholder?: string
width: number
height: number
aspectRatio: number
hash: string
}
export declare interface ImageVariant {
path: string
url: string
width: number
height: number
format: ImageFormat
size: number
}
export declare interface ImageMetadata {
width: number
height: number
format: string
hasAlpha?: boolean
}
export type ImageFormat = 'webp' | 'avif' | 'jpeg' | 'png'
// ============================================================================
// Sharp Integration
// ============================================================================
declare type SharpInstance = {
metadata(): Promise<ImageMetadata>
resize(width: number, height?: number, options?: { fit?: string }): SharpInstance
webp(options?: { quality?: number }): SharpInstance
avif(options?: { quality?: number }): SharpInstance
jpeg(options?: { quality?: number }): SharpInstance
png(options?: { quality?: number }): SharpInstance
toBuffer(): Promise<Buffer>
blur(sigma: number): SharpInstance
toFormat(format: string, options?: { quality?: number }): SharpInstance
}
declare type SharpModule = {
(input: Buffer | string): SharpInstance
default?: (input: Buffer | string) => SharpInstance
}
// ============================================================================
// Error Handling
// ============================================================================
export declare class ImageProcessingError extends Error {
code: string;
constructor(message: string, code: string);
}