Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(image): add AI-generated avatars #3126

Merged
merged 30 commits into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9530d27
feat(image): Add AI-generated avatars
matthewmayer Sep 21, 2024
2bb43b9
fix tests
matthewmayer Sep 21, 2024
28e5cf7
Merge branch 'next' into avatar-ai
ST-DDT Oct 26, 2024
5312d51
Merge branch 'next' into avatar-ai
matthewmayer Feb 3, 2025
c359235
update to personPortrait and new URIs
matthewmayer Feb 3, 2025
d6be62b
Merge branch 'next' into avatar-ai
matthewmayer Feb 3, 2025
895d653
bump since value
matthewmayer Feb 4, 2025
acfd587
add this to the weekly url availability checks:
matthewmayer Feb 4, 2025
3438d59
new test
matthewmayer Feb 4, 2025
6b2b0b8
remove 16px size
matthewmayer Feb 4, 2025
6407fca
Merge branch 'next' into avatar-ai
matthewmayer Feb 4, 2025
b726bb8
update docs
matthewmayer Feb 4, 2025
e987dbc
Update index.ts
matthewmayer Feb 5, 2025
e963e43
Update index.ts
matthewmayer Feb 5, 2025
6638f8d
Update index.ts
matthewmayer Feb 5, 2025
93b964c
Update index.ts
matthewmayer Feb 5, 2025
1c32022
Update index.ts
matthewmayer Feb 5, 2025
f7882cb
Update index.ts
matthewmayer Feb 5, 2025
f8c699e
Update index.ts
matthewmayer Feb 5, 2025
7ab7522
Update index.ts
matthewmayer Feb 5, 2025
425c2cf
Update index.ts
matthewmayer Feb 5, 2025
5e8dabb
fix URL
matthewmayer Feb 5, 2025
949ccec
tweak docs
matthewmayer Feb 5, 2025
557b25e
Update index.ts
matthewmayer Feb 5, 2025
e23fd66
final urls
matthewmayer Feb 7, 2025
8ed7ef7
new tests
matthewmayer Feb 7, 2025
5c248a3
final urls
matthewmayer Feb 7, 2025
a35394b
chore: fix import scan
ST-DDT Feb 7, 2025
242609e
chore: format
ST-DDT Feb 7, 2025
41b893c
Merge branch 'next' into avatar-ai
ST-DDT Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions src/modules/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { toBase64 } from '../../internal/base64';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../../internal/module-base';
import type { SexType } from '../person';

/**
* Module to generate images.
Expand All @@ -11,7 +12,7 @@ import { ModuleBase } from '../../internal/module-base';
*
* For a random placeholder image containing only solid color and text, use [`urlPlaceholder()`](https://fakerjs.dev/api/image.html#urlplaceholder) (uses a third-party service) or [`dataUri()`](https://fakerjs.dev/api/image.html#datauri) (returns a SVG string).
*
* For a random user avatar image, use [`avatar()`](https://fakerjs.dev/api/image.html#avatar).
* For a random user avatar image, use [`avatar()`](https://fakerjs.dev/api/image.html#avatar), or [`personPortrait()`](https://fakerjs.dev/api/image.html#personportrait) which has more control over the size and sex of the person.
*
* If you need more control over the content of the images, you can pass a `category` parameter e.g. `'cat'` or `'nature'` to [`urlLoremFlickr()`](https://fakerjs.dev/api/image.html#urlloremflickr) or simply use [`faker.helpers.arrayElement()`](https://fakerjs.dev/api/helpers.html#arrayelement) with your own array of image URLs.
*/
Expand All @@ -27,7 +28,11 @@ export class ImageModule extends ModuleBase {
*/
avatar(): string {
// Add new avatar providers here, when adding a new one.
return this.avatarGitHub();
const avatarMethod = this.faker.helpers.arrayElement([
this.personPortrait,
this.avatarGitHub,
]);
return avatarMethod();
}

/**
Expand All @@ -45,6 +50,48 @@ export class ImageModule extends ModuleBase {
)}`;
}

/**
* Generates a random square portrait (avatar) of a person.
* These are static images of fictional people created by an AI, Stable Diffusion 3.
* The image URLs are served via the JSDelivr CDN and subject to their [terms of use](https://www.jsdelivr.com/terms).
*
* @param options Options for generating an AI avatar.
* @param options.sex The sex of the person for the avatar. Can be `'female'` or `'male'`. If not provided, defaults to a random selection.
* @param options.size The size of the image. Can be `512`, `256`, `128`, `64` or `32`. If not provided, defaults to `512`.
*
* @example
* faker.image.personPortrait()
* // 'https://cdn.jsdelivr.net/.../female/512/57.jpg'
* faker.image.personPortrait({ sex: 'male', size:'128' })
* // 'https://cdn.jsdelivr.net/.../male/128/27.jpg'
*
* @since 9.5.0
*/
personPortrait(
options: {
/**
* The sex of the person for the avatar.
* Can be `'female'` or `'male'`.
*
* @default faker.person.sexType()
*/
sex?: SexType;
/**
* The size of the image.
* Can be `512`, `256`, `128`, `64` or `32`.
*
* @default 512
*/
size?: 512 | 256 | 128 | 64 | 32;
} = {}
): string {
const { sex = this.faker.person.sexType(), size = 512 } = options;
// FIXME: This should be replaced by the final CDN url, presumably something like: 'https://cdn.jsdelivr.net/gh/faker-js/assets-person-portrait'
const baseURL =
'https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images';
return `${baseURL}/${sex}/${size}/${this.faker.number.int({ min: 0, max: 99 })}.jpg`;
}

/**
* Generates a random avatar from `https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar`.
*
Expand All @@ -59,7 +106,7 @@ export class ImageModule extends ModuleBase {
avatarLegacy(): string {
deprecated({
deprecated: 'faker.image.avatarLegacy()',
proposed: 'faker.image.avatar()',
proposed: 'faker.image.avatar() or faker.image.personPortrait()',
since: '9.0.2',
until: '10.0.0',
});
Expand Down
7 changes: 7 additions & 0 deletions test/integration/modules/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ describe('image', () => {
});
});

describe('personPortrait', () => {
it('should return a random asset url', async () => {
const actual = faker.image.personPortrait();
await assertWorkingUrl(actual);
});
});

describe('url', () => {
it('should return a random image url', async () => {
const actual = faker.image.url();
Expand Down
30 changes: 27 additions & 3 deletions test/modules/__snapshots__/image.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`image > 42 > avatar 1`] = `"https://avatars.githubusercontent.com/u/37454012"`;
exports[`image > 42 > avatar 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/male/512/73.jpg"`;

exports[`image > 42 > avatarGitHub 1`] = `"https://avatars.githubusercontent.com/u/37454012"`;

Expand All @@ -22,6 +22,14 @@ exports[`image > 42 > dataUri > with width 1`] = `"data:image/svg+xml;base64,PHN

exports[`image > 42 > dataUri > with width and height 1`] = `"data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22128%22%20height%3D%22128%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22%238ead33%22%2F%3E%3Ctext%20x%3D%2264%22%20y%3D%2264%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E128x128%3C%2Ftext%3E%3C%2Fsvg%3E"`;

exports[`image > 42 > personPortrait > noArgs 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/female/512/95.jpg"`;

exports[`image > 42 > personPortrait > with sex 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/female/512/37.jpg"`;

exports[`image > 42 > personPortrait > with sex and size 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/male/256/37.jpg"`;

exports[`image > 42 > personPortrait > with size 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/female/128/95.jpg"`;

exports[`image > 42 > url > noArgs 1`] = `"https://picsum.photos/seed/993RBH1Y/1498/3802"`;

exports[`image > 42 > url > with height 1`] = `"https://picsum.photos/seed/B993RBH1Y/1498/128"`;
Expand Down Expand Up @@ -76,7 +84,7 @@ exports[`image > 42 > urlPlaceholder > with width 1`] = `"https://via.placeholde

exports[`image > 42 > urlPlaceholder > with width and height 1`] = `"https://via.placeholder.com/128x128/8ead33/1ddf0f.webp?text=benevolentia%20attonbitus%20auctus"`;

exports[`image > 1211 > avatar 1`] = `"https://avatars.githubusercontent.com/u/92852016"`;
exports[`image > 1211 > avatar 1`] = `"https://avatars.githubusercontent.com/u/89347165"`;

exports[`image > 1211 > avatarGitHub 1`] = `"https://avatars.githubusercontent.com/u/92852016"`;

Expand All @@ -98,6 +106,14 @@ exports[`image > 1211 > dataUri > with width 1`] = `"data:image/svg+xml;charset=

exports[`image > 1211 > dataUri > with width and height 1`] = `"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgYmFzZVByb2ZpbGU9ImZ1bGwiIHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4Ij48cmVjdCB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiBmaWxsPSIjZWQ0ZmVmIi8+PHRleHQgeD0iNjQiIHk9IjY0IiBmb250LXNpemU9IjIwIiBhbGlnbm1lbnQtYmFzZWxpbmU9Im1pZGRsZSIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZmlsbD0id2hpdGUiPjEyOHgxMjg8L3RleHQ+PC9zdmc+"`;

exports[`image > 1211 > personPortrait > noArgs 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/male/512/89.jpg"`;

exports[`image > 1211 > personPortrait > with sex 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/female/512/92.jpg"`;

exports[`image > 1211 > personPortrait > with sex and size 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/male/256/92.jpg"`;

exports[`image > 1211 > personPortrait > with size 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/male/128/89.jpg"`;

exports[`image > 1211 > url > noArgs 1`] = `"https://loremflickr.com/3714/3573?lock=8982492793493979"`;

exports[`image > 1211 > url > with height 1`] = `"https://picsum.photos/seed/ZFGLlH/3714/128"`;
Expand Down Expand Up @@ -152,7 +168,7 @@ exports[`image > 1211 > urlPlaceholder > with width 1`] = `"https://via.placehol

exports[`image > 1211 > urlPlaceholder > with width and height 1`] = `"https://via.placeholder.com/128x128/ed4fef/a7fbae.webp?text=dapifer%20usque%20unde"`;

exports[`image > 1337 > avatar 1`] = `"https://avatars.githubusercontent.com/u/26202467"`;
exports[`image > 1337 > avatar 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/female/512/27.jpg"`;

exports[`image > 1337 > avatarGitHub 1`] = `"https://avatars.githubusercontent.com/u/26202467"`;

Expand All @@ -174,6 +190,14 @@ exports[`image > 1337 > dataUri > with width 1`] = `"data:image/svg+xml;base64,P

exports[`image > 1337 > dataUri > with width and height 1`] = `"data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20baseProfile%3D%22full%22%20width%3D%22128%22%20height%3D%22128%22%3E%3Crect%20width%3D%22100%25%22%20height%3D%22100%25%22%20fill%3D%22%23536a7b%22%2F%3E%3Ctext%20x%3D%2264%22%20y%3D%2264%22%20font-size%3D%2220%22%20alignment-baseline%3D%22middle%22%20text-anchor%3D%22middle%22%20fill%3D%22white%22%3E128x128%3C%2Ftext%3E%3C%2Fsvg%3E"`;

exports[`image > 1337 > personPortrait > noArgs 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/female/512/15.jpg"`;

exports[`image > 1337 > personPortrait > with sex 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/female/512/26.jpg"`;

exports[`image > 1337 > personPortrait > with sex and size 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/male/256/26.jpg"`;

exports[`image > 1337 > personPortrait > with size 1`] = `"https://cdn.jsdelivr.net/gh/matthewmayer/assets-person-portrait@new-images/female/128/15.jpg"`;

exports[`image > 1337 > url > noArgs 1`] = `"https://loremflickr.com/1048/635?lock=4137158724208997"`;

exports[`image > 1337 > url > with height 1`] = `"https://loremflickr.com/1048/128?lock=2505140979113303"`;
Expand Down
17 changes: 17 additions & 0 deletions test/modules/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ describe('image', () => {
type: 'svg-uri',
});
});

t.describe('personPortrait', (t) => {
t.it('noArgs')
.it('with sex', { sex: 'female' })
.it('with size', { size: 128 })
.it('with sex and size', { sex: 'male', size: 256 });
});
});

describe('avatar', () => {
Expand Down Expand Up @@ -144,6 +151,16 @@ describe('image', () => {
});
});

describe('personPortrait', () => {
it('should return a random avatar url from AI', () => {
const imageUrl = faker.image.personPortrait();

expect(imageUrl).toBeTypeOf('string');
expect(imageUrl).toMatch(/^https:\/\/cdn\.jsdelivr\.net\/.*\.jpg$/);
expect(() => new URL(imageUrl)).not.toThrow();
});
});

describe('url', () => {
it('should return a random image url', () => {
const actual = faker.image.url();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ exports[`check docs completeness > all modules and methods are present 1`] = `
"avatarGitHub",
"avatarLegacy",
"dataUri",
"personPortrait",
"url",
"urlLoremFlickr",
"urlPicsumPhotos",
Expand Down