@awesome-compressor/browser-compress-image
Version:
🚀 A powerful, lightweight browser image compression library with TypeScript support. Compress JPEG, PNG, GIF images with multiple output formats (Blob, File, Base64, ArrayBuffer) and zero dependencies.
301 lines • 9.01 kB
TypeScript
//#region src/types.d.ts
type CompressResultType = 'blob' | 'file' | 'base64' | 'arrayBuffer';
type OutputType = 'avif' | 'jpeg' | 'jxl' | 'png' | 'webp';
type CompressResult<T extends CompressResultType> = T extends 'blob' ? Blob : T extends 'file' ? File : T extends 'base64' ? string : T extends 'arrayBuffer' ? ArrayBuffer : never;
/**
* 工具配置接口
*/
interface ToolConfig {
/**
* 工具名称
*/
name: string;
/**
* API 密钥或其他配置参数
*/
key?: string;
/**
* 其他自定义配置参数
*/
[key: string]: any;
}
interface CompressOptions {
/**
* 压缩质量 (0-1)
* @default 0.6
*/
quality?: number;
/**
* 压缩模式
* - 'keepSize': 保持图片尺寸不变 (如100x100输入,输出仍为100x100),只改变文件大小
* - 'keepQuality': 保持图片质量不变,但可以改变尺寸
* @default 'keepSize'
*/
mode?: 'keepSize' | 'keepQuality';
/**
* 目标宽度 (仅在 keepQuality 模式下生效)
*/
targetWidth?: number;
/**
* 目标高度 (仅在 keepQuality 模式下生效)
*/
targetHeight?: number;
/**
* 最大宽度 (仅在 keepQuality 模式下生效)
*/
maxWidth?: number;
/**
* 最大高度 (仅在 keepQuality 模式下生效)
*/
maxHeight?: number;
/**
* 是否保留 EXIF 信息
* @default false
*/
preserveExif?: boolean;
/**
* 是否返回所有工具的压缩结果
* @default false
*/
returnAllResults?: boolean;
/**
* 返回结果类型
* @default 'blob'
*/
type?: CompressResultType;
/**
* 工具配置数组,用于传入各个工具的特定配置
* @example
* [
* { name: 'tinypng', key: 'your-api-key' },
* { name: 'other-tool', customConfig: 'value' }
* ]
*/
toolConfigs?: ToolConfig[];
}
interface CompressResultItem<T extends CompressResultType> {
tool: string;
result: CompressResult<T>;
originalSize: number;
compressedSize: number;
compressionRatio: number;
duration: number;
success: boolean;
error?: string;
}
interface MultipleCompressResults<T extends CompressResultType> {
bestResult: CompressResult<T>;
bestTool: string;
allResults: CompressResultItem<T>[];
totalDuration: number;
}
//#endregion
//#region src/compress.d.ts
declare function compress<T extends CompressResultType = 'blob'>(file: File, options: CompressOptions & {
type?: T;
returnAllResults: true;
}): Promise<MultipleCompressResults<T>>;
declare function compress<T extends CompressResultType = 'blob'>(file: File, options: CompressOptions & {
type?: T;
returnAllResults?: false;
}): Promise<CompressResult<T>>;
declare function compress<T extends CompressResultType = 'blob'>(file: File, quality?: number, type?: T): Promise<CompressResult<T>>;
interface CompressionStats {
bestTool: string;
compressedFile: Blob;
originalSize: number;
compressedSize: number;
compressionRatio: number;
totalDuration: number;
toolsUsed: {
tool: string;
size: number;
duration: number;
compressionRatio: number;
success: boolean;
error?: string;
}[];
}
declare function compressWithStats(file: File, qualityOrOptions?: number | CompressOptions): Promise<CompressionStats>;
//#endregion
//#region src/compressWithTools.d.ts
type CompressorTool = 'browser-image-compression' | 'compressorjs' | 'gifsicle' | 'canvas' | 'jsquash' | 'original' | 'tinypng';
type CompressorFunction = (file: File, options: any) => Promise<Blob>;
declare class ToolRegistry {
private tools;
private toolsCollections;
registerTool(name: CompressorTool, func: CompressorFunction, formats?: string[]): void;
getTool(name: CompressorTool): CompressorFunction | undefined;
getRegisteredTools(): CompressorTool[];
getToolsForFileType(fileType: string): CompressorTool[];
setToolPriority(fileType: string, tools: CompressorTool[]): void;
isToolRegistered(name: CompressorTool): boolean;
}
declare const globalToolRegistry: ToolRegistry;
interface CompressWithToolsOptions extends CompressOptions {
type?: CompressResultType;
returnAllResults?: boolean;
toolRegistry?: ToolRegistry;
}
declare function compressWithTools<T extends CompressResultType = 'blob'>(file: File, options: CompressWithToolsOptions & {
type?: T;
returnAllResults: true;
}): Promise<MultipleCompressResults<T>>;
declare function compressWithTools<T extends CompressResultType = 'blob'>(file: File, options: CompressWithToolsOptions & {
type?: T;
returnAllResults?: false;
}): Promise<CompressResult<T>>;
//#endregion
//#region src/tools/compressWithBrowserImageCompression.d.ts
declare function compressWithBrowserImageCompression(file: File, options: {
quality: number;
mode: string;
targetWidth?: number;
targetHeight?: number;
maxWidth?: number;
maxHeight?: number;
preserveExif?: boolean;
}): Promise<Blob>;
//#endregion
//#region src/tools/compressWithCompressorJS.d.ts
declare function compressWithCompressorJS(file: File, options: {
quality: number;
mode: string;
targetWidth?: number;
targetHeight?: number;
maxWidth?: number;
maxHeight?: number;
preserveExif?: boolean;
}): Promise<Blob>;
//#endregion
//#region src/tools/compressWithCanvas.d.ts
declare function compressWithCanvas(file: File, options: {
quality: number;
mode: string;
targetWidth?: number;
targetHeight?: number;
maxWidth?: number;
maxHeight?: number;
preserveExif?: boolean;
}): Promise<Blob>;
//#endregion
//#region src/tools/compressWithGifsicle.d.ts
declare function compressWithGifsicle(file: File, options: {
quality: number;
mode: string;
targetWidth?: number;
targetHeight?: number;
maxWidth?: number;
maxHeight?: number;
preserveExif?: boolean;
}): Promise<Blob>;
//#endregion
//#region src/tools/compressWithJsquash.d.ts
declare function compressWithJsquash(file: File, options: {
quality: number;
mode: string;
targetWidth?: number;
targetHeight?: number;
maxWidth?: number;
maxHeight?: number;
preserveExif?: boolean;
}): Promise<Blob>;
//#endregion
//#region src/tools/compressWithTinyPng.d.ts
declare function compressWithTinyPng(file: File, options: {
quality: number;
mode: string;
targetWidth?: number;
targetHeight?: number;
maxWidth?: number;
maxHeight?: number;
preserveExif?: boolean;
key?: string;
}): Promise<Blob>;
declare function clearTinyPngCache(): void;
declare function getTinyPngCacheSize(): number;
declare function getTinyPngCacheInfo(): {
totalEntries: number;
maxSize: number;
usageRate: number;
entries: {
key: string;
size: number;
type: string;
}[];
};
declare function configureTinyPngCache(maxSize?: number): void;
//#endregion
//#region src/tools.d.ts
declare function registerAllTools(): void;
declare function registerBrowserImageCompression(): void;
declare function registerCompressorJS(): void;
declare function registerCanvas(): void;
declare function registerGifsicle(): void;
declare function registerJsquash(): void;
declare function registerTinyPng(): void;
//#endregion
//#region src/utils/lruCache.d.ts
/**
* LRU (Least Recently Used) 缓存实现
* 当缓存达到最大容量时,自动淘汰最久未使用的项目
*/
declare class LRUCache<K, V> {
private cache;
private maxSize;
constructor(maxSize?: number);
/**
* 获取缓存项,如果存在则将其移到最新位置
*/
get(key: K): V | undefined;
/**
* 设置缓存项,如果超过最大容量则淘汰最久未使用的项
*/
set(key: K, value: V): void;
/**
* 检查缓存中是否存在指定的key
*/
has(key: K): boolean;
/**
* 清空所有缓存
*/
clear(): void;
/**
* 获取当前缓存大小
*/
get size(): number;
/**
* 获取最大缓存大小
*/
get maxCapacity(): number;
/**
* 设置新的最大缓存大小
*/
setMaxSize(newMaxSize: number): void;
/**
* 获取所有缓存条目的迭代器
*/
entries(): IterableIterator<[K, V]>;
/**
* 获取所有缓存的key
*/
keys(): IterableIterator<K>;
/**
* 获取所有缓存的value
*/
values(): IterableIterator<V>;
/**
* 删除指定的缓存项
*/
delete(key: K): boolean;
/**
* 获取缓存统计信息
*/
getStats(): {
size: number;
maxSize: number;
usageRate: number;
};
}
//#endregion
export { CompressOptions, CompressResult, CompressResultItem, CompressResultType, CompressWithToolsOptions, CompressionStats, CompressorFunction, CompressorTool, LRUCache, MultipleCompressResults, OutputType, ToolConfig, ToolRegistry, clearTinyPngCache, compress, compressWithBrowserImageCompression, compressWithCanvas, compressWithCompressorJS, compressWithGifsicle, compressWithJsquash, compressWithStats, compressWithTinyPng, compressWithTools, configureTinyPngCache, getTinyPngCacheInfo, getTinyPngCacheSize, globalToolRegistry, registerAllTools, registerBrowserImageCompression, registerCanvas, registerCompressorJS, registerGifsicle, registerJsquash, registerTinyPng };