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 🙏

© 2024 – Pkg Stats / Ryan Hefner

svg2base64

v1.0.7

Published

Convert SVGs to images and output them to stdout as base64 (PhantomJS)

Downloads

26

Readme

Click here to get this package from www.npmjs.com.

Convert SVGs to images and output them to stdout as base64 (PhantomJS)

If you want to convert SVG to image from some other script like (PHP, Python, ...) or for whatever reason you need an image to be encoded in base64, you can use this.

Changelog:

v1.0.6 You can specify additional css and defs options to style your SVGs.

Examples:

var svg2base64 = require('../lib/svg2base64.js');

// svg string
var svg =
  '<svg xmlns="http://www.w3.org/2000/svg" width="300px" height="80">'
  + '<text x="150" y="40" dy=".4em" font-size="50px" text-anchor="middle">'
  + 'Hello World!'
  + '</text>'
  + '</svg>';

var scale = 4;

Convert svg to base64 encoded image

svg2base64(svg, {
  "scale": scale,
  "id": "svg",
  "type": 'png'
});

Convert svg to base64 encoded image with added css

svg2base64(svg, {
  "scale": scale,
  "id": "svgCss",
  "type": 'png',
  "css": "text { fill: #735ce8; stroke: #33d8b8; }"
});

Convert svg to base64 encoded image with gradient (css+defs)

svg2base64(svg, {
  "scale": scale,
  "id": "svgWithGradient",
  "type": 'png',
  "defs": 
  "<linearGradient id='gradient' "
  + "x1='0' y1='0' x2='" + 300 * scale + "' y2='0' "
  + "gradientUnits='userSpaceOnUse'> "
  + "<stop offset='0%' style='stop-color:#735ce8'/> "
  + "<stop offset='100%' style='stop-color:#33d8b8'/> "
  + "</linearGradient>",
  "css": "text { fill: url(#gradient); }"
});

You can run this from PHP for example:

exec('node example.js', $images);

foreach($images as $img) {
	$img = explode(' ', $trim($img))
	if(sizeof($img) == 2) {
		$imgID = $img[0];
		$imgData = $img[1];
		// Do with your base64 image what you want (we can just save it)
		file_put_contents($imgID.'.png', base64_decode($imgData));
	}
}

Usage:

svg2base64(svg, {options})

  1. svg: svg String
  2. options
    • 'scale': scale factor (default: 1)
    • 'id': useful (or basically needed) in parallel conversion. If you are converting multiple images in async, id must be used to determine which base64 string represents certain image. (default: empty string)
    • 'type': image type ('png' or 'jpeg'; default: 'png')
    • 'width': image width (default: svg width)
    • 'height': image height (default: svg height)
    • 'defs': defs string (default: empty string) MDN ref
    • 'css': css string (default: empty string)

Output:

Each base64 image string is outputted in one row of stdout. If id for the image is set, id and base64 image data are delimited with space char.

id base64_image id base64_image ...

mySvg iVBORw0KGgoAAAANSUhEUgAABNAAAAGACAYAAABshJ+1AAAABHNCSVQICAgIfAhkiAAAAA...