🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis β†’
Socket
Book a DemoInstallSign in
Socket

@svg-fns/svg2img

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@svg-fns/svg2img

Utility functions to convert SVGs into different formats (Base64, Blob, Data URI, raster images). Works in client and server with optimal performance.

latest
Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
2.2K
-3.44%
Maintainers
1
Weekly downloads
Β 
Created
Source

@svg-fns/svg2img

test codecov Version Downloads npm bundle size

Convert SVG to PNG, JPEG, WebP, AVIF, Data URL, Blob, or Buffer β€” isomorphic, tree-shakeable, and typed.

✨ Overview

@svg-fns/svg2img makes it effortless to transform raw SVG strings into modern image formats.

  • Isomorphic β†’ works in Browser (Canvas API) & Node.js (Sharp).
  • Tree-shakeable β†’ zero-bloat, minimal runtime.
  • Typed β†’ written in TypeScript for DX.
  • Modern formats β†’ PNG, JPEG, WebP, AVIF.
  • Resizing & fitting β†’ scale, width/height, cover/contain/fill.

πŸš€ Quick Start

import { svgToDataUrl } from "@svg-fns/svg2img";

const mySvg = `<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40" fill="royalblue" />
</svg>`;

// Convert to PNG Data URL at 200px wide
const pngDataUrl = await svgToDataUrl(mySvg, { format: "png", width: 200 });

const img = document.createElement("img");
img.src = pngDataUrl;
document.body.appendChild(img);

πŸ“¦ Installation

# Browser or universal usage
pnpm add @svg-fns/svg2img
npm install @svg-fns/svg2img
yarn add @svg-fns/svg2img

πŸ‘‰ For Node.js (Buffer conversion), install sharp:

pnpm add sharp

πŸ”‘ Core API

Browser

  • svgToBlob(svg, options) β†’ Blob
  • svgToDataUrl(svg, options) β†’ string (data URL)

Node.js

  • svgToBuffer(svg, options) β†’ Buffer
  • svgToDataUrlNode(svg, options) β†’ string (data URL)

Universal

  • svgToDataUrl(svg, options) β†’ auto-detects env β†’ returns Data URL

🎨 Examples

1. Browser β†’ JPEG Blob

import { svgToBlob } from "@svg-fns/svg2img";

const blob = await svgToBlob(mySvg, {
  format: "jpeg",
  quality: 0.8,
  background: "#fff",
});

const url = URL.createObjectURL(blob);
document.querySelector("img")!.src = url;

2. Node.js β†’ WebP file

import { svgToBuffer } from "@svg-fns/svg2img";
import { writeFile } from "fs/promises";

const buffer = await svgToBuffer(mySvg, {
  format: "webp",
  width: 500,
  height: 500,
  fit: "contain",
});

await writeFile("output.webp", buffer);

βš™οΈ Options

interface SvgConversionOptions {
  format?: "png" | "jpeg" | "webp" | "avif" | "svg";
  quality?: number; // 0–1, default: 0.92
  width?: number; // px
  height?: number; // px
  scale?: number; // multiplier, default: 1
  background?: string; // e.g. "#fff"
  fit?: "cover" | "contain" | "fill" | "inside" | "outside";
}
  • format β†’ output type (default: "svg").
  • fit β†’ how to resize (cover/crop vs contain/letterbox).
  • scale β†’ for high-DPI rendering.
  • background β†’ useful for JPEG (no alpha).

🀝 Contributing

We welcome issues & PRs!

❀️ Sponsor

If @svg-fns/svg2img saves you time, consider sponsoring. Your support fuels maintenance, features & community growth.

License

This library is licensed under the MPL-2.0 open-source license.

Please enroll in our courses or sponsor our work.

with πŸ’– by Mayank Kumar Chaudhari

Keywords

svg

FAQs

Package last updated on 02 Nov 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts