diff options
| author | inkedtree <[email protected]> | 2023-09-05 09:25:37 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-05 07:25:37 +0000 |
| commit | 869b9b49e77848f2ad9677e54b4ab6db6cf5c58b (patch) | |
| tree | 68f9f5a4ff914aefe858d51cafa319cd0f99b485 /src/modules | |
| parent | f71cc1cde9f016405830a6db1ffd78e4f1988a55 (diff) | |
| download | faker-869b9b49e77848f2ad9677e54b4ab6db6cf5c58b.tar.xz faker-869b9b49e77848f2ad9677e54b4ab6db6cf5c58b.zip | |
feat(image): add image dataUri with base64 (#2273)
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/image/index.ts | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/src/modules/image/index.ts b/src/modules/image/index.ts index cfefdb8a..bec9e98c 100644 --- a/src/modules/image/index.ts +++ b/src/modules/image/index.ts @@ -348,15 +348,17 @@ export class ImageModule { } /** - * Generates a random data uri containing an svg image. + * Generates a random data uri containing an URL-encoded SVG image or a Base64-encoded SVG image. * * @param options Options for generating a data uri. * @param options.width The width of the image. Defaults to `640`. * @param options.height The height of the image. Defaults to `480`. * @param options.color The color of the image. Defaults to `grey`. + * @param options.type The type of the image. Defaults to `'svg-uri'`. * * @example * faker.image.dataUri() // 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http...' + * faker.image.dataUri({ type: 'svg-base64' }) // 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3...' * * @since 4.0.0 */ @@ -380,9 +382,21 @@ export class ImageModule { * @default 'grey' */ color?: string; + /** + * The type of the image to return. Consisting of + * the file extension and the used encoding. + * + * @default 'svg-uri' + */ + type?: 'svg-uri' | 'svg-base64'; } = {} ): string { - const { width = 640, height = 480, color = 'grey' } = options; + const { + width = 640, + height = 480, + color = 'grey', + type = 'svg-uri', + } = options; const svgString = `<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" width="${width}" height="${height}"><rect width="100%" height="100%" fill="${color}"/><text x="${ width / 2 @@ -390,8 +404,11 @@ export class ImageModule { height / 2 }" font-size="20" alignment-baseline="middle" text-anchor="middle" fill="white">${width}x${height}</text></svg>`; - const rawPrefix = 'data:image/svg+xml;charset=UTF-8,'; - return rawPrefix + encodeURIComponent(svgString); + return type === 'svg-uri' + ? `data:image/svg+xml;charset=UTF-8,${encodeURIComponent(svgString)}` + : `data:image/svg+xml;base64,${Buffer.from(svgString).toString( + 'base64' + )}`; } /** |
