项目作者: VoicenterTeam

项目描述 :
A library for uploading and generating avatars.
高级语言: TypeScript
项目地址: git://github.com/VoicenterTeam/avatars.git
创建时间: 2021-05-17T08:01:37Z
项目社区:https://github.com/VoicenterTeam/avatars

开源协议:MIT License

下载


Avatar Library

A library for uploading and generating avatars.

How to install

  1. npm install @voicenter/avatars;

Importing using ES6 modules:

  1. import { Avatar } from "@voicenter/avatars";

Importing using CommonJS:

  1. const { Avatar } = require("@voicenter/avatars");

How to use

Create an instance of the Avatar class

  1. const avatar = new Avatar(config);

whereconfig is a config object

  1. {
  2. avatarsPath: string; // path to folder with saved avatars
  3. templatesPath: string; // path to folder with templates
  4. sizes?: Array<number>; // sizes of images to save [optional]
  5. }

Example:

  1. {
  2. avatarsPath: "./src/media",
  3. templatesPath: "./src/templates"
  4. }

To generate images of custom sizes, add sizes to the config. Default sizes are 168, 32, 24

  1. {
  2. avatarsPath: "./src/media",
  3. templatesPath: "./src/templates",
  4. sizes: [300, 200, 100]
  5. }

Library methods

Avatar.upload


Uploads the avatar image in sizes provided in config or default sizes (168x168, 32x32 and 24x24).

The images are saved as src/media/<AvatarAccountID>/<AccountID>/<size>.png

  • Arguments
  • inputBody [object] [required]:
    • AvatarAccountID [integer] [required]: ID of the Account.
    • AvatarID [integer] [required]: ID of the Avatar.
    • AvatarData [object] [required]:
      • Coordinates : {top, left, width, height } [Object] [required]: Object of coordinates for crop. Check the image below to understand the meaning of the object properties. If you don’t want to crop the image, set top: 0, left: 0, width: <width of the image>, height: <height of the image>
      • File [string] [required]: Base64 encoded image. For test you can convert image to Base64 here: https://www.base64-image.de

alt text

  • Example:
  1. await avatar.upload({
  2. AvatarAccountID: 21,
  3. AvatarID: 3,
  4. AvatarData: {
  5. Coordinates: { width: 100, height: 100, left: 20, top: 20 },
  6. File: "data:image/jpeg;base64,/9j/4AAAABBGQ...",
  7. },
  8. });

Avatar.generateFromTemplate


Generates the avatar image from present template and saves it in sizes provided in config or default sizes (168x168, 32x32 and 24x24) using the template and background color given in the input object.

The images are saved as src/media/<AvatarAccountID>/<AccountID>/<size>.png

  • Arguments

  • inputBody [object] [required]:

    • AvatarAccountID [integer] [required]: ID of the Account.
    • AvatarID [integer] [required]: ID of the Avatar.
    • AvatarData [object] [required]:
      • Hex [string] [required]: hex color of the background
      • TemplateID [integer] [required]: ID of the template
  • Example:
  1. await avatar.generateFromTemplate({
  2. AvatarAccountID: 21,
  3. AvatarID: 2,
  4. AvatarData: { TemplateID: 1, Hex: "#640a82" },
  5. });

Avatar.generateFromContent


Generates the avatar image with given text and saves it in sizes provided in config or default sizes (168x168, 32x32 and 24x24) using the background color given in the input object.

The images are saved as src/media/<AvatarAccountID>/<AccountID>/<size>.png

  • Arguments

  • inputBody [object] [required]:

    • AvatarAccountID [integer] [required]: ID of the Account.
    • AvatarID [integer] [required]: ID of the Avatar.
    • AvatarData [object] [required]:
      • Hex [string] [required]: hex color of the background
      • Content [string] [required]: Text to be placed on the image
  • Example:
  1. await avatar.generateFromContent({
  2. AvatarAccountID: 21,
  3. AvatarID: 4,
  4. AvatarData: { Content: "SZ", Hex: "#640a82" },
  5. });