azion
Version:
Azion Packages for Edge Computing.
154 lines (150 loc) • 4.92 kB
TypeScript
/**
* Provides the image's height, width, and contains the image's raw pixels.
* For use when communicating between JS and WASM, and also natively.
*/
declare class PhotonImage$1 {
free(): void;
/**
* Create a new PhotonImage from a Vec of u8s, which represent raw pixels.
* @param {Uint8Array} raw_pixels
* @param {number} width
* @param {number} height
*/
constructor(raw_pixels: Uint8Array, width: number, height: number);
/**
* Create a new PhotonImage from a base64 string.
* @param {string} base64
* @returns {PhotonImage}
*/
static new_from_base64(base64: string): PhotonImage$1;
/**
* Create a new PhotonImage from a byteslice.
* @param {Uint8Array} vec
* @returns {PhotonImage}
*/
static new_from_byteslice(vec: Uint8Array): PhotonImage$1;
/**
* Create a new PhotonImage from a Blob/File.
* @param {Blob} blob
* @returns {PhotonImage}
*/
static new_from_blob(blob: Blob): PhotonImage$1;
/**
* Create a new PhotonImage from a HTMLImageElement
* @param {HTMLImageElement} image
* @returns {PhotonImage}
*/
static new_from_image(image: HTMLImageElement): PhotonImage$1;
/**
* Get the width of the PhotonImage.
* @returns {number}
*/
get_width(): number;
/**
* Get the PhotonImage's pixels as a Vec of u8s.
* @returns {Uint8Array}
*/
get_raw_pixels(): Uint8Array;
/**
* Get the height of the PhotonImage.
* @returns {number}
*/
get_height(): number;
/**
* Convert the PhotonImage to base64.
* @returns {string}
*/
get_base64(): string;
/**
* Convert the PhotonImage to raw bytes. Returns PNG.
* @returns {Uint8Array}
*/
get_bytes(): Uint8Array;
/**
* Convert the PhotonImage to raw bytes. Returns a JPEG.
* @param {number} quality
* @returns {Uint8Array}
*/
get_bytes_jpeg(quality: number): Uint8Array;
/**
* Convert the PhotonImage to raw bytes. Returns a WEBP.
* @returns {Uint8Array}
*/
get_bytes_webp(): Uint8Array;
/**
* Convert the PhotonImage's raw pixels to JS-compatible ImageData.
* @returns {ImageData}
*/
get_image_data(): ImageData;
/**
* Convert ImageData to raw pixels, and update the PhotonImage's raw pixels to this.
* @param {ImageData} img_data
*/
set_imgdata(img_data: ImageData): void;
}
interface WasmImage {
/** The underlying PhotonImage object. */
image: PhotonImage;
/** Get the width of the image. */
width(): number;
/** Get the height of the image. */
height(): number;
/**
* Resize the image.
* @param width - The new width.
* @param height - The new height.
* @param usePercent - If true, width and height are treated as percentages.
*/
resize(width: number, height: number, usePercent?: boolean): WasmImage;
/**
* Get the image as a Response object in the specified format.
* @param format - The desired image format ('webp', 'jpeg', or 'png').
* @param quality - The quality of the image (0-100), only applicable for 'jpeg' format.
* @throws {Error} If an unsupported image format is specified.
*/
getImageResponse(format: SupportedImageFormat, quality?: number): Response;
/** Clean up resources associated with the image. */
clean(): void;
}
type SupportedImageFormat = 'webp' | 'jpeg' | 'png';
type PhotonImage = PhotonImage$1;
/**
* Resizes the given image.
*/
declare function resize(image: PhotonImage$1, width: number, height: number, usePercent?: boolean): PhotonImage$1;
/**
* Gets the image as a Response object in the specified format.
*/
declare function getImageResponse(image: PhotonImage$1, format: SupportedImageFormat, quality?: number): Response;
/**
* Cleans up resources associated with the image.
* @param {photon.PhotonImage} image - The image to clean up.
*/
declare function clean(image: PhotonImage$1): void;
/**
* Loads an image from a given path or URL and returns a WasmImage object.
*
* @param {string} pathOrURL - The path or URL of the image to load.
* @returns {Promise<WasmImage>} A promise that resolves to a WasmImage object.
*
* @throws {Error} If the image extension is invalid or if there's an error fetching the image.
*
* @example
* // Load an image from a URL
* const imageProcessor = await loadImage('https://example.com/image.jpg');
*
* // Get the image dimensions
* const width = imageProcessor.width();
* const height = imageProcessor.height();
*
* // Resize the image
* const resizedImage = imageProcessor.resize(800, 600, false);
*
* // Get the image as a webp response
* const webpResponse = resizedImage.getImageResponse('webp');
*
* // Clean up resources
* imageProcessor.clean();
*/
declare function loadImage(pathOrURL: string): Promise<WasmImage>;
export { type PhotonImage, type SupportedImageFormat, type WasmImage, clean, loadImage as default, getImageResponse, loadImage, resize };