Image
Module to generate images.
Overview
For a random image, use url()
. This will not return the image directly but a URL pointing to an image from one of two demo image providers "Picsum" and "LoremFlickr". You can request an image specifically from one of two providers using urlLoremFlickr()
or urlPicsumPhotos()
.
For a random placeholder image containing only solid color and text, use urlPlaceholder()
(uses a third-party service) or dataUri()
(returns a SVG string).
For a random user avatar image, use avatar()
.
If you need more control over the content of the images, you can pass a category
parameter e.g. 'cat'
or 'nature'
to urlLoremFlickr()
or simply use faker.helpers.arrayElement()
with your own array of image URLs.
avatar
Generates a random avatar image url.
Available since v2.0.1
Returns: string
function avatar(): string;
Examples
faker.image.avatar()
// 'https://avatars.githubusercontent.com/u/97165289'
Source
avatarGitHub
Generates a random avatar from GitHub.
Available since v8.0.0
Returns: string
function avatarGitHub(): string;
Examples
faker.image.avatarGitHub()
// 'https://avatars.githubusercontent.com/u/97165289'
Source
avatarLegacy
Deprecated
This method is deprecated and will be removed in a future version.
The links are no longer working. Use avatar()
instead.
Generates a random avatar from https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar
.
Available since v8.0.0
Returns: string
function avatarLegacy(): string;
Examples
faker.image.avatarLegacy()
// 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/170.jpg'
Source
dataUri
Generates a random data uri containing an URL-encoded SVG image or a Base64-encoded SVG image.
Available since v4.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | Options for generating a data uri. |
options.color? | string | faker.color.rgb() | The color of the image. Must be a color supported by svg. |
options.height? | number | faker.number.int({ min: 1, max: 3999 }) | The height of the image. |
options.type? | 'svg-uri' | 'svg-base64' | faker.helpers.arrayElements(['svg-uri', 'svg-base64']) | The type of the image to return. Consisting of the file extension and the used encoding. |
options.width? | number | faker.number.int({ min: 1, max: 3999 }) | The width of the image. |
Returns: string
function dataUri(
options: {
width?: number;
height?: number;
color?: string;
type?: 'svg-uri' | 'svg-base64';
} = {}
): string;
Examples
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...'
Source
url
Generates a random image url.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | Options for generating a URL for an image. |
options.height? | number | faker.number.int({ min: 1, max: 3999 }) | The height of the image. |
options.width? | number | faker.number.int({ min: 1, max: 3999 }) | The width of the image. |
Returns: string
function url(
options: {
width?: number;
height?: number;
} = {}
): string;
Examples
faker.image.url() // 'https://loremflickr.com/640/480?lock=1234'
Source
urlLoremFlickr
Generates a random image url provided via https://loremflickr.com.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | Options for generating a URL for an image. |
options.category? | string | Category to use for the image. | |
options.height? | number | faker.number.int({ min: 1, max: 3999 }) | The height of the image. |
options.width? | number | faker.number.int({ min: 1, max: 3999 }) | The width of the image. |
Returns: string
function urlLoremFlickr(
options: {
width?: number;
height?: number;
category?: string;
} = {}
): string;
Examples
faker.image.urlLoremFlickr() // 'https://loremflickr.com/640/480?lock=1234'
faker.image.urlLoremFlickr({ width: 128 }) // 'https://loremflickr.com/128/480?lock=1234'
faker.image.urlLoremFlickr({ height: 128 }) // 'https://loremflickr.com/640/128?lock=1234'
faker.image.urlLoremFlickr({ category: 'nature' }) // 'https://loremflickr.com/640/480/nature?lock=1234'
Source
urlPicsumPhotos
Generates a random image url provided via https://picsum.photos.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | Options for generating a URL for an image. |
options.blur? | 0 | 10 | 2 | 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | faker.number.int({ max: 10 }) | Whether the image should be blurred. |
options.grayscale? | boolean | faker.datatype.boolean() | Whether the image should be grayscale. |
options.height? | number | faker.number.int({ min: 1, max: 3999 }) | The height of the image. |
options.width? | number | faker.number.int({ min: 1, max: 3999 }) | The width of the image. |
Returns: string
function urlPicsumPhotos(
options: {
width?: number;
height?: number;
grayscale?: boolean;
blur?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
} = {}
): string;
Examples
faker.image.urlPicsumPhotos() // 'https://picsum.photos/seed/NWbJM2B/640/480'
faker.image.urlPicsumPhotos({ width: 128 }) // 'https://picsum.photos/seed/NWbJM2B/128/480'
faker.image.urlPicsumPhotos({ height: 128 }) // 'https://picsum.photos/seed/NWbJM2B/640/128'
faker.image.urlPicsumPhotos({ grayscale: true }) // 'https://picsum.photos/seed/NWbJM2B/640/480?grayscale'
faker.image.urlPicsumPhotos({ blur: 4 }) // 'https://picsum.photos/seed/NWbJM2B/640/480?blur=4'
faker.image.urlPicsumPhotos({ blur: 4, grayscale: true }) // 'https://picsum.photos/seed/NWbJM2B/640/480?grayscale&blur=4'
Source
urlPlaceholder
Generates a random image url provided via https://via.placeholder.com/.
Available since v8.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options | { ... } | {} | Options for generating a URL for an image. |
options.backgroundColor? | string | faker.color.rgb({ format: 'hex', prefix: '' }) | The background color of the image. |
options.format? | 'gif' | 'jpeg' | 'jpg' | 'png' | 'webp' | faker.helpers.arrayElement(['gif', 'jpeg', 'jpg', 'png', 'webp']) | The format of the image. |
options.height? | number | faker.number.int({ min: 1, max: 3999 }) | The height of the image. |
options.text? | string | faker.lorem.words() | The text to display on the image. |
options.textColor? | string | faker.color.rgb({ format: 'hex', prefix: '' }) | The text color of the image. |
options.width? | number | faker.number.int({ min: 1, max: 3999 }) | The width of the image. |
Returns: string
function urlPlaceholder(
options: {
width?: number;
height?: number;
backgroundColor?: string;
textColor?: string;
format?: 'gif' | 'jpeg' | 'jpg' | 'png' | 'webp';
text?: string;
} = {}
): string;
Examples
faker.image.urlPlaceholder() // 'https://via.placeholder.com/150x180/FF0000/FFFFFF.webp?text=lorem'
faker.image.urlPlaceholder({ width: 128 }) // 'https://via.placeholder.com/128x180/FF0000/FFFFFF.webp?text=lorem'
faker.image.urlPlaceholder({ height: 128 }) // 'https://via.placeholder.com/150x128/FF0000/FFFFFF.webp?text=lorem'
faker.image.urlPlaceholder({ backgroundColor: '000000' }) // 'https://via.placeholder.com/150x180/000000/FFFFFF.webp?text=lorem'
faker.image.urlPlaceholder({ textColor: '000000' }) // 'https://via.placeholder.com/150x180/FF0000/000000.webp?text=lorem'
faker.image.urlPlaceholder({ format: 'png' }) // 'https://via.placeholder.com/150x180/FF0000/FFFFFF.png?text=lorem'
faker.image.urlPlaceholder({ text: 'lorem ipsum' }) // 'https://via.placeholder.com/150x180/FF0000/FFFFFF.webp?text=lorem+ipsum'
faker.image.urlPlaceholder({ width: 128, height: 128, backgroundColor: '000000', textColor: 'FF0000', format: 'png', text: 'lorem ipsum' }) // 'https://via.placeholder.com/128x128/000000/FF0000.png?text=lorem+ipsum'