Class ImageUtils

Provides utility functions for image manipulation.

Methods

  • Converts between image formats using ImageMagick.

    Parameters

    • imageInput: string

      Path or data URL of the input image.

    • OptionaloutputPath: string

      Optional output path for the converted image. If not provided, a temporary file is used.

    • options: ConvertImageFormatOptions = {}

      Conversion options.

    Returns Promise<string>

    Data URL of the converted image.

    import {ImageUtils} from 'nodejs-shared';

    // Convert jpg to png and write in file.
    await ImageUtils.convertImageFormat('path/to/image.jpg', 'path/to/image.png');

    If the input file is not found or an error occurs during conversion.

  • Crops an image using Sharp.

    Parameters

    • inputPath: string

      Path to the input image file.

    • outputPath: string

      Path to save the cropped image.

    • options: CropOptions

      Cropping options.

    Returns Promise<void>

    import {ImageUtils} from 'nodejs-shared';

    ImageUtils.cropImage('path/to/image.jpg', 'path/to/image2.jpg', {left: 100, top: 100, width: 200, height: 200});
  • Extracts and saves the first frame of an animated GIF using ImageMagick.

    Parameters

    • imageInput: string

      Path or data URL of the animated GIF.

    • OptionaloutputPath: string

      Output path for the first frame. If not provided, the original file will be overwritten if it is a path; if it's a data URL, outputPath is required.

    Returns Promise<void>

    import {ImageUtils} from 'nodejs-shared';

    // Overwrite the first frame with the original file.
    await ImageUtils.extractFirstGifFrame('path/to/image.gif');

    // Write the first frame in a separate file.
    await ImageUtils.extractFirstGifFrame('path/to/image.gif', 'path/to/image2.gif');

    If the input file is not found, input is a data URL and no output path is specified, or an error occurs during processing.

  • Gets the number of frames in a GIF image using ImageMagick.

    Parameters

    • imageInput: string

      Path or data URL of the GIF image.

    Returns Promise<null | number>

    Number of frames, or null if not a GIF or an error occurred.

    import {ImageUtils} from 'nodejs-shared';

    await ImageUtils.getGifFrameCount('path/to/image.gif');

    If input is a file path and the file is not found.

  • Gets image dimensions using the image-size npm package.

    Parameters

    • filePath: string

      Path to the image file.

    Returns null | {
        height: number;
        width: number;
    }

    Image dimensions, or null if an error occurs or the image is invalid.

    import {ImageUtils} from 'nodejs-shared';

    ImageUtils.getImageDimensions('path/to/image.jpg');// {width: 960, height: 640}
  • Merges multiple images into a single image using Sharp.

    Parameters

    • inputPaths: string[]

      An array of paths to the input image files.

    • outputPath: string

      The path to save the merged image.

    • options: Partial<MergeImagesOptions> = {}

      Optional settings for merging images.

    Returns Promise<void>

    import {ImageUtils} from 'nodejs-shared';

    await ImageUtils.mergeImages(
    ['path/to/image1.jpg', 'path/to/image1.jpg'],
    'path/to/image3.jpg',
    {direction: 'vertical', offset: 30}
    );

    If inputPaths is not an array or if it's empty.

  • Resizes an image using Sharp.

    Parameters

    • inputPath: string

      Path to the input image file.

    • options: ResizeOptions = {}

      Resizing options.

    Returns Promise<void>

    import {ImageUtils} from 'nodejs-shared';

    // Overwrites the original file with the resized image.
    ImageUtils.resizeImage('path/to/image.jpg', {width: 200});

    // Write the resized image in another file.
    ImageUtils.resizeImage('path/to/image.jpg', {
    output: 'path/to/image2.jpg',
    width: 200,
    });