Profile-Image
This JavaScript class generates a placeholder image or default profile image as SVG or PNG. You can use the following parameters in the constructor:
class ProfileImage (name string
, options [optional] object
)
name string
- A string of characters. Any full name will be shortened to the first letter of the first two words.
options.textColor string
- Sets the text color. Any browser supported color value can be used.
default: "#ffffff"
options.backgroundColor string
- Aets the background color. Any browser supported color value can be used.
default: "#1f6feb"
options.fontFamily string
- Sets the font family.
default: "Arial"
options.fontSize number
- Sets the font size of the text.
default: 40
options.fontWeight string
- Sets the font weight of the text.
default: "normal"
Methods
svg()
- Returns a String containing a SVG with the set parameters.
png()
- Returns a String containing a base64 encoded PNG with the set parameters.
previewInConsole()
- Logs a color preview in the console.
listAttributesInConsole()
- List all object attributes as a table in the console.
- NodeJS: Version 10.0.0 or higher required.
Usage
Include the script in your HTML.
<script language="javascript" type="text/javascript" src="/path/to/file/src/profile-image.js"></script>
SVG
var image = new ProfileImage("Max Miller");
image.svg();
returns
<svg viewBox='0 0 100 100' width='100' height='100' style='background-color:#1f6feb'>
<rect width='100' height='100' x='0' y='0' fill='#1f6feb'></rect>
<text x='50%' y='50%' alignment-baseline='central' text-anchor='middle' font-weight='bold' font-family='Arial' font-size='40' fill='#ffffff' dominant-baseline='middle' text-anchor='middle'>MM</text>
</svg>
as a string.
PNG
var image = new ProfileImage("Max Miller", {backgroundColor: "#45c421", textColor: "black", fontWeight: "bold"});
image.png();
returns base64 encoded PNG as string.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAYAAABccqhmAAAVAklEQVR4Xu2deXiV1ZnAX4QQQoBsJECAhBAKWssOihuLlC52my5j+0wtVhix1uLSp9MZnc7q0trHjdqWutS6ddrnsWpbO1ZnRqVK6wIotKKlskiQpGxJWAOEkDkHpIRwt3Pu952ee88v//iH957l9775cc7Jd96vx5yldZ3CDwQgECSBHgggyLgzaQgcIYAASAQIBEwAAQQcfKYOAQRADkAgYAIIIODgM3UIIAByAAIBE0AAAQefqUMAAZADEAiYAAIIOPhMHQIIgByAQMAEEE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4SgAB+BoZxgUBBwQQgAPIdAEBXwkgAF8jw7gg4IAAAnAAmS4g4CsBBOBrZBgXBBwQQAAOINMFBHwlgAB8jQzjgoADAgjAAWS6gICvBBCAr5FhXBBwQAABOIBMFxDwlQAC8DUyjAsCDgggAAeQ6QICvhJAAL5GhnFBwAEBBOAAMl1AwFcCCMDXyDAuCDgggAAcQKYLCPhKAAH4GhnGBQEHBBCAA8h0AQFfCSAAXyPDuCDggAACcACZLiDgKwEE4GtkGBcEHBBAAA4g0wUEfCWAAHyNDOOCgAMCCMABZLqAgK8EEICvkWFcEHBAAAE4gEwXEPCVAALwNTKMCwIOCCAAB5DpAgK+Evh/3V3FtRh6CLAAAAAASUVORK5CYII=
Example
The result for
var image = new ProfileImage("Nick Keaton", {fontWeight: "bold"});
var src = image.png();
would look like this when used as “src”:
Alternatives
There is also a PHP and a Ruby class for SVGs as well as modules for NodeJS. The module “profile-image” is without the png method. “profile-image-png” contains the png method but requires node-canvas.
API
You can also use the API for Testing:
https://api.nilskoepke.com/profile-image?name=John+Doe&backgroundColor=rgb(33,194,87)
returns
Set your custom options as individual URL parameters. For spaces use “+” → “John+Doe”. For color parameters you can use color names like “green”, rgb and rgba like “rgb(33,194,87)”, hsl, hsla and hex values. Hex color values cannot start with “#”. Use “$” instead: “#ffffff” → “$ffffff”