diff options
| author | Shinigami <[email protected]> | 2022-05-03 15:48:20 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-05-03 15:48:20 +0200 |
| commit | a2da7c496e9a3741d165ddfe6128b50837fec361 (patch) | |
| tree | 88d371bc19487bc8a34d9043035aed8e4fedd7d5 /src/modules/image | |
| parent | cc46a0c19af2752b6210c24b715fcce20197b6d9 (diff) | |
| download | faker-a2da7c496e9a3741d165ddfe6128b50837fec361.tar.xz faker-a2da7c496e9a3741d165ddfe6128b50837fec361.zip | |
refactor!: reorganize src folder (#909)
Diffstat (limited to 'src/modules/image')
| -rw-r--r-- | src/modules/image/index.ts | 345 | ||||
| -rw-r--r-- | src/modules/image/providers/lorempicsum.ts | 126 | ||||
| -rw-r--r-- | src/modules/image/providers/lorempixel.ts | 288 | ||||
| -rw-r--r-- | src/modules/image/providers/unsplash.ts | 157 |
4 files changed, 916 insertions, 0 deletions
diff --git a/src/modules/image/index.ts b/src/modules/image/index.ts new file mode 100644 index 00000000..9e8e1879 --- /dev/null +++ b/src/modules/image/index.ts @@ -0,0 +1,345 @@ +import type { Faker } from '../..'; +import type { MethodsOf } from '../../utils/types'; +import { LoremPicsum } from './providers/lorempicsum'; +import { Lorempixel } from './providers/lorempixel'; +import { Unsplash } from './providers/unsplash'; + +/** + * Module to generate placeholder images. + * + * Default provider is unsplash image provider. + */ +export class Image { + readonly lorempixel: Lorempixel; + readonly unsplash: Unsplash; + readonly lorempicsum: LoremPicsum; + + constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Image.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + + this.lorempixel = new Lorempixel(this.faker); + this.unsplash = new Unsplash(this.faker); + this.lorempicsum = new LoremPicsum(this.faker); + } + + /** + * Generates a random image url from one of the supported categories. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.image() // 'http://loremflickr.com/640/480/city' + * faker.image.image(1234, 2345) // 'http://loremflickr.com/1234/2345/sports' + * faker.image.image(1234, 2345, true) // 'http://loremflickr.com/1234/2345/nature?56789' + */ + image(width?: number, height?: number, randomize?: boolean): string { + const categories: MethodsOf<Image, Image['image']> = [ + 'abstract', + 'animals', + 'business', + 'cats', + 'city', + 'food', + 'nightlife', + 'fashion', + 'people', + 'nature', + 'sports', + 'technics', + 'transport', + ]; + return this[this.faker.helpers.arrayElement(categories)]( + width, + height, + randomize + ); + } + + /** + * Generates a random avatar image url. + * + * @example + * faker.image.avatar() + * // 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/170.jpg' + */ + avatar(): string { + return this.faker.internet.avatar(); + } + + /** + * Generates a random image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param category The category of the image. By default, a random one will be selected. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * @param https When true, return a `https` url. Otherwise, return a `http` url. + * + * @example + * faker.image.imageUrl() // 'http://loremflickr.com/640/480' + * faker.image.imageUrl(1234, 2345) // 'http://loremflickr.com/1234/2345' + * faker.image.imageUrl(1234, 2345, 'cat') // 'http://loremflickr.com/1234/2345/cat' + * faker.image.imageUrl(1234, 2345, 'cat', true) // 'http://loremflickr.com/1234/2345/cat?6849' + * faker.image.imageUrl(1234, 2345, 'cat', true, true) // 'https://loremflickr.com/1234/2345/cat?56789' + */ + imageUrl( + width?: number, + height?: number, + category?: string, + randomize?: boolean, + https?: boolean + ): string { + width = width || 640; + height = height || 480; + let protocol = 'http://'; + if (https === true) { + protocol = 'https://'; + } + let url = `${protocol}loremflickr.com/${width}/${height}`; + if (category != null) { + url += `/${category}`; + } + + if (randomize) { + url += `?${this.faker.datatype.number()}`; + } + + return url; + } + + /** + * Generates a random abstract image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.abstract() // 'http://loremflickr.com/640/480/abstract' + * faker.image.abstract(1234, 2345) // 'http://loremflickr.com/1234/2345/abstract' + * faker.image.abstract(1234, 2345, true) // 'http://loremflickr.com/1234/2345/abstract?56789' + */ + abstract(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'abstract', randomize); + } + + /** + * Generates a random animal image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.animals() // 'http://loremflickr.com/640/480/animals' + * faker.image.animals(1234, 2345) // 'http://loremflickr.com/1234/2345/animals' + * faker.image.animals(1234, 2345, true) // 'http://loremflickr.com/1234/2345/animals?56789' + */ + animals(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'animals', randomize); + } + + /** + * Generates a random business image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.business() // 'http://loremflickr.com/640/480/business' + * faker.image.business(1234, 2345) // 'http://loremflickr.com/1234/2345/business' + * faker.image.business(1234, 2345, true) // 'http://loremflickr.com/1234/2345/business?56789' + */ + business(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'business', randomize); + } + + /** + * Generates a random cat image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.cats() // 'http://loremflickr.com/640/480/cats' + * faker.image.cats(1234, 2345) // 'http://loremflickr.com/1234/2345/cats' + * faker.image.cats(1234, 2345, true) // 'http://loremflickr.com/1234/2345/cats?56789' + */ + cats(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'cats', randomize); + } + + /** + * Generates a random city image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.city() // 'http://loremflickr.com/640/480/city' + * faker.image.city(1234, 2345) // 'http://loremflickr.com/1234/2345/city' + * faker.image.city(1234, 2345, true) // 'http://loremflickr.com/1234/2345/city?56789' + */ + city(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'city', randomize); + } + + /** + * Generates a random food image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.food() // 'http://loremflickr.com/640/480/food' + * faker.image.food(1234, 2345) // 'http://loremflickr.com/1234/2345/food' + * faker.image.food(1234, 2345, true) // 'http://loremflickr.com/1234/2345/food?56789' + */ + food(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'food', randomize); + } + + /** + * Generates a random nightlife image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.nightlife() // 'http://loremflickr.com/640/480/nightlife' + * faker.image.nightlife(1234, 2345) // 'http://loremflickr.com/1234/2345/nightlife' + * faker.image.nightlife(1234, 2345, true) // 'http://loremflickr.com/1234/2345/nightlife?56789' + */ + nightlife(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'nightlife', randomize); + } + + /** + * Generates a random fashion image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.fashion() // 'http://loremflickr.com/640/480/fashion' + * faker.image.fashion(1234, 2345) // 'http://loremflickr.com/1234/2345/fashion' + * faker.image.fashion(1234, 2345, true) // 'http://loremflickr.com/1234/2345/fashion?56789' + */ + fashion(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'fashion', randomize); + } + + /** + * Generates a random people image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.people() // 'http://loremflickr.com/640/480/people' + * faker.image.people(1234, 2345) // 'http://loremflickr.com/1234/2345/people' + * faker.image.people(1234, 2345, true) // 'http://loremflickr.com/1234/2345/people?56789' + */ + people(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'people', randomize); + } + + /** + * Generates a random nature image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.nature() // 'http://loremflickr.com/640/480/nature' + * faker.image.nature(1234, 2345) // 'http://loremflickr.com/1234/2345/nature' + * faker.image.nature(1234, 2345, true) // 'http://loremflickr.com/1234/2345/nature?56789' + */ + nature(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'nature', randomize); + } + + /** + * Generates a random sports image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.sports() // 'http://loremflickr.com/640/480/sports' + * faker.image.sports(1234, 2345) // 'http://loremflickr.com/1234/2345/sports' + * faker.image.sports(1234, 2345, true) // 'http://loremflickr.com/1234/2345/sports?56789' + */ + sports(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'sports', randomize); + } + + /** + * Generates a random technics image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.technics() // 'http://loremflickr.com/640/480/technics' + * faker.image.technics(1234, 2345) // 'http://loremflickr.com/1234/2345/technics' + * faker.image.technics(1234, 2345, true) // 'http://loremflickr.com/1234/2345/technics?56789' + */ + technics(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'technics', randomize); + } + + /** + * Generates a random transport image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to randomize the image or not. Defaults to `false`. + * + * @example + * faker.image.transport() // 'http://loremflickr.com/640/480/transport' + * faker.image.transport(1234, 2345) // 'http://loremflickr.com/1234/2345/transport' + * faker.image.transport(1234, 2345, true) // 'http://loremflickr.com/1234/2345/transport?56789' + */ + transport(width?: number, height?: number, randomize?: boolean): string { + return this.imageUrl(width, height, 'transport', randomize); + } + + /** + * Generates a random data uri containing an svg image. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param color The color to use. Defaults to `grey`. + * + * @example + * faker.image.dataUri() // 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http...' + */ + dataUri(width?: number, height?: number, color: string = 'grey'): string { + 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 + }" y="${ + 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); + } +} diff --git a/src/modules/image/providers/lorempicsum.ts b/src/modules/image/providers/lorempicsum.ts new file mode 100644 index 00000000..25869205 --- /dev/null +++ b/src/modules/image/providers/lorempicsum.ts @@ -0,0 +1,126 @@ +import type { Faker } from '../../..'; + +/** + * Module to generate links to random images on `https://picsum.photos/`. + */ +// TODO ST-DDT 2022-03-11: Rename to picsum? +export class LoremPicsum { + constructor(private readonly faker: Faker) {} + + /** + * Generates a new picsum image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param grayscale Whether to return a grayscale image. Default to `false`. + * @param blur The optional level of blur to apply. Supports `1` - `10`. + */ + image( + width?: number, + height?: number, + grayscale?: boolean, + blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 + ): string { + return this.imageUrl(width, height, grayscale, blur); + } + + /** + * Generates a new picsum image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param grayscale Whether to return a grayscale image. Default to `false`. + */ + imageGrayscale(width?: number, height?: number, grayscale?: boolean): string { + return this.imageUrl(width, height, grayscale); + } + + /** + * Generates a new picsum image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param blur The optional level of blur to apply. Supports `1` - `10`. + */ + imageBlurred( + width?: number, + height?: number, + blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 + ): string { + return this.imageUrl(width, height, undefined, blur); + } + + /** + * Generates a new picsum image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param grayscale Whether to return a grayscale image. Default to `false`. + * @param blur The optional level of blur to apply. Supports `1` - `10`. + * @param seed The optional seed to use. + */ + imageRandomSeeded( + width?: number, + height?: number, + grayscale?: boolean, + blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10, + seed?: string + ): string { + // TODO ST-DDT 2022-03-11: This method does the same as image url, maybe generate a seed, if it is missig? + return this.imageUrl(width, height, grayscale, blur, seed); + } + + /** + * Returns a random avatar url. + * + * @example + * faker.internet.avatar() + * // 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/315.jpg' + */ + // TODO ST-DDT 2022-03-11: Deprecate this method as it is duplicate and has nothing to do with lorempicsum. + avatar(): string { + return this.faker.internet.avatar(); + } + + /** + * Generates a new picsum image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param grayscale Whether to return a grayscale image. Default to `false`. + * @param blur The optional level of blur to apply. Supports `1` - `10`. + * @param seed The optional seed to use. + */ + imageUrl( + width?: number, + height?: number, + grayscale?: boolean, + blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10, + seed?: string + ): string { + width = width || 640; + height = height || 480; + + let url = 'https://picsum.photos'; + + if (seed) { + url += `/seed/${seed}`; + } + + url += `/${width}/${height}`; + + if (grayscale && blur) { + return `${url}?grayscale&blur=${blur}`; + } + + if (grayscale) { + return `${url}?grayscale`; + } + + if (blur) { + return `${url}?blur=${blur}`; + } + + return url; + } +} diff --git a/src/modules/image/providers/lorempixel.ts b/src/modules/image/providers/lorempixel.ts new file mode 100644 index 00000000..f1ab384b --- /dev/null +++ b/src/modules/image/providers/lorempixel.ts @@ -0,0 +1,288 @@ +import type { Faker } from '../../..'; +import type { MethodsOf } from '../../../utils/types'; + +/** + * Module to generate links to random images on `https://lorempixel.com/`. + */ +export class Lorempixel { + constructor(private readonly faker: Faker) {} + + /** + * Generates a new lorempixel image url for a random supported category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + image(width?: number, height?: number, randomize?: boolean): string { + const categories: MethodsOf<Lorempixel, Lorempixel['image']> = [ + 'abstract', + 'animals', + 'business', + 'cats', + 'city', + 'food', + 'nightlife', + 'fashion', + 'people', + 'nature', + 'sports', + 'technics', + 'transport', + ]; + return this[this.faker.helpers.arrayElement(categories)]( + width, + height, + randomize + ); + } + + /** + * Returns a random avatar url. + * + * @example + * faker.internet.avatar() + * // 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/315.jpg' + */ + // TODO ST-DDT 2022-03-11: Deprecate this method as it is duplicate and has nothing to do with lorempixel. + avatar(): string { + return this.faker.internet.avatar(); + } + + /** + * Generates a new lorempixel image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param category The category of the image to generate. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + imageUrl( + width?: number, + height?: number, + category?: string, + randomize?: boolean + ): string { + width = width || 640; + height = height || 480; + + let url = `https://lorempixel.com/${width}/${height}`; + if (category != null) { + url += `/${category}`; + } + + if (randomize) { + url += `?${this.faker.datatype.number()}`; + } + + return url; + } + + /** + * Generates a new lorempixel image url using the "abstract" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + abstract(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'abstract', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "animals" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + animals(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'animals', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "business" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + business(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'business', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "cats" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + cats(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'cats', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "city" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + city(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'city', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "food" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + food(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'food', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "nightlife" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + nightlife(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'nightlife', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "fashion" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + fashion(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'fashion', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "people" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + people(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'people', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "nature" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + nature(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'nature', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "sports" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + sports(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'sports', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "technics" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + technics(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'technics', + randomize + ); + } + + /** + * Generates a new lorempixel image url using the "transport" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param randomize Whether to append a seed to the url. Defaults to `false`. + */ + transport(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'transport', + randomize + ); + } +} diff --git a/src/modules/image/providers/unsplash.ts b/src/modules/image/providers/unsplash.ts new file mode 100644 index 00000000..45bd815c --- /dev/null +++ b/src/modules/image/providers/unsplash.ts @@ -0,0 +1,157 @@ +import type { Faker } from '../../..'; + +/** + * Module to generate links to random images on `https://source.unsplash.com/`. + */ +export class Unsplash { + // TODO ST-DDT 2022-03-11: Remove unused(?) constant + categories = [ + 'food', + 'nature', + 'people', + 'technology', + 'objects', + 'buildings', + ]; + + constructor(private readonly faker: Faker) {} + + /** + * Generates a new unsplash image url for a random supported category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param keyword The image keywords to use. + */ + image(width?: number, height?: number, keyword?: string): string { + return this.imageUrl(width, height, undefined, keyword); + } + + /** + * Returns a random avatar url. + * + * @example + * faker.internet.avatar() + * // 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/315.jpg' + */ + // TODO ST-DDT 2022-03-11: Deprecate this method as it is duplicate and has nothing to do with unsplash. + avatar(): string { + return this.faker.internet.avatar(); + } + + /** + * Generates a new unsplash image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param category The category of the image to generate. + * @param keyword The image keywords to use. + */ + imageUrl( + width?: number, + height?: number, + category?: string, + keyword?: string + ): string { + width = width || 640; + height = height || 480; + + let url = 'https://source.unsplash.com'; + + if (category != null) { + url += `/category/${category}`; + } + + url += `/${width}x${height}`; + + if (keyword != null) { + const keywordFormat = /^([A-Za-z0-9].+,[A-Za-z0-9]+)$|^([A-Za-z0-9]+)$/; + if (keywordFormat.test(keyword)) { + url += `?${keyword}`; + } + } + + return url; + } + + /** + * Generates a new unsplash image url using the "food" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param keyword The image keywords to use. + */ + food(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl(width, height, 'food', keyword); + } + + /** + * Generates a new unsplash image url using the "people" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param keyword The image keywords to use. + */ + people(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl(width, height, 'people', keyword); + } + + /** + * Generates a new unsplash image url using the "nature" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param keyword The image keywords to use. + */ + nature(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl(width, height, 'nature', keyword); + } + + /** + * Generates a new unsplash image url using the "technology" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param keyword The image keywords to use. + */ + technology(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl( + width, + height, + 'technology', + keyword + ); + } + + /** + * Generates a new unsplash image url using the "objects" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param keyword The image keywords to use. + */ + objects(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl( + width, + height, + 'objects', + keyword + ); + } + + /** + * Generates a new unsplash image url using the "buildings" category. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param keyword The image keywords to use. + */ + buildings(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl( + width, + height, + 'buildings', + keyword + ); + } +} |
