// Copyright (c) 2024 Cloudflare, Inc. // Licensed under the Apache 2.0 license found in the LICENSE file or at: // https://opensource.org/licenses/Apache-2.0 type ImageInfoResponse = | { format: 'image/svg+xml' } | { format: string; fileSize: number; width: number; height: number; }; type ImageTransform = { width?: number; height?: number; background?: string; blur?: number; border?: | { color?: string; width?: number; } | { top?: number; bottom?: number; left?: number; right?: number; }; brightness?: number; contrast?: number; fit?: 'scale-down' | 'contain' | 'pad' | 'squeeze' | 'cover' | 'crop'; flip?: 'h' | 'v' | 'hv'; gamma?: number; segment?: 'foreground'; gravity?: | 'face' | 'left' | 'right' | 'top' | 'bottom' | 'center' | 'auto' | 'entropy' | { x?: number; y?: number; mode: 'remainder' | 'box-center'; }; rotate?: 0 | 90 | 180 | 270; saturation?: number; sharpen?: number; trim?: | 'border' | { top?: number; bottom?: number; left?: number; right?: number; width?: number; height?: number; border?: | boolean | { color?: string; tolerance?: number; keep?: number; }; }; }; type ImageDrawOptions = { opacity?: number; repeat?: boolean | string; top?: number; left?: number; bottom?: number; right?: number; }; type ImageInputOptions = { encoding?: 'base64'; }; type ImageOutputOptions = { format: | 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp' | 'image/avif' | 'rgb' | 'rgba'; quality?: number; background?: string; anim?: boolean; }; interface ImageMetadata { id: string; filename?: string; uploaded?: string; requireSignedURLs: boolean; meta?: Record; variants: string[]; draft?: boolean; creator?: string; } interface ImageUploadOptions { id?: string; filename?: string; requireSignedURLs?: boolean; metadata?: Record; creator?: string; /** * If 'base64', the input data will be decoded from base64 before processing */ encoding?: 'base64'; } interface ImageUpdateOptions { requireSignedURLs?: boolean; metadata?: Record; creator?: string; } interface ImageListOptions { limit?: number; cursor?: string; sortOrder?: 'asc' | 'desc'; creator?: string; } interface ImageList { images: ImageMetadata[]; cursor?: string; listComplete: boolean; } interface ImageHandle { /** * Get metadata for a hosted image * @returns Image metadata, or null if not found */ details(): Promise; /** * Get the raw image data for a hosted image * @returns ReadableStream of image bytes, or null if not found */ bytes(): Promise | null>; /** * Update hosted image metadata * @param options Properties to update * @returns Updated image metadata * @throws {@link ImagesError} if update fails */ update(options: ImageUpdateOptions): Promise; /** * Delete a hosted image * @returns True if deleted, false if not found */ delete(): Promise; } interface HostedImagesBinding { /** * Get a handle for a hosted image * @param imageId The ID of the image (UUID or custom ID) * @returns A handle for per-image operations */ image(imageId: string): ImageHandle; /** * Upload a new hosted image * @param image The image file to upload * @param options Upload configuration * @returns Metadata for the uploaded image * @throws {@link ImagesError} if upload fails */ upload( image: ReadableStream | ArrayBuffer, options?: ImageUploadOptions ): Promise; /** * List hosted images with pagination * @param options List configuration * @returns List of images with pagination info * @throws {@link ImagesError} if list fails */ list(options?: ImageListOptions): Promise; } interface ImagesBinding { /** * Get image metadata (type, width and height) * @throws {@link ImagesError} with code 9412 if input is not an image * @param stream The image bytes */ info( stream: ReadableStream, options?: ImageInputOptions ): Promise; /** * Begin applying a series of transformations to an image * @param stream The image bytes * @returns A transform handle */ input( stream: ReadableStream, options?: ImageInputOptions ): ImageTransformer; /** * Access hosted images CRUD operations */ readonly hosted: HostedImagesBinding; } interface ImageTransformer { /** * Apply transform next, returning a transform handle. * You can then apply more transformations, draw, or retrieve the output. * @param transform */ transform(transform: ImageTransform): ImageTransformer; /** * Draw an image on this transformer, returning a transform handle. * You can then apply more transformations, draw, or retrieve the output. * @param image The image (or transformer that will give the image) to draw * @param options The options configuring how to draw the image */ draw( image: ReadableStream | ImageTransformer, options?: ImageDrawOptions ): ImageTransformer; /** * Retrieve the image that results from applying the transforms to the * provided input * @param options Options that apply to the output e.g. output format */ output(options: ImageOutputOptions): Promise; } type ImageTransformationOutputOptions = { encoding?: 'base64'; }; interface ImageTransformationResult { /** * The image as a response, ready to store in cache or return to users */ response(): Response; /** * The content type of the returned image */ contentType(): string; /** * The bytes of the response */ image(options?: ImageTransformationOutputOptions): ReadableStream; } interface ImagesError extends Error { readonly code: number; readonly message: string; readonly stack?: string; }