Skip to content

@dovenv/utils-media - API documentation โ€‹

Functions โ€‹

base642ImageBuffer() โ€‹

ts
function base642ImageBuffer(input: string): Promise<Buffer<ArrayBuffer>>

Converts a base64-encoded image string into a Buffer.

Parameters โ€‹

ParameterTypeDescription
inputstringThe base64 string representing the image, including the data URI scheme.

Returns โ€‹

Promise<Buffer<ArrayBuffer>>

  • A promise that resolves to a Buffer containing the image data.

Throws โ€‹

  • If the input string is not a valid base64 image string.

gif() โ€‹

ts
function gif(params: {
  asciiOptions: {
     c_ratio: number;
     chars: string;
     color: boolean;
     fit:   | "box"
        | "width"
        | "height"
        | "original"
        | "none";
    };
  asciiOutput: boolean;
  input: MediaInput;
 }): Promise<{
  start: () => void;
  stop: () => void;
}>

Displays a GIF in the terminal.

Parameters โ€‹

ParameterTypeDescription
paramsobjectOptions for displaying the GIF.
params.asciiOptions?objectOptions for asciiOutput.
params.asciiOptions.c_ratio?numberSince a monospace character is taller than it is wide, this property defines the integer approximation of the ratio of the width to height. You probably don't need to change this. Default 2
params.asciiOptions.chars?stringThe characters to use for the asciified image. Default .,:;i1tfLCG08@
params.asciiOptions.color?booleanDefines if the output should be colored (true) or black and white (false) Default true
params.asciiOptions.fit?| "box" | "width" | "height" | "original" | "none"The fit to resize the image to: โ€ข box - Resize the image such that it fits inside a bounding box defined by the specified width and height. Maintains aspect ratio. โ€ข width - Resize the image by scaling the width to the specified width. Maintains aspect ratio. โ€ข height - Resize the image by scaling the height to the specified height. Maintains aspect ratio. โ€ข original - Doesn't resize the image. โ€ข none - Scales the width and height to the specified values, ignoring original aspect ratio. Default box
params.asciiOutput?booleanEnable a ascii output. Default false
params.inputMediaInputInput to the media PATH, URL, STRING or BUFFER.

Returns โ€‹

Promise<{ start: () => void; stop: () => void; }>

  • A promise that resolves with an object containing a single method stop(). Calling stop() will clear the GIF from the terminal.
NameType
start() => void
stop() => void

Example โ€‹

ts
// simple use with url
const myGif = await gif({
  input: 'https://64.media.tumblr.com/38adef3da23d26058e3085ce271b39c1/tumblr_nil77wk20l1qhnszoo1_400.gifv'
});
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
await myGif.start();
await delay(5000);
await myGif.stop();
await delay(2000);
await myGif.start();

gif2ascii() โ€‹

ts
function gif2ascii(params: {
  animate: Omit<AnimateProps, "frames">;
  c_ratio: number;
  chars: string;
  color: boolean;
  fit:   | "box"
     | "width"
     | "height"
     | "original"
     | "none";
  height: string | number;
  input: MediaInput;
  width: string | number;
 }): Promise<{
  start: () => void;
  stop: () => void;
}>

Converts a GIF to an ASCII animation.

Parameters โ€‹

ParameterTypeDescription
paramsobjectOptions for converting the GIF.
params.animate?Omit<AnimateProps, "frames">Options for the animation.
params.c_ratio?numberSince a monospace character is taller than it is wide, this property defines the integer approximation of the ratio of the width to height. You probably don't need to change this. Default 2
params.chars?stringThe characters to use for the asciified image. Default .,:;i1tfLCG08@
params.color?booleanDefines if the output should be colored (true) or black and white (false) Default true
params.fit?| "box" | "width" | "height" | "original" | "none"The fit to resize the image to: โ€ข box - Resize the image such that it fits inside a bounding box defined by the specified width and height. Maintains aspect ratio. โ€ข width - Resize the image by scaling the width to the specified width. Maintains aspect ratio. โ€ข height - Resize the image by scaling the height to the specified height. Maintains aspect ratio. โ€ข original - Doesn't resize the image. โ€ข none - Scales the width and height to the specified values, ignoring original aspect ratio. Default box
params.height?string | numberThe height to resize the image to. Use a percentage to set the image width to x% of the terminal window height. Default 100%
params.inputMediaInputInput to the media PATH, URL, STRING or BUFFER.
params.width?string | numberThe width to resize the image to. Use a percentage to set the image width to x% of the terminal window width. Default 100%

Returns โ€‹

Promise<{ start: () => void; stop: () => void; }>

  • A promise that resolves with a string containing the ASCII animation.
NameType
start() => void
stop() => void

gif2asciiArray() โ€‹

ts
function gif2asciiArray(params: {
  c_ratio: number;
  chars: string;
  color: boolean;
  fit:   | "box"
     | "width"
     | "height"
     | "original"
     | "none";
  height: string | number;
  input: MediaInput;
  width: string | number;
}): Promise<string[]>

Converts each frame of a GIF image to an ASCII string.

Parameters โ€‹

ParameterTypeDescription
paramsobjectOptions for converting the GIF.
params.c_ratio?numberSince a monospace character is taller than it is wide, this property defines the integer approximation of the ratio of the width to height. You probably don't need to change this. Default 2
params.chars?stringThe characters to use for the asciified image. Default .,:;i1tfLCG08@
params.color?booleanDefines if the output should be colored (true) or black and white (false) Default true
params.fit?| "box" | "width" | "height" | "original" | "none"The fit to resize the image to: โ€ข box - Resize the image such that it fits inside a bounding box defined by the specified width and height. Maintains aspect ratio. โ€ข width - Resize the image by scaling the width to the specified width. Maintains aspect ratio. โ€ข height - Resize the image by scaling the height to the specified height. Maintains aspect ratio. โ€ข original - Doesn't resize the image. โ€ข none - Scales the width and height to the specified values, ignoring original aspect ratio. Default box
params.height?string | numberThe height to resize the image to. Use a percentage to set the image width to x% of the terminal window height. Default 100%
params.inputMediaInputInput to the media PATH, URL, STRING or BUFFER.
params.width?string | numberThe width to resize the image to. Use a percentage to set the image width to x% of the terminal window width. Default 100%

Returns โ€‹

Promise<string[]>

  • A promise that resolves with an array of ASCII strings, each representing a frame of the GIF.

gif2images() โ€‹

ts
function gif2images(params: {
  input: MediaInput;
}): Promise<Buffer<ArrayBufferLike>[]>

Extracts frames from a GIF image and returns them as an array of buffers.

Parameters โ€‹

ParameterTypeDescription
paramsobjectOptions for extracting frames from the GIF.
params.inputMediaInputInput to the media PATH, URL, STRING or BUFFER.

Returns โ€‹

Promise<Buffer<ArrayBufferLike>[]>

  • A promise that resolves with an array of buffers, each representing a frame of the GIF.

image() โ€‹

ts
function image(params: ImageProps): Promise<string>

Return an image for been print.

Parameters โ€‹

ParameterTypeDescription
paramsImagePropsOptions to customize the display of the image.

Returns โ€‹

Promise<string>

  • Promise that resolves with the image formatted for the terminal.

Examples โ€‹

ts
// simple use with url
const IMG = await image( {
   input: 'https://avatars.githubusercontent.com/u/111685953'
});
console.log( IMG );
ts
// simple use with path
const IMG = await image( {
  input: './image.png'
});
console.log( IMG );
ts
// ascii output
const IMG = await image( {
  input: 'https://avatars.githubusercontent.com/u/111685953',
  asciiOutput: true
});
console.log(IMG);
ts
// ascii output with custom opts
const IMG = await image( {
   input: 'https://avatars.githubusercontent.com/u/111685953',
   width: '100%',
   height: '100%',
   preserveAspectRatio: true,
   asciiOutput: true,
   asciiOptions: {
      chars: ' #*',
   }
});
console.log(IMG);

image2ascii() โ€‹

ts
function image2ascii(params: Image2AsciiProps): Promise<string>

Converts an image to ASCII art.

Parameters โ€‹

ParameterTypeDescription
paramsImage2AsciiPropsParameters for the conversion.

Returns โ€‹

Promise<string>

  • Promise that resolves to the ASCII representation of the image.

Examples โ€‹

ts
// simple use with url
const IMG = await image2ascii( {
   input: 'https://avatars.githubusercontent.com/u/111685953'
});
console.log( IMG );
ts
// simple use with path
const IMG = await image2ascii( {
  input: './image.png'
});
console.log( IMG );
ts

svg2ascii() โ€‹

ts
function svg2ascii(params: {
  c_ratio: number;
  chars: string;
  color: boolean;
  fit:   | "box"
     | "width"
     | "height"
     | "original"
     | "none";
  height: string | number;
  input: IconDefinition | MediaInput;
  svgOptions: Svg2ImgCoreProps;
  width: string | number;
}): Promise<string>

Converts an SVG to ASCII art.

Parameters โ€‹

ParameterTypeDescription
paramsobjectParameters for the conversion.
params.c_ratio?numberSince a monospace character is taller than it is wide, this property defines the integer approximation of the ratio of the width to height. You probably don't need to change this. Default 2
params.chars?stringThe characters to use for the asciified image. Default .,:;i1tfLCG08@
params.color?booleanDefines if the output should be colored (true) or black and white (false) Default true
params.fit?| "box" | "width" | "height" | "original" | "none"The fit to resize the image to: โ€ข box - Resize the image such that it fits inside a bounding box defined by the specified width and height. Maintains aspect ratio. โ€ข width - Resize the image by scaling the width to the specified width. Maintains aspect ratio. โ€ข height - Resize the image by scaling the height to the specified height. Maintains aspect ratio. โ€ข original - Doesn't resize the image. โ€ข none - Scales the width and height to the specified values, ignoring original aspect ratio. Default box
params.height?string | numberThe height to resize the image to. Use a percentage to set the image width to x% of the terminal window height. Default 100%
params.inputIconDefinition | MediaInputInput to the media PATH, URL, STRING, BUFFER or IconDefinition (FONTAWESOME).
params.svgOptions?Svg2ImgCorePropsSvg options.
params.width?string | numberThe width to resize the image to. Use a percentage to set the image width to x% of the terminal window width. Default 100%

Returns โ€‹

Promise<string>

  • Promise that resolves to the ASCII representation of the SVG.

Examples โ€‹

ts
// simple use with string
const svg = `<svg width="100" height="100">
  <rect width="100%" height="100%" fill="red" />
</svg>`
const ascii = await svg2ascii( { input: svg } )
console.log( ascii )
ts
// simple use with url
const svg = `https://my-web.com/my-svg-code.svg`
const ascii = await svg2ascii( { input: svg } )
console.log( ascii )
ts
// simple use with path
const svg = `./my-svg-path.svg`
const ascii = await svg2ascii( { input: svg } )
console.log( ascii )

svg2imageBuffer() โ€‹

ts
function svg2imageBuffer(params: {
  input: IconDefinition | MediaInput;
  svgOptions: Svg2ImgCoreProps;
}): Promise<Buffer<ArrayBufferLike>>

Converts an SVG to an image buffer.

Parameters โ€‹

ParameterTypeDescription
paramsobjectParameters for the conversion.
params.inputIconDefinition | MediaInputInput to the media PATH, URL, STRING, BUFFER or IconDefinition (FONTAWESOME).
params.svgOptions?Svg2ImgCorePropsSvg options.

Returns โ€‹

Promise<Buffer<ArrayBufferLike>>

  • A promise that resolves to the image buffer.

svg2terminal() โ€‹

ts
function svg2terminal(params: {
  asciiOptions: {
     c_ratio: number;
     chars: string;
     color: boolean;
     fit:   | "box"
        | "width"
        | "height"
        | "original"
        | "none";
    };
  asciiOutput: boolean;
  height: string | number;
  input: IconDefinition | MediaInput;
  preserveAspectRatio: boolean;
  svgOptions: Svg2ImgCoreProps;
  width: string | number;
}): Promise<string>

Convert SVG to image string for terminal display.

Parameters โ€‹

ParameterTypeDescription
paramsobjectOptions object.
params.asciiOptions?objectOptions for asciiOutput.
params.asciiOptions.c_ratio?numberSince a monospace character is taller than it is wide, this property defines the integer approximation of the ratio of the width to height. You probably don't need to change this. Default 2
params.asciiOptions.chars?stringThe characters to use for the asciified image. Default .,:;i1tfLCG08@
params.asciiOptions.color?booleanDefines if the output should be colored (true) or black and white (false) Default true
params.asciiOptions.fit?| "box" | "width" | "height" | "original" | "none"The fit to resize the image to: โ€ข box - Resize the image such that it fits inside a bounding box defined by the specified width and height. Maintains aspect ratio. โ€ข width - Resize the image by scaling the width to the specified width. Maintains aspect ratio. โ€ข height - Resize the image by scaling the height to the specified height. Maintains aspect ratio. โ€ข original - Doesn't resize the image. โ€ข none - Scales the width and height to the specified values, ignoring original aspect ratio. Default box
params.asciiOutput?booleanEnable a ascii output. Default false
params.height?string | numberCustom image height. Can be set as percentage or number of rows of the terminal. It is recommended to use the percentage options.
params.inputIconDefinition | MediaInputInput to the media PATH, URL, STRING, BUFFER or IconDefinition (FONTAWESOME).
params.preserveAspectRatio?booleanIf false, the aspect ratio will not be preserved . Default true
params.svgOptions?Svg2ImgCorePropsSvg options.
params.width?string | numberCustom image width. Can be set as percentage or number of columns of the terminal. It is recommended to use the percentage options.

Returns โ€‹

Promise<string>

  • Image buffer.

Examples โ€‹

ts
// simple use with string
const svg = `<svg width="100" height="100">
  <rect width="100%" height="100%" fill="red" />
</svg>`
const output = await svg2terminal( { input: svg } )
console.log( output )
ts
// simple use with url
const svg = `https://my-web.com/my-svg-code.svg`
const output = await svg2terminal( { input: svg } )
console.log( output )
ts
// simple use with path
const svg = `./my-svg-path.svg`
const output = await svg2terminal( { input: svg } )
console.log( output )

text2image() โ€‹

ts
function text2image(params: Text2ImageProps): Promise<Buffer<ArrayBufferLike>>

Converts text to an image.

Parameters โ€‹

ParameterTypeDescription
paramsText2ImagePropsParameters to convert text to image.

Returns โ€‹

Promise<Buffer<ArrayBufferLike>>

The image buffer.

Example โ€‹

ts
const buffer = await text2image( {
  input : 'Hello world!',
  fontSize : 42,
  backgroundColor : '#fff',
} )

writeImageFromBase64() โ€‹

ts
function writeImageFromBase64(opts: {
  input: string;
  output: string;
}): Promise<void>

Writes a base64-encoded image to a file.

Parameters โ€‹

ParameterTypeDescription
optsobjectThe options object containing the input and output file paths.
opts.inputstring-
opts.outputstring-

Returns โ€‹

Promise<void>

  • A promise that resolves when the file has been written.

Type Aliases โ€‹

Gif2AsciiArrayProps โ€‹

ts
type Gif2AsciiArrayProps: Prettify<MediaSharedProps & Omit<AsciifyOptions, "input">>;

Gif2AsciiProps โ€‹

ts
type Gif2AsciiProps: Prettify<Gif2AsciiArrayProps & {
  animate: Omit<Parameters<typeof animate>[0], "frames">;
}>;

Gif2ImagesProps โ€‹

ts
type Gif2ImagesProps: Prettify<MediaSharedProps>;

GifProps โ€‹

ts
type GifProps: Prettify<MediaSharedProps & Exclude<GifOptions, undefined> & AsciiOpts>;

Image2AsciiProps โ€‹

ts
type Image2AsciiProps: MediaSharedProps & AsciifyOptions;

ImageProps โ€‹

ts
type ImageProps: MediaSharedProps & ImageParams & AsciiOpts;

Svg2AsciiProps โ€‹

ts
type Svg2AsciiProps: Prettify<SvgSharedProps & Omit<AsciifyOptions, "input">>;

Svg2ImageProps โ€‹

ts
type Svg2ImageProps: Prettify<SvgSharedProps>;

SvgProps โ€‹

ts
type SvgProps: Prettify<SvgSharedProps & Omit<ImageProps, "input">>;

Text2ImageProps โ€‹

ts
type Text2ImageProps: {
  input: string;
} & Partial<IOptions>;

Type declaration โ€‹

NameType
inputstring

Variables โ€‹

svg โ€‹

ts
const svg: {
  deserialize: _parse;
  serialize: _stringify;
};

Type declaration โ€‹

NameTypeDefault value
deserialize(input: string, options?: IParseOptions) => Promise<INode>_parse
serialize(ast: INode, options?: IStringifyOptions) => string_stringify