https://github.com/voicenterteam/avatars
A library for uploading and generating avatars.
https://github.com/voicenterteam/avatars
avatar avatar-image
Last synced: 4 months ago
JSON representation
A library for uploading and generating avatars.
- Host: GitHub
- URL: https://github.com/voicenterteam/avatars
- Owner: VoicenterTeam
- License: mit
- Created: 2021-05-17T08:01:37.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-06T02:32:54.000Z (over 2 years ago)
- Last Synced: 2024-12-31T02:27:46.111Z (6 months ago)
- Topics: avatar, avatar-image
- Language: TypeScript
- Homepage:
- Size: 896 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Avatar Library
A library for uploading and generating avatars.## How to install
```bash
npm install @voicenter/avatars;
```Importing using ES6 modules:
```js
import { Avatar } from "@voicenter/avatars";
```Importing using CommonJS:
```js
const { Avatar } = require("@voicenter/avatars");
```
## How to use
Create an instance of the `Avatar` class
```js
const avatar = new Avatar(config);
```
where`config` is a config object```js
{
avatarsPath: string; // path to folder with saved avatars
templatesPath: string; // path to folder with templates
sizes?: Array; // sizes of images to save [optional]
}
```Example:
```js
{
avatarsPath: "./src/media",
templatesPath: "./src/templates"
}
```
To generate images of custom sizes, add `sizes` to the config. Default sizes are `168, 32, 24`
```js
{
avatarsPath: "./src/media",
templatesPath: "./src/templates",
sizes: [300, 200, 100]
}
```## 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///.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: , height: `
* `File` [string] [required]: Base64 encoded image. For test you can convert image to Base64 here:
* **Example:**
```js
await avatar.upload({
AvatarAccountID: 21,
AvatarID: 3,
AvatarData: {
Coordinates: { width: 100, height: 100, left: 20, top: 20 },
File: "data:image/jpeg;base64,/9j/4AAAABBGQ...",
},
});```
### 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///.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:**
```js
await avatar.generateFromTemplate({
AvatarAccountID: 21,
AvatarID: 2,
AvatarData: { TemplateID: 1, Hex: "#640a82" },
});
```### 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///.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:**
```js
await avatar.generateFromContent({
AvatarAccountID: 21,
AvatarID: 4,
AvatarData: { Content: "SZ", Hex: "#640a82" },
});
```