jdenticon.toPng()

Renders an indenticon and return a Buffer containing a PNG data stream.

jdenticon.toPng(hash|value, size[, config])
AvailabilityBrowserNode.js
jdenticon.toPng()NoYes

Examples

The following example shows how to use toPng to generate an identicon and save it to a file using Node.js.

Sample code

var jdenticon = require("jdenticon");
const fs = require("fs");

const png = jdenticon.toPng("icon value", 200);
fs.writeFileSync("./testicon.png", png);

Parameters

hash|value is considered a hash string if the string is hexadecimal and contains at least 11 characters. It is otherwise considered a value that will be hashed using SHA1.

size defines the width and height, icons are always square, of the icon in pixels, including its padding.

config (from v2.2.0) specifies an optional configuration object. If no configuration is specified, the global configuration is used. For backward compability this parameter can also be a padding value in the range 0.0 to 0.5.

Remarks

The following priority order is used to resolve the icon configuration when the icon is drawn:

  1. config parameter to toPng()
  2. Configuration object passed to jdenticon.configure().
  3. jdenticon_config global variable.

Why toPng is not supported in browsers

To generate PNG icons Jdenticon uses an internal PNG renderer. The renderer depends on several Node.js components that are not available in browsers. The most critical component that is only available on Node.js is a DEFLATE compressor.

There are polyfills available for browser support, but it's hard to justify adding the extra payload required for those polyfills when it is possible, and probably also faster, to just use the <canvas> or <svg> based renderers. The browser support for <canvas> and <svg> is also very good these days.