aboutsummaryrefslogtreecommitdiff
path: root/src/modules/image/providers/placeholder.ts
blob: 42108c562d052b46522c19e55746912a160f4842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* eslint-disable deprecation/deprecation */
import type { Faker } from '../../..';
import { deprecated } from '../../../internal/deprecated';

/**
 * Module to generate links to images on `https://via.placeholder.com/`.
 *
 * @deprecated Use `faker.image.urlPlaceholder` instead.
 */
export class Placeholder {
  constructor(private readonly faker: Faker) {
    // Bind `this` so namespaced is working correctly
    for (const name of Object.getOwnPropertyNames(
      Placeholder.prototype
    ) as Array<keyof Placeholder | 'constructor'>) {
      if (name === 'constructor' || typeof this[name] !== 'function') {
        continue;
      }

      this[name] = this[name].bind(this);
    }
  }

  /**
   * Generates a new placeholder image url.
   *
   * @param width The width of the image (in pixels). Defaults to `640`.
   * @param height The height of the image (in pixels). Defaults to `width`.
   * @param text The text of the image.
   * @param format The file format of the image. Supports `png`, `jpeg`, `png`, `gif`, `webp`.
   * @param backgroundColor The background color of the placeholder. Supports HEX CODE format.
   * @param textColor The text color of the placeholder. Requires `backgroundColor`. Supports HEX CODE format.
   *
   * @example
   * faker.image.placeholder.imageUrl() // https://via.placeholder.com/640x640
   * faker.image.placeholder.imageUrl(200) // https://via.placeholder.com/200x200
   * faker.image.placeholder.imageUrl(200, 100) // https://via.placeholder.com/200x100
   * faker.image.placeholder.imageUrl(200, 100, 'Fish') // https://via.placeholder.com/200x100?text=Fish
   * faker.image.placeholder.imageUrl(200, 100, 'Fish', 'webp') // https://via.placeholder.com/200x100.webp?text=Fish
   * faker.image.placeholder.imageUrl(200, 100, 'Fish', 'webp') // https://via.placeholder.com/200x100.webp?text=Fish
   * faker.image.placeholder.imageUrl(200, 100, 'Fish', 'webp', '000000', 'ffffff) // https://via.placeholder.com/200x100/000000/FFFFFF.webp?text=Fish
   *
   * @deprecated Use `faker.image.urlPlaceholder` instead.
   */
  imageUrl(
    width?: number,
    height?: number,
    text?: string,
    format?: 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp',
    backgroundColor?: string,
    textColor?: string
  ): string {
    deprecated({
      deprecated: 'faker.placeholder.imageUrl',
      proposed: 'faker.image.urlPlaceholder',
      since: '8.0',
      until: '9.0',
    });
    width = width || 640;
    height = height || width;

    let url = 'https://via.placeholder.com';
    url += `/${width}x${height}`;

    if (backgroundColor != null) {
      url += `/${backgroundColor.replace('#', '').toUpperCase()}`;

      if (textColor != null) {
        url += `/${textColor.replace('#', '').toUpperCase()}`;
      }
    }

    if (format != null) {
      url += `.${format}`;
    }

    if (text != null) {
      const urlParam = new URLSearchParams({ text });
      url += `?${urlParam.toString()}`;
    }

    return url;
  }

  /**
   * Generate a new placeholder image with random colors and text.
   *
   * @param width The width of the image (in pixels). Defaults to `640`.
   * @param height The height of the image (in pixels). Defaults to `width`.
   * @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp`.
   *
   * @example
   * faker.image.placeholder.randomUrl() // https://via.placeholder.com/640x640/000000/ffffff?text=lorum
   * faker.image.placeholder.randomUrl(150) // https://via.placeholder.com/150x150/000000/ffffff?text=lorum
   * faker.image.placeholder.randomUrl(150, 200) // https://via.placeholder.com/150x200/000000/ffffff?text=lorum
   * faker.image.placeholder.randomUrl(150, 200, 'png') // https://via.placeholder.com/150x200/000000/ffffff.png?text=lorum
   *
   * @deprecated Use `faker.image.urlPlaceholder` instead.
   */
  randomUrl(
    width?: number,
    height?: number,
    format?: 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp'
  ): string {
    deprecated({
      deprecated: 'faker.placeholder.randomUrl',
      proposed: 'faker.image.urlPlaceholder',
      since: '8.0',
      until: '9.0',
    });
    return this.imageUrl(
      width,
      height,
      this.faker.lorem.word(),
      format,
      this.faker.color.rgb({
        casing: 'upper',
        prefix: '',
      }),
      this.faker.color.rgb({ casing: 'upper', prefix: '' })
    );
  }
}