@@ -4,6 +4,8 @@ import { Unsplash } from './image_providers/unsplash';
4
4
import { LoremPicsum } from './image_providers/lorempicsum' ;
5
5
6
6
/**
7
+ * Module to generate placeholder images.
8
+ *
7
9
* Default provider is unsplash image provider.
8
10
*/
9
11
export class Image {
@@ -26,12 +28,16 @@ export class Image {
26
28
}
27
29
28
30
/**
29
- * image
31
+ * Generates a random image url from one of the supported categories.
30
32
*
31
- * @method faker.image.image
32
- * @param width
33
- * @param height
34
- * @param randomize
33
+ * @param width The width of the image.
34
+ * @param height The height of the image.
35
+ * @param randomize Whether to randomize the image or not.
36
+ *
37
+ * @example
38
+ * faker.image.image() // 'http://placeimg.com/640/480/city'
39
+ * faker.image.image(1234, 2345) // 'http://placeimg.com/1234/2345/sports'
40
+ * faker.image.image(1234, 2345, true) // 'http://placeimg.com/1234/2345/nature?56789'
35
41
*/
36
42
image ( width ?: number , height ?: number , randomize ?: boolean ) : string {
37
43
const categories = [
@@ -57,22 +63,31 @@ export class Image {
57
63
}
58
64
59
65
/**
60
- * avatar
66
+ * Generates a random avatar image url.
61
67
*
62
- * @method faker.image.avatar
68
+ * @example
69
+ * faker.image.avatar()
70
+ * // 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/170.jpg'
63
71
*/
64
72
avatar ( ) : string {
65
73
return this . faker . internet . avatar ( ) ;
66
74
}
67
75
68
76
/**
69
- * imageUrl
77
+ * Generates a random image url.
78
+ *
79
+ * @param width The width of the image. Defaults to `640`.
80
+ * @param height The height of the image. Defaults to `480`.
81
+ * @param category The category of the image. By default, a random one will be selected.
82
+ * @param randomize Whether to randomize the image or not.
83
+ * @param https When true, return a `https` url. Otherwise, return a `http` url.
70
84
*
71
- * @method faker.image.imageUrl
72
- * @param width
73
- * @param height
74
- * @param category
75
- * @param randomize
85
+ * @example
86
+ * faker.image.imageUrl() // 'http://placeimg.com/640/480'
87
+ * faker.image.imageUrl(1234, 2345) // 'http://placeimg.com/1234/2345'
88
+ * faker.image.imageUrl(1234, 2345, 'cat') // 'http://placeimg.com/1234/2345/cat'
89
+ * faker.image.imageUrl(1234, 2345, 'cat', true) // 'http://placeimg.com/1234/2345/cat?6849'
90
+ * faker.image.imageUrl(1234, 2345, 'cat', true, true) // 'https://placeimg.com/1234/2345/cat?56789'
76
91
*/
77
92
imageUrl (
78
93
width ?: number ,
@@ -100,168 +115,222 @@ export class Image {
100
115
}
101
116
102
117
/**
103
- * abstract
118
+ * Generates a random abstract image url.
119
+ *
120
+ * @param width The width of the image.
121
+ * @param height The height of the image.
122
+ * @param randomize Whether to randomize the image or not.
104
123
*
105
- * @method faker.image.abstract
106
- * @param width
107
- * @param height
108
- * @param randomize
124
+ * @example
125
+ * faker.image.abstract() // 'http://placeimg.com/640/480/abstract'
126
+ * faker.image.abstract(1234, 2345) // 'http://placeimg.com/1234/2345/abstract'
127
+ * faker.image.abstract(1234, 2345, true) // 'http://placeimg.com/1234/2345/abstract?56789'
109
128
*/
110
129
abstract ( width ?: number , height ?: number , randomize ?: boolean ) : string {
111
130
return this . faker . image . imageUrl ( width , height , 'abstract' , randomize ) ;
112
131
}
113
132
114
133
/**
115
- * animals
134
+ * Generates a random animal image url.
116
135
*
117
- * @method faker.image.animals
118
- * @param width
119
- * @param height
120
- * @param randomize
136
+ * @param width The width of the image.
137
+ * @param height The height of the image.
138
+ * @param randomize Whether to randomize the image or not.
139
+ *
140
+ * @example
141
+ * faker.image.animals() // 'http://placeimg.com/640/480/animals'
142
+ * faker.image.animals(1234, 2345) // 'http://placeimg.com/1234/2345/animals'
143
+ * faker.image.animals(1234, 2345, true) // 'http://placeimg.com/1234/2345/animals?56789'
121
144
*/
122
145
animals ( width ?: number , height ?: number , randomize ?: boolean) : string {
123
146
return this . faker . image . imageUrl ( width , height , 'animals' , randomize ) ;
124
147
}
125
148
126
149
/**
127
- * business
150
+ * Generates a random business image url.
151
+ *
152
+ * @param width The width of the image.
153
+ * @param height The height of the image.
154
+ * @param randomize Whether to randomize the image or not.
128
155
*
129
- * @method faker.image.business
130
- * @param width
131
- * @param height
132
- * @param randomize
156
+ * @example
157
+ * faker.image.business() // 'http://placeimg.com/640/480/business'
158
+ * faker.image.business(1234, 2345) // 'http://placeimg.com/1234/2345/business'
159
+ * faker.image.business(1234, 2345, true) // 'http://placeimg.com/1234/2345/business?56789'
133
160
*/
134
161
business ( width ?: number , height ?: number , randomize ?: boolean) : string {
135
162
return this . faker . image . imageUrl ( width , height , 'business' , randomize ) ;
136
163
}
137
164
138
165
/**
139
- * cats
166
+ * Generates a random cat image url.
140
167
*
141
- * @method faker.image.cats
142
- * @param width
143
- * @param height
144
- * @param randomize
168
+ * @param width The width of the image.
169
+ * @param height The height of the image.
170
+ * @param randomize Whether to randomize the image or not.
171
+ *
172
+ * @example
173
+ * faker.image.cats() // 'http://placeimg.com/640/480/cats'
174
+ * faker.image.cats(1234, 2345) // 'http://placeimg.com/1234/2345/cats'
175
+ * faker.image.cats(1234, 2345, true) // 'http://placeimg.com/1234/2345/cats?56789'
145
176
*/
146
177
cats ( width ?: number , height ?: number , randomize ?: boolean) : string {
147
178
return this . faker . image . imageUrl ( width , height , 'cats' , randomize ) ;
148
179
}
149
180
150
181
/**
151
- * city
182
+ * Generates a random city image url.
183
+ *
184
+ * @param width The width of the image.
185
+ * @param height The height of the image.
186
+ * @param randomize Whether to randomize the image or not.
152
187
*
153
- * @method faker.image.city
154
- * @param width
155
- * @param height
156
- * @param randomize
188
+ * @example
189
+ * faker.image.city() // 'http://placeimg.com/640/480/city'
190
+ * faker.image.city(1234, 2345) // 'http://placeimg.com/1234/2345/city'
191
+ * faker.image.city(1234, 2345, true) // 'http://placeimg.com/1234/2345/city?56789'
157
192
*/
158
193
city ( width ?: number , height ?: number , randomize ?: boolean) : string {
159
194
return this . faker . image . imageUrl ( width , height , 'city' , randomize ) ;
160
195
}
161
196
162
197
/**
163
- * food
198
+ * Generates a random food image url.
164
199
*
165
- * @method faker.image.food
166
- * @param width
167
- * @param height
168
- * @param randomize
200
+ * @param width The width of the image.
201
+ * @param height The height of the image.
202
+ * @param randomize Whether to randomize the image or not.
203
+ *
204
+ * @example
205
+ * faker.image.food() // 'http://placeimg.com/640/480/food'
206
+ * faker.image.food(1234, 2345) // 'http://placeimg.com/1234/2345/food'
207
+ * faker.image.food(1234, 2345, true) // 'http://placeimg.com/1234/2345/food?56789'
169
208
*/
170
209
food ( width ?: number , height ?: number , randomize ?: boolean) : string {
171
210
return this . faker . image . imageUrl ( width , height , 'food' , randomize ) ;
172
211
}
173
212
174
213
/**
175
- * nightlife
214
+ * Generates a random nightlife image url.
215
+ *
216
+ * @param width The width of the image.
217
+ * @param height The height of the image.
218
+ * @param randomize Whether to randomize the image or not.
176
219
*
177
- * @method faker.image.nightlife
178
- * @param width
179
- * @param height
180
- * @param randomize
220
+ * @example
221
+ * faker.image.nightlife() // 'http://placeimg.com/640/480/nightlife'
222
+ * faker.image.nightlife(1234, 2345) // 'http://placeimg.com/1234/2345/nightlife'
223
+ * faker.image.nightlife(1234, 2345, true) // 'http://placeimg.com/1234/2345/nightlife?56789'
181
224
*/
182
225
nightlife ( width ?: number , height ?: number , randomize ?: boolean) : string {
183
226
return this . faker . image . imageUrl ( width , height , 'nightlife' , randomize ) ;
184
227
}
185
228
186
229
/**
187
- * fashion
230
+ * Generates a random fashion image url.
188
231
*
189
- * @method faker.image.fashion
190
- * @param width
191
- * @param height
192
- * @param randomize
232
+ * @param width The width of the image.
233
+ * @param height The height of the image.
234
+ * @param randomize Whether to randomize the image or not.
235
+ *
236
+ * @example
237
+ * faker.image.fashion() // 'http://placeimg.com/640/480/fashion'
238
+ * faker.image.fashion(1234, 2345) // 'http://placeimg.com/1234/2345/fashion'
239
+ * faker.image.fashion(1234, 2345, true) // 'http://placeimg.com/1234/2345/fashion?56789'
193
240
*/
194
241
fashion ( width ?: number , height ?: number , randomize ?: boolean) : string {
195
242
return this . faker . image . imageUrl ( width , height , 'fashion' , randomize ) ;
196
243
}
197
244
198
245
/**
199
- * people
246
+ * Generates a random people image url.
247
+ *
248
+ * @param width The width of the image.
249
+ * @param height The height of the image.
250
+ * @param randomize Whether to randomize the image or not.
200
251
*
201
- * @method faker.image.people
202
- * @param width
203
- * @param height
204
- * @param randomize
252
+ * @example
253
+ * faker.image.people() // 'http://placeimg.com/640/480/people'
254
+ * faker.image.people(1234, 2345) // 'http://placeimg.com/1234/2345/people'
255
+ * faker.image.people(1234, 2345, true) // 'http://placeimg.com/1234/2345/people?56789'
205
256
*/
206
257
people ( width ?: number , height ?: number , randomize ?: boolean) : string {
207
258
return this . faker . image . imageUrl ( width , height , 'people' , randomize ) ;
208
259
}
209
260
210
261
/**
211
- * nature
262
+ * Generates a random nature image url.
212
263
*
213
- * @method faker.image.nature
214
- * @param width
215
- * @param height
216
- * @param randomize
264
+ * @param width The width of the image.
265
+ * @param height The height of the image.
266
+ * @param randomize Whether to randomize the image or not.
267
+ *
268
+ * @example
269
+ * faker.image.nature() // 'http://placeimg.com/640/480/nature'
270
+ * faker.image.nature(1234, 2345) // 'http://placeimg.com/1234/2345/nature'
271
+ * faker.image.nature(1234, 2345, true) // 'http://placeimg.com/1234/2345/nature?56789'
217
272
*/
218
273
nature ( width ?: number , height ?: number , randomize ?: boolean) : string {
219
274
return this . faker . image . imageUrl ( width , height , 'nature' , randomize ) ;
220
275
}
221
276
222
277
/**
223
- * sports
278
+ * Generates a random sports image url.
279
+ *
280
+ * @param width The width of the image.
281
+ * @param height The height of the image.
282
+ * @param randomize Whether to randomize the image or not.
224
283
*
225
- * @method faker.image.sports
226
- * @param width
227
- * @param height
228
- * @param randomize
284
+ * @example
285
+ * faker.image.sports() // 'http://placeimg.com/640/480/sports'
286
+ * faker.image.sports(1234, 2345) // 'http://placeimg.com/1234/2345/sports'
287
+ * faker.image.sports(1234, 2345, true) // 'http://placeimg.com/1234/2345/sports?56789'
229
288
*/
230
289
sports ( width ?: number , height ?: number , randomize ?: boolean) : string {
231
290
return this . faker . image . imageUrl ( width , height , 'sports' , randomize ) ;
232
291
}
233
292
234
293
/**
235
- * technics
294
+ * Generates a random technics image url.
236
295
*
237
- * @method faker.image.technics
238
- * @param width
239
- * @param height
240
- * @param randomize
296
+ * @param width The width of the image.
297
+ * @param height The height of the image.
298
+ * @param randomize Whether to randomize the image or not.
299
+ *
300
+ * @example
301
+ * faker.image.technics() // 'http://placeimg.com/640/480/technics'
302
+ * faker.image.technics(1234, 2345) // 'http://placeimg.com/1234/2345/technics'
303
+ * faker.image.technics(1234, 2345, true) // 'http://placeimg.com/1234/2345/technics?56789'
241
304
*/
242
305
technics ( width ?: number , height ?: number , randomize ?: boolean) : string {
243
306
return this . faker . image . imageUrl ( width , height , 'technics' , randomize ) ;
244
307
}
245
308
246
309
/**
247
- * transport
310
+ * Generates a random transport image url.
311
+ *
312
+ * @param width The width of the image.
313
+ * @param height The height of the image.
314
+ * @param randomize Whether to randomize the image or not.
248
315
*
249
- * @method faker.image.transport
250
- * @param width
251
- * @param height
252
- * @param randomize
316
+ * @example
317
+ * faker.image.transport() // 'http://placeimg.com/640/480/transport'
318
+ * faker.image.transport(1234, 2345) // 'http://placeimg.com/1234/2345/transport'
319
+ * faker.image.transport(1234, 2345, true) // 'http://placeimg.com/1234/2345/transport?56789'
253
320
*/
254
321
transport ( width ?: number , height ?: number , randomize ?: boolean) : string {
255
322
return this . faker . image . imageUrl ( width , height , 'transport' , randomize ) ;
256
323
}
257
324
258
325
/**
259
- * dataUri
326
+ * Generates a random data uri containing an svg image.
260
327
*
261
- * @method faker.image.dataUri
262
- * @param width
263
- * @param height
264
- * @param color
328
+ * @param width The width of the image.
329
+ * @param height The height of the image.
330
+ * @param color The color to use. Defaults to `grey`.
331
+ *
332
+ * @example
333
+ * faker.image.dataUri() // 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http...'
265
334
*/
266
335
dataUri ( width ?: number , height ?: number , color : string = 'grey' ) : string {
267
336
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="${
@@ -272,7 +341,4 @@ export class Image {
272
341
const rawPrefix = 'data:image/svg+xml;charset=UTF-8,' ;
273
342
return rawPrefix + encodeURIComponent ( svgString ) ;
274
343
}
275
-
276
- // Object.assign(self, self.unsplash);
277
- // How to set default as unsplash? should be image.default?
278
344
}
0 commit comments