Static
isStatic
parseParses a data URL and extracts its components (MIME type, base64 data, file extension, and byte size). Normalizes "jpeg" extension to "jpg".
The data URL to parse.
An object containing the parsed type
, base64
data, extension
, and bytesize
, or null
if the input is not a valid data URL.
import {MediaUtils} from 'nodejs-shared';
// Parse a data URL to extract its MIME type, base64 encoded data, file extension, and size.
const dataUrlParts = MediaUtils.parseDataUrl('data:image/jpeg;base64,AA==...');
console.log(dataUrlParts);
// {
// mimeType: 'image/jpeg',
// base64: '/9j/4AAQSk...',
// extension: 'jpg',
// bytesize: 45056
// }
Static
writeWrites image data to a file.
The path to the output file. The file extension must be provided.
The image data as a data URL, Buffer, or SVG string.
File write options.
import {MediaUtils} from 'nodejs-shared';
// Write a JPEG image using a data URL.
MediaUtils.writeImage('path/to/image.jpg', 'data:image/jpeg;base64,AA==...');
// Write a PNG image using a Buffer.
MediaUtils.writeImage('path/to/image.png', Buffer.from([...]));
// Write an SVG image using a string.
writeImage('path/to/image.svg', '<svg>...</svg>');
// Write with file system options.
MediaUtils.writeImage('path/to/image.png', Buffer.from([...]), {mode: 0o644, owner: {username: 'nginx', groupName: 'nginx'}});
Utility class for media manipulation, including image processing and data URL handling.