Skip to content

Commit

Permalink
Add webp and avif support
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianKoerner committed Jul 22, 2024
1 parent 1194229 commit ea1c19a
Show file tree
Hide file tree
Showing 7 changed files with 886 additions and 823 deletions.
1,620 changes: 803 additions & 817 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
"@dicebear/api-7": "*",
"@dicebear/api-8": "*",
"@dicebear/api-9": "*",
"@dicebear/converter": "^9.0.0",
"@dicebear/converter": "^9.1.0",
"@fastify/cors": "^8.4.0",
"change-case": "^5.0.2",
"fastify": "^4.23.2",
"qs": "^6.11.2"
},
"devDependencies": {
"@dicebear/core": "^7.0.1",
"@fontsource/noto-sans": "^5.0.22",
"@fontsource/noto-sans-jp": "^5.0.19",
"@fontsource/noto-sans-kr": "^5.0.19",
Expand Down
18 changes: 18 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ export const config: Config = {
},
exif: Boolean(Number(process.env.JPEG_EXIF ?? 1)),
},
webp: {
enabled: Boolean(Number(process.env.WEBP ?? 1)),
size: {
min: Number(process.env.WEBP_SIZE_MIN ?? 1),
max: Number(process.env.WEBP_SIZE_MAX ?? 256),
default: Number(process.env.WEBP_SIZE_DEFAULT ?? 128),
},
exif: Boolean(Number(process.env.WEBP_EXIF ?? 1)),
},
avif: {
enabled: Boolean(Number(process.env.AVIF ?? 1)),
size: {
min: Number(process.env.AVIF_SIZE_MIN ?? 1),
max: Number(process.env.AVIF_SIZE_MAX ?? 256),
default: Number(process.env.AVIF_SIZE_DEFAULT ?? 128),
},
exif: Boolean(Number(process.env.AVIF_EXIF ?? 1)),
},
json: {
enabled: Boolean(Number(process.env.JSON ?? 1)),
},
Expand Down
44 changes: 42 additions & 2 deletions src/handler/avatar.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import type { Core } from '../types.js';
import { config } from '../config.js';
import { toJpeg, toPng } from '@dicebear/converter';
import { toJpeg, toPng, toWebp, toAvif } from '@dicebear/converter';
import { getRequiredFonts } from '../utils/fonts.js';

export type AvatarRequest = {
Params: {
format: 'svg' | 'png' | 'jpg' | 'jpeg' | 'json';
format: 'svg' | 'png' | 'jpg' | 'jpeg' | 'webp' | 'avif' | 'json';
options?: Record<string, any>;
};
Querystring: Record<string, any>;
Expand Down Expand Up @@ -39,6 +39,26 @@ export function avatarHandler(app: FastifyInstance, core: Core, style: any) {
: config.png.size.default;
}

// Validate Size for WebP Format
if (request.params.format === 'webp') {
options['size'] = options['size']
? Math.min(
Math.max(options['size'], config.webp.size.min),
config.jpeg.size.max
)
: config.png.size.default;
}

// Validate Size for Avif Format
if (request.params.format === 'avif') {
options['size'] = options['size']
? Math.min(
Math.max(options['size'], config.avif.size.min),
config.jpeg.size.max
)
: config.png.size.default;
}

// Define default seed
options['seed'] = options['seed'] ?? '';

Expand Down Expand Up @@ -81,6 +101,26 @@ export function avatarHandler(app: FastifyInstance, core: Core, style: any) {

return Buffer.from(jpeg);

case 'webp':
reply.header('Content-Type', 'image/webp');

const webp = await toWebp(avatar.toString(), {
includeExif: config.png.exif,
fonts: getRequiredFonts(avatar.toString(), app.fonts),
}).toArrayBuffer();

return Buffer.from(webp);

case 'avif':
reply.header('Content-Type', 'image/avif');

const avif = await toAvif(avatar.toString(), {
includeExif: config.png.exif,
fonts: getRequiredFonts(avatar.toString(), app.fonts),
}).toArrayBuffer();

return Buffer.from(avif);

case 'json':
reply.header('Content-Type', 'application/json');

Expand Down
2 changes: 2 additions & 0 deletions src/routes/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const paramsSchema: Record<string, JSONSchema7Definition> = {
'svg',
...(config.png.enabled ? ['png'] : []),
...(config.jpeg.enabled ? ['jpg', 'jpeg'] : []),
...(config.webp.enabled ? ['webp'] : []),
...(config.avif.enabled ? ['avif'] : []),
...(config.json.enabled ? ['json'] : []),
],
},
Expand Down
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ export type Config = {
};
exif: boolean;
};
webp: {
enabled: boolean;
size: {
max: number;
min: number;
default: number;
};
exif: boolean;
};
avif: {
enabled: boolean;
size: {
max: number;
min: number;
default: number;
};
exif: boolean;
};
json: {
enabled: boolean;
};
Expand Down
4 changes: 2 additions & 2 deletions versions/9.x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"types": "./index.d.ts"
},
"dependencies": {
"@dicebear/collection": "^9.0.0",
"@dicebear/core": "^9.0.0"
"@dicebear/collection": "^9.1.0",
"@dicebear/core": "^9.1.0"
}
}

0 comments on commit ea1c19a

Please sign in to comment.