jdenticon.toPng()

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

jdenticon.toPng(hash|value, size[, padding])
Compability
Browser: No Node.js: Yes

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"),
    fs = require("fs"),
    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.

padding specifies the padding surrounding the icon in percents in the range 0.0 to 0.5. Default is 0.08.

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.