5.2 KiB
Note
本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
English · 原始项目 · 上游 README
原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
DiceBear 头像库
DiceBear 是一款开源头像库。它可将任意种子字符串(例如用户名或电子邮件地址)转换为 35 种以上、由不同艺术家设计的风格之一的 SVG 头像。相同种子始终生成相同头像,因此你可以存储字符串而非图片,也无需让用户上传个人资料照片。
可通过风格选项自定义头像:颜色、背景、旋转,以及发型、眼镜等个别特征。
Playground | Documentation | Editor
一个库,六种语言
DiceBear 10 以 JavaScript、PHP、Python、Rust、Go 和 Dart 的原生库形式发布。每个移植版本都通过共享测试套件,要求 SVG 输出与 JavaScript 参考实现字节级一致。在浏览器中生成头像,稍后在 Go 或 PHP 后端重新生成,你会得到相同的字节。
| 语言 | 包 | 安装 |
|---|---|---|
| JavaScript / TypeScript | @dicebear/core |
npm install @dicebear/core |
| PHP | dicebear/core |
composer require dicebear/core |
| Python | dicebear-core |
pip install dicebear-core |
| Rust | dicebear-core |
cargo add dicebear-core |
| Go | dicebear-go |
go get github.com/dicebear/dicebear-go/v10 |
| Dart | dicebear_core |
dart pub add dicebear_core |
在 JavaScript 中用法如下;documentation 中有各语言的等价写法:
import { Avatar } from '@dicebear/core';
import definition from '@dicebear/styles/lorelei.json' with { type: 'json' };
const avatar = new Avatar(definition, {
seed: 'John Doe',
size: 128,
});
avatar.toString(); // SVG string
avatar.toDataUri(); // data:image/svg+xml;charset=utf-8,...
35 种以上的头像风格是来自 dicebear/styles 仓库的普通 JSON 定义,每种语言都有对应的包。你也可以使用 Figma 或从零开始 create your own style,。
无需编写代码
- HTTP API 可通过普通 URL 返回头像,免费且无需账户:
https://api.dicebear.com/10.x/lorelei/svg?seed=Felix。如需完全控制和隐私保护,你可以用单个 Docker 容器 host it yourself。 - CLI 可批量生成头像文件:
npx dicebear lorelei --count 10。 - editor 可让你手动组装单个头像并导出。
本仓库
本 monorepo 包含六个核心库、CLI、SVG 转栅格转换器、文档站点(dicebear.com),)以及编辑器。相关项目位于各自的仓库中:
dicebear/styles: 官方头像风格定义dicebear/schema: 定义与选项背后的 JSON Schemadicebear/api: 可自托管的 HTTP APIdicebear/exporter-plugin-for-figma: 供风格作者使用的 Figma 插件
欢迎贡献;CONTRIBUTING.md 说明了环境配置以及各类变更应归属的位置。
许可证
代码采用 MIT licensed,包括商业用途。头像风格是各位艺术家的作品,并附带各自的许可证;license overview 列出了全部内容,许多仅要求署名。
赞助方
Advertisement: 非常感谢为我们提供免费或折扣产品的赞助方。