Generate SVG Or PNG Avatar Images From Usernames – profile-image.js

Category: Image , Javascript | September 22, 2022
Author:nkoepke
Views Total:118 views
Official Page:Go to website
Last Update:September 22, 2022
License:MIT

Preview:

Generate SVG Or PNG Avatar Images From Usernames – profile-image.js

Description:

A JavaScript library that generates SVG and PNG profile (avatar) images from any text. The resulting image is a simple portrait of the first letter in your name.

See Also:

How to use it:

1. Download and import the profile-image.js library.

<script src="src/profile-image.js"></script>

2. Create a new ProfileImage and define the username as follows:

var image = new ProfileImage("CSS Script");

3. Output the profile image as SVG.

image.svg();
// result
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 100 100' width='100' height='100' style='font-weight: normal;'>
  <rect width='100' height='100' x='0' y='0' fill='#1f6feb'></rect>
  <text x='50%' y='50%' alignment-baseline='central' text-anchor='middle' font-family='Arial' font-size='40' fill='#ffffff' dominant-baseline='middle'>CS</text>
</svg>

4. Output the profile image as base64 encoded PNG.

image.png();
// result
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAAAXNSR0IArs4c6QAAGRxJREFUeF7tnQmUFNXVx+/M9PQ2PTOgRMQlgogQJfoBIiiCfgouR0AFBAkB3FDcQhKJaDQqLhGiMdGgaEARiMAHMqBgFEREwY1F1igKqOCngBswW09Pbzm3hj4Mw9D1qvpVdz3fv87xHDxT/eq+3731r7fel3fCqO+ShAsEQEBLAnkQAC39jkqDgEEAAoBAAAGNCUAANHY+qg4CEADEAAhoTAACoLHzUXUQgAAgBkBAYwIQAI2dj6qDAAQAMQACGhOAAGjsfFQdBCAAiAEQ0JgABEBj56PqIAABQAyAgMYEIAAaOx9VBwEIAGIABDQmAAHQ2PmoOghAABADIKAxAQiAxs5H1UEAAoAYAAGNCUAANHY+qg4CEADEAAhoTAACoLHzUXUQgAAgBkBAYwIQAI2dj6qDAAQAMQACGhOAAGjsfFQdBCAAiAEQ0JgABEBj56PqIAABQAyAgMYEIAAa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BIKAOAQiAOr6CpSAgnQAEQDpSFAgC6hCAAKjjK1gKAtIJQACkI0WBIKAOAQiAOr6CpSAgnQAEQDpSFAgC6hCAAKjjK1gKAtIJQACkI0WBIKAOAQiAOr6CpSAgnQAEQDpSFAgC6hCAAKjjK1gKAtIJQACkI0WBIKAOAQiAOr6CpSAgnQAEQDpSFAgC6hCAAKjjK1gKAtIJQACkI0WBIKAOAQiAOr6CpSAgnQAEQDpSFAgC6hCAAKjjK1gKAtIJQACkI0WBIKAOAQiAOr6CpSAgnQAEQDpSFAgC6hCAAKjjK1gKAtIJQACkI0WBIKAOAQiAOr6CpSAgnQAEQDpSFAgC6hCAAKjjK1gKAtIJQACkI0WBIKAOAQiAOr6CpSAgnQAEQDpSFAgC6hCAAKjjK1gKAtIJQACkI0WBIKAOAQiAOr6CpSAgnQAEQDpSFAgC6hCAAKjjK1gKAtIJQACkI0WBIKAOAQiAOr6CpSAgncB/AVWdqZex3SUXAAAAAElFTkSuQmCC

5. Logs a color preview in the console.

image.previewInConsole();

6. Customize the profile image.

var image = new ProfileImage("CSS Script",{
    textColor: "#ffffff";
    backgroundColor: "#1f6feb";
    fontFamily: "Arial";
    fontSize: 40;
    fontWeight: "normal";
});

Changelog:

v0.0.4 (09/22/2022)

  • Update

You Might Be Interested In:


Leave a Reply