@dicebear/converter 9.3.2 → 9.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/core.js CHANGED
@@ -1,33 +1,37 @@
1
1
  import { getMimeType } from './utils/mime-type.js';
2
2
  import { ensureSize } from './utils/svg.js';
3
- export const toPng = (avatar) => {
4
- return toFormat(avatar, 'png');
3
+ export const toPng = (avatar, options = {}) => {
4
+ return toFormat(avatar, 'png', options);
5
5
  };
6
- export const toJpeg = (avatar) => {
7
- return toFormat(avatar, 'jpeg');
6
+ export const toJpeg = (avatar, options = {}) => {
7
+ return toFormat(avatar, 'jpeg', options);
8
8
  };
9
- export const toWebp = (avatar) => {
10
- return toFormat(avatar, 'webp');
9
+ export const toWebp = (avatar, options = {}) => {
10
+ return toFormat(avatar, 'webp', options);
11
11
  };
12
- export const toAvif = (avatar) => {
13
- return toFormat(avatar, 'avif');
12
+ export const toAvif = (avatar, options = {}) => {
13
+ return toFormat(avatar, 'avif', options);
14
14
  };
15
- function toFormat(avatar, format) {
15
+ function toFormat(avatar, format, options = {}) {
16
+ if (options.includeExif) {
17
+ console.warn('The `exif` option is not supported in the browser version of `@dicebear/converter`. \n' +
18
+ 'Please use the node version of `@dicebear/converter` to generate images with exif data.');
19
+ }
16
20
  const svg = typeof avatar === 'string' ? avatar : avatar.toString();
17
21
  return {
18
- toDataUri: () => toDataUri(svg, format),
19
- toArrayBuffer: () => toArrayBuffer(svg, format),
22
+ toDataUri: () => toDataUri(svg, format, options),
23
+ toArrayBuffer: () => toArrayBuffer(svg, format, options),
20
24
  };
21
25
  }
22
- async function toDataUri(svg, format, exif) {
26
+ async function toDataUri(svg, format, options) {
23
27
  if ('svg' === format) {
24
28
  return `data:${getMimeType(format)};utf8,${encodeURIComponent(svg)}`;
25
29
  }
26
- const canvas = await toCanvas(svg, format, exif);
30
+ const canvas = await toCanvas(svg, format, options);
27
31
  return canvas.toDataURL(getMimeType(format));
28
32
  }
29
- async function toArrayBuffer(rawSvg, format, exif) {
30
- const canvas = await toCanvas(rawSvg, format, exif);
33
+ async function toArrayBuffer(rawSvg, format, options) {
34
+ const canvas = await toCanvas(rawSvg, format, options);
31
35
  return await new Promise((resolve, reject) => {
32
36
  canvas.toBlob((blob) => {
33
37
  blob
@@ -36,12 +40,8 @@ async function toArrayBuffer(rawSvg, format, exif) {
36
40
  }, getMimeType(format));
37
41
  });
38
42
  }
39
- async function toCanvas(rawSvg, format, exif) {
40
- if (exif) {
41
- console.warn('The `exif` option is not supported in the browser version of `@dicebear/converter`. \n' +
42
- 'Please use the node version of `@dicebear/converter` to generate images with exif data.');
43
- }
44
- const { svg, size } = ensureSize(rawSvg);
43
+ async function toCanvas(rawSvg, format, options) {
44
+ const { svg, size } = ensureSize(rawSvg, options.size);
45
45
  const canvas = document.createElement('canvas');
46
46
  canvas.width = size;
47
47
  canvas.height = size;
@@ -56,7 +56,7 @@ async function toCanvas(rawSvg, format, exif) {
56
56
  const img = document.createElement('img');
57
57
  img.width = size;
58
58
  img.height = size;
59
- img.setAttribute('src', await toDataUri(svg, 'svg'));
59
+ img.setAttribute('src', await toDataUri(svg, 'svg', options));
60
60
  return new Promise((resolve, reject) => {
61
61
  img.onload = () => {
62
62
  context.drawImage(img, 0, 0, size, size);
package/lib/node/core.js CHANGED
@@ -39,7 +39,7 @@ async function toArrayBuffer(rawSvg, format, exif, options) {
39
39
  }
40
40
  async function toBuffer(rawSvg, format, exif, options) {
41
41
  const hasFonts = Array.isArray(options.fonts);
42
- const { svg } = ensureSize(rawSvg);
42
+ const { svg } = ensureSize(rawSvg, options.size);
43
43
  let buffer = (await renderAsync(svg, {
44
44
  font: {
45
45
  loadSystemFonts: !hasFonts,
package/lib/types.d.ts CHANGED
@@ -13,6 +13,7 @@ export interface Exif {
13
13
  [key: string]: string;
14
14
  }
15
15
  export interface Options {
16
+ size?: number;
16
17
  fonts?: string[];
17
18
  includeExif?: boolean;
18
19
  }
@@ -1,5 +1,5 @@
1
1
  import { Metadata } from '../types';
2
- export declare function ensureSize(svg: string, defaultSize?: number): {
2
+ export declare function ensureSize(svg: string, size?: number): {
3
3
  svg: string;
4
4
  size: number;
5
5
  };
package/lib/utils/svg.js CHANGED
@@ -1,11 +1,15 @@
1
1
  import { XMLParser } from 'fast-xml-parser';
2
- export function ensureSize(svg, defaultSize = 512) {
3
- let size = defaultSize;
2
+ const MAX_SIZE = 2048;
3
+ const DEFAULT_SIZE = 512;
4
+ function sanitizeSize(size) {
5
+ if (!Number.isFinite(size) || size <= 0) {
6
+ return DEFAULT_SIZE;
7
+ }
8
+ return Math.floor(Math.min(size, MAX_SIZE));
9
+ }
10
+ export function ensureSize(svg, size = DEFAULT_SIZE) {
11
+ size = sanitizeSize(size);
4
12
  svg = svg.replace(/<svg([^>]*)/, (match, g1) => {
5
- const found = g1.match(/width="([^"]+)"/);
6
- if (found) {
7
- size = parseInt(found[1]);
8
- }
9
13
  if (g1.match(/width="([^"]+)"/)) {
10
14
  g1 = g1.replace(/width="([^"]+)"/, `width="${size}"`);
11
15
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dicebear/converter",
3
- "version": "9.3.2",
3
+ "version": "9.4.1",
4
4
  "description": "SVG Converter for DiceBear",
5
5
  "keywords": [
6
6
  "dicebear"