ImageUtils: {
    applyGrayscale(file): Promise<Blob>;
    calculateAspectRatio(width, height): number;
    base64ToBlob(base64, type?): Promise<Blob>;
    blobToBase64(blob): Promise<string>;
    compressImage(file, quality?): Promise<Blob>;
    convertBase64ToFile(base64, filename): null | Promise<File>;
    createThumbnail(file, maxSize?): Promise<string>;
    cropImage(file, x, y, width, height): Promise<Blob>;
    getImageMetadata(file): Promise<ImageMetadata>;
    getDominantColor(file): Promise<string>;
    getScaledDimensions(img, maxWidth?, maxHeight?): ScaledDimensions;
    imageExists(url): Promise<boolean>;
    isImageFile(file): boolean;
    loadImage(url): Promise<HTMLImageElement>;
    loadImageElement(url): Promise<null | HTMLImageElement>;
    resizeImage(file, options?): Promise<Blob>;
    rotateImage(file, degrees): Promise<Blob>;
    urlToFile(url, filename, mimeType): Promise<File>;
    validateImageDimensions(width, height, maxWidth, maxHeight): boolean;
} = ...

Type declaration

  • applyGrayscale:function
    • Applies a grayscale filter to an image by converting all pixels to their average RGB value.

      Parameters

      • file: File

        The image file to convert to grayscale

      Returns Promise<Blob>

      Promise resolving to the grayscale image as a Blob

      Example

      const grayscaleImage = await ImageUtils.applyGrayscale(imageFile);
      // Use the grayscale blob as needed
  • calculateAspectRatio:function
    • Calculates the aspect ratio of an image from its dimensions.

      Parameters

      • width: number

        The width of the image in pixels

      • height: number

        The height of the image in pixels

      Returns number

      The aspect ratio as a decimal number (width/height)

      Example

      const ratio = ImageUtils.calculateAspectRatio(1920, 1080); // Returns 1.777...
      const isWidescreen = ratio > 1.5; // true for 16:9 ratio
  • base64ToBlob:function
    • Converts a base64 string to a Blob object.

      Parameters

      • base64: string

        The base64 string to convert (without data URL prefix)

      • type: string = "image/jpeg"

        The MIME type of the image (default: "image/jpeg")

      Returns Promise<Blob>

      Promise resolving to a Blob object

      Example

      const blob = await ImageUtils.base64ToBlob(base64String, 'image/png');
      const url = URL.createObjectURL(blob);
  • blobToBase64:function
    • Converts a Blob to a base64 string (without data URL prefix).

      Parameters

      • blob: Blob

        The Blob to convert

      Returns Promise<string>

      Promise resolving to base64 string

      Example

      const base64 = await ImageUtils.blobToBase64(imageBlob);
      console.log('data:image/jpeg;base64,' + base64); // Full data URL
  • compressImage:function
    • Compresses an image file by reducing its quality while maintaining dimensions.

      Parameters

      • file: File

        The image file to compress

      • quality: number = 0.7

        Compression quality from 0 (lowest) to 1 (highest, default: 0.7)

      Returns Promise<Blob>

      Promise resolving to the compressed image as a Blob

      Example

      const compressed = await ImageUtils.compressImage(largeImage, 0.5);
      console.log(`Original: ${largeImage.size} bytes, Compressed: ${compressed.size} bytes`);
  • convertBase64ToFile:function
    • Converts a base64 data URL string to a File object.

      Parameters

      • base64: string

        The base64 data URL string (must include data:image/... prefix)

      • filename: string

        The name for the resulting file

      Returns null | Promise<File>

      Promise resolving to File object, or null if base64 is invalid

      Example

      const file = await ImageUtils.convertBase64ToFile(
      'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==',
      'pixel.png'
      );
  • createThumbnail:function
    • Creates a thumbnail image with specified maximum dimensions.

      Parameters

      • file: File

        The image file to create a thumbnail from

      • maxSize: number = 200

        Maximum size in pixels for the longest dimension (default: 200)

      Returns Promise<string>

      Promise resolving to thumbnail as base64 data URL string

      Example

      const thumbnail = await ImageUtils.createThumbnail(imageFile, 150);
      document.getElementById('preview').src = thumbnail;
  • cropImage:function
    • Crops an image to specified rectangular dimensions.

      Parameters

      • file: File

        The image file to crop

      • x: number

        Starting x coordinate for the crop (pixels from left)

      • y: number

        Starting y coordinate for the crop (pixels from top)

      • width: number

        Width of the crop area in pixels

      • height: number

        Height of the crop area in pixels

      Returns Promise<Blob>

      Promise resolving to the cropped image as a Blob

      Example

      // Crop a 200x200 square from the center of the image
      const cropped = await ImageUtils.cropImage(imageFile, 100, 100, 200, 200);
  • getImageMetadata:function
    • Extracts metadata information from an image file.

      Parameters

      • file: File

        The image file to analyze

      Returns Promise<ImageMetadata>

      Promise resolving to object containing width, height, type, and size

      Example

      const metadata = await ImageUtils.getImageMetadata(imageFile);
      console.log(`Image: ${metadata.width}x${metadata.height}, ${metadata.type}, ${metadata.size} bytes`);
  • getDominantColor:function
    • Analyzes an image to determine its most dominant color.

      Parameters

      • file: File

        The image file to analyze

      Returns Promise<string>

      Promise resolving to the dominant color as a hex string

      Example

      const dominantColor = await ImageUtils.getDominantColor(imageFile);
      console.log(`Dominant color: ${dominantColor}`); // e.g., "#ff5733"
      document.body.style.backgroundColor = dominantColor;
  • getScaledDimensions:function
    • Calculates scaled dimensions for an image while maintaining aspect ratio.

      Parameters

      • img: HTMLImageElement

        The HTML image element to scale

      • Optional maxWidth: number

        Optional maximum width constraint

      • Optional maxHeight: number

        Optional maximum height constraint

      Returns ScaledDimensions

      Object containing the calculated scaled width and height

      Example

      const scaled = ImageUtils.getScaledDimensions(imageElement, 800, 600);
      console.log(`Scaled dimensions: ${scaled.width}x${scaled.height}`);
  • imageExists:function
    • Checks if an image exists and is accessible at the given URL.

      Parameters

      • url: string

        The URL to check for image availability

      Returns Promise<boolean>

      Promise resolving to true if the image exists and is accessible

      Example

      const exists = await ImageUtils.imageExists('https://example.com/image.jpg');
      if (exists) {
      console.log('Image is available');
      }
  • isImageFile:function
    • Validates whether a file is an image based on its MIME type.

      Parameters

      • file: File

        The file to check

      Returns boolean

      True if the file is an image, false otherwise

      Example

      const isImage = ImageUtils.isImageFile(selectedFile);
      if (!isImage) {
      alert('Please select an image file');
      }
  • loadImage:function
    • Loads an image from a URL and returns the HTMLImageElement.

      Parameters

      • url: string

        The URL of the image to load

      Returns Promise<HTMLImageElement>

      Promise resolving to HTMLImageElement when image loads successfully

      Throws

      If the image fails to load

      Example

      try {
      const img = await ImageUtils.loadImage('https://example.com/image.jpg');
      console.log(`Loaded image: ${img.width}x${img.height}`);
      } catch (error) {
      console.error('Failed to load image:', error);
      }
  • loadImageElement:function
    • Loads and decodes an image from a URL with error handling.

      Parameters

      • url: string

        The URL of the image to load

      Returns Promise<null | HTMLImageElement>

      Promise resolving to HTMLImageElement or null if loading fails

      Example

      const img = await ImageUtils.loadImageElement('https://example.com/image.jpg');
      if (img) {
      document.body.appendChild(img);
      } else {
      console.log('Failed to load image');
      }
  • resizeImage:function
    • Resizes an image file to fit within specified dimensions while maintaining aspect ratio.

      Parameters

      • file: File

        The image file to resize

      • options: ImageResizeOptions = {}

        Resize options including max dimensions, quality, and output format

      Returns Promise<Blob>

      Promise resolving to the resized image as a Blob

      Example

      const resized = await ImageUtils.resizeImage(originalFile, {
      maxWidth: 800,
      maxHeight: 600,
      quality: 0.9,
      format: 'jpeg'
      });
  • rotateImage:function
    • Rotates an image by the specified number of degrees around its center.

      Parameters

      • file: File

        The image file to rotate

      • degrees: number

        Degrees to rotate (0-360, positive for clockwise)

      Returns Promise<Blob>

      Promise resolving to the rotated image as a Blob

      Example

      const rotated90 = await ImageUtils.rotateImage(imageFile, 90);
      const rotatedMinus45 = await ImageUtils.rotateImage(imageFile, -45);
  • urlToFile:function
    • Converts a URL to a File object by downloading the content.

      Parameters

      • url: string

        The URL to convert to a file

      • filename: string

        The name for the resulting file

      • mimeType: string

        The MIME type for the file

      Returns Promise<File>

      Promise resolving to a File object

      Example

      const file = await ImageUtils.urlToFile(
      'https://example.com/image.jpg',
      'downloaded-image.jpg',
      'image/jpeg'
      );
  • validateImageDimensions:function
    • Validates that image dimensions are within specified limits.

      Parameters

      • width: number

        The width to validate

      • height: number

        The height to validate

      • maxWidth: number

        The maximum allowed width

      • maxHeight: number

        The maximum allowed height

      Returns boolean

      True if dimensions are within limits, false otherwise

      Example

      const isValid = ImageUtils.validateImageDimensions(1920, 1080, 2000, 2000);
      if (!isValid) {
      console.log('Image is too large');
      }