@dovenv/utils-media - API documentation β
Functions β
base642ImageBuffer() β
function base642ImageBuffer(input: string): Promise<Buffer<ArrayBuffer>>Converts a base64-encoded image string into a Buffer.
Parameters β
| Parameter | Type | Description |
|---|---|---|
input | string | The 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.
createBadgeSVG() β
function createBadgeSVG(data: Format): Promise<string>Cheate shields.io SVGs.
Parameters β
| Parameter | Type | Description |
|---|---|---|
data | Format | The format of the badge. |
Returns β
Promise<string>
The SVG code.
See β
https://www.npmjs.com/package/badge-maker
gif() β
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 β
| Parameter | Type | Description |
|---|---|---|
params | object | Options for displaying the GIF. |
params.asciiOptions? | object | Options for asciiOutput. |
params.asciiOptions.c_ratio? | number | Since 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? | string | The characters to use for the asciified image. Default .,:;i1tfLCG08@ |
params.asciiOptions.color? | boolean | Defines 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? | boolean | Enable a ascii output. Default false |
params.input | MediaInput | Input 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(). Callingstop()will clear the GIF from the terminal.
| Name | Type |
|---|---|
start | () => void |
stop | () => void |
Example β
// 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() β
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 β
| Parameter | Type | Description |
|---|---|---|
params | object | Options for converting the GIF. |
params.animate? | Omit<AnimateProps, "frames"> | Options for the animation. |
params.c_ratio? | number | Since 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? | string | The characters to use for the asciified image. Default .,:;i1tfLCG08@ |
params.color? | boolean | Defines 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 | number | The height to resize the image to. Use a percentage to set the image width to x% of the terminal window height. Default 100% |
params.input | MediaInput | Input to the media PATH, URL, STRING or BUFFER. |
params.width? | string | number | The 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.
| Name | Type |
|---|---|
start | () => void |
stop | () => void |
gif2asciiArray() β
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 β
| Parameter | Type | Description |
|---|---|---|
params | object | Options for converting the GIF. |
params.c_ratio? | number | Since 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? | string | The characters to use for the asciified image. Default .,:;i1tfLCG08@ |
params.color? | boolean | Defines 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 | number | The height to resize the image to. Use a percentage to set the image width to x% of the terminal window height. Default 100% |
params.input | MediaInput | Input to the media PATH, URL, STRING or BUFFER. |
params.width? | string | number | The 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() β
function gif2images(params: {
input: MediaInput;
}): Promise<Buffer<ArrayBufferLike>[]>Extracts frames from a GIF image and returns them as an array of buffers.
Parameters β
| Parameter | Type | Description |
|---|---|---|
params | object | Options for extracting frames from the GIF. |
params.input | MediaInput | Input 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() β
function image(params: ImageProps): Promise<string>Return an image for been print.
Parameters β
| Parameter | Type | Description |
|---|---|---|
params | ImageProps | Options to customize the display of the image. |
Returns β
Promise<string>
- Promise that resolves with the image formatted for the terminal.
Examples β
// simple use with url
const IMG = await image( {
input: 'https://avatars.githubusercontent.com/u/111685953'
});
console.log( IMG );// simple use with path
const IMG = await image( {
input: './image.png'
});
console.log( IMG );// ascii output
const IMG = await image( {
input: 'https://avatars.githubusercontent.com/u/111685953',
asciiOutput: true
});
console.log(IMG);// 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() β
function image2ascii(params: Image2AsciiProps): Promise<string>Converts an image to ASCII art.
Parameters β
| Parameter | Type | Description |
|---|---|---|
params | Image2AsciiProps | Parameters for the conversion. |
Returns β
Promise<string>
- Promise that resolves to the ASCII representation of the image.
Examples β
// simple use with url
const IMG = await image2ascii( {
input: 'https://avatars.githubusercontent.com/u/111685953'
});
console.log( IMG );// simple use with path
const IMG = await image2ascii( {
input: './image.png'
});
console.log( IMG );svg2ascii() β
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 β
| Parameter | Type | Description |
|---|---|---|
params | object | Parameters for the conversion. |
params.c_ratio? | number | Since 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? | string | The characters to use for the asciified image. Default .,:;i1tfLCG08@ |
params.color? | boolean | Defines 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 | number | The height to resize the image to. Use a percentage to set the image width to x% of the terminal window height. Default 100% |
params.input | IconDefinition | MediaInput | Input to the media PATH, URL, STRING, BUFFER or IconDefinition (FONTAWESOME). |
params.svgOptions? | Svg2ImgCoreProps | Svg options. |
params.width? | string | number | The 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 β
// 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 )// simple use with url
const svg = `https://my-web.com/my-svg-code.svg`
const ascii = await svg2ascii( { input: svg } )
console.log( ascii )// simple use with path
const svg = `./my-svg-path.svg`
const ascii = await svg2ascii( { input: svg } )
console.log( ascii )svg2imageBuffer() β
function svg2imageBuffer(params: {
input: IconDefinition | MediaInput;
svgOptions: Svg2ImgCoreProps;
}): Promise<Buffer<ArrayBufferLike>>Converts an SVG to an image buffer.
Parameters β
| Parameter | Type | Description |
|---|---|---|
params | object | Parameters for the conversion. |
params.input | IconDefinition | MediaInput | Input to the media PATH, URL, STRING, BUFFER or IconDefinition (FONTAWESOME). |
params.svgOptions? | Svg2ImgCoreProps | Svg options. |
Returns β
Promise<Buffer<ArrayBufferLike>>
- A promise that resolves to the image buffer.
svg2terminal() β
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 β
| Parameter | Type | Description |
|---|---|---|
params | object | Options object. |
params.asciiOptions? | object | Options for asciiOutput. |
params.asciiOptions.c_ratio? | number | Since 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? | string | The characters to use for the asciified image. Default .,:;i1tfLCG08@ |
params.asciiOptions.color? | boolean | Defines 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? | boolean | Enable a ascii output. Default false |
params.height? | string | number | Custom image height. Can be set as percentage or number of rows of the terminal. It is recommended to use the percentage options. |
params.input | IconDefinition | MediaInput | Input to the media PATH, URL, STRING, BUFFER or IconDefinition (FONTAWESOME). |
params.preserveAspectRatio? | boolean | If false, the aspect ratio will not be preserved . Default true |
params.svgOptions? | Svg2ImgCoreProps | Svg options. |
params.width? | string | number | Custom 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 β
// 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 )// simple use with url
const svg = `https://my-web.com/my-svg-code.svg`
const output = await svg2terminal( { input: svg } )
console.log( output )// simple use with path
const svg = `./my-svg-path.svg`
const output = await svg2terminal( { input: svg } )
console.log( output )text2image() β
function text2image(params: Text2ImageProps): Promise<Buffer<ArrayBufferLike>>Converts text to an image.
Parameters β
| Parameter | Type | Description |
|---|---|---|
params | Text2ImageProps | Parameters to convert text to image. |
Returns β
Promise<Buffer<ArrayBufferLike>>
The image buffer.
Example β
const buffer = await text2image( {
input : 'Hello world!',
fontSize : 42,
backgroundColor : '#fff',
} )writeImageFromBase64() β
function writeImageFromBase64(opts: {
input: string;
output: string;
}): Promise<void>Writes a base64-encoded image to a file.
Parameters β
| Parameter | Type | Description |
|---|---|---|
opts | object | The options object containing the input and output file paths. |
opts.input | string | - |
opts.output | string | - |
Returns β
Promise<void>
- A promise that resolves when the file has been written.
Type Aliases β
Gif2AsciiArrayProps β
type Gif2AsciiArrayProps: Prettify<MediaSharedProps & Omit<AsciifyOptions, "input">>;Gif2AsciiProps β
type Gif2AsciiProps: Prettify<Gif2AsciiArrayProps & {
animate: Omit<Parameters<typeof animate>[0], "frames">;
}>;Gif2ImagesProps β
type Gif2ImagesProps: Prettify<MediaSharedProps>;GifProps β
type GifProps: Prettify<MediaSharedProps & Exclude<GifOptions, undefined> & AsciiOpts>;Image2AsciiProps β
type Image2AsciiProps: MediaSharedProps & AsciifyOptions;ImageProps β
type ImageProps: MediaSharedProps & ImageParams & AsciiOpts;Svg2AsciiProps β
type Svg2AsciiProps: Prettify<SvgSharedProps & Omit<AsciifyOptions, "input">>;Svg2ImageProps β
type Svg2ImageProps: Prettify<SvgSharedProps>;SvgProps β
type SvgProps: Prettify<SvgSharedProps & Omit<ImageProps, "input">>;Text2ImageProps β
type Text2ImageProps: {
input: string;
} & Partial<IOptions>;Type declaration β
| Name | Type |
|---|---|
input | string |
Variables β
svg β
const svg: {
deserialize: _parse;
serialize: _stringify;
};Type declaration β
| Name | Type | Default value |
|---|---|---|
deserialize | (input: string, options?: IParseOptions) => Promise<INode> | _parse |
serialize | (ast: INode, options?: IStringifyOptions) => string | _stringify |
