npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@svg-fns/svg2img

v0.2.0

Published

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

Readme

@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.