Skip to content

Commit 3c3e567

Browse files
Shinigami92damienwebdev
authored andcommitted
feat: migrate image (#92)
1 parent 8fcfcc6 commit 3c3e567

File tree

2 files changed

+292
-1
lines changed

2 files changed

+292
-1
lines changed

src/image.ts

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
import type { Faker } from '.';
2+
3+
const Lorempixel = require('./image_providers/lorempixel');
4+
const Unsplash = require('./image_providers/unsplash');
5+
const LoremPicsum = require('./image_providers/lorempicsum');
6+
7+
/**
8+
* Default provider is unsplash image provider.
9+
*/
10+
export class Image {
11+
readonly lorempixel: typeof Lorempixel;
12+
readonly unsplash: typeof Unsplash;
13+
readonly lorempicsum: typeof LoremPicsum;
14+
15+
constructor(private readonly faker: Faker) {
16+
// Bind `this` so namespaced is working correctly
17+
for (const name of Object.getOwnPropertyNames(Image.prototype)) {
18+
if (name === 'constructor' || typeof this[name] !== 'function') {
19+
continue;
20+
}
21+
this[name] = this[name].bind(this);
22+
}
23+
24+
this.lorempixel = new Lorempixel(this.faker);
25+
this.unsplash = new Unsplash(this.faker);
26+
this.lorempicsum = new LoremPicsum(this.faker);
27+
}
28+
29+
/**
30+
* image
31+
*
32+
* @method faker.image.image
33+
* @param width
34+
* @param height
35+
* @param randomize
36+
*/
37+
image(width?: number, height?: number, randomize?: boolean): string {
38+
const categories = [
39+
'abstract',
40+
'animals',
41+
'business',
42+
'cats',
43+
'city',
44+
'food',
45+
'nightlife',
46+
'fashion',
47+
'people',
48+
'nature',
49+
'sports',
50+
'technics',
51+
'transport',
52+
];
53+
return this[this.faker.random.arrayElement(categories)](
54+
width,
55+
height,
56+
randomize
57+
);
58+
}
59+
60+
/**
61+
* avatar
62+
*
63+
* @method faker.image.avatar
64+
*/
65+
avatar(): string {
66+
return this.faker.internet.avatar();
67+
}
68+
69+
/**
70+
* imageUrl
71+
*
72+
* @method faker.image.imageUrl
73+
* @param width
74+
* @param height
75+
* @param category
76+
* @param randomize
77+
*/
78+
imageUrl(
79+
width?: number,
80+
height?: number,
81+
category?: string,
82+
randomize?: boolean,
83+
https?: boolean
84+
): string {
85+
width ||= 640;
86+
height ||= 480;
87+
let protocol = 'http://';
88+
if (typeof https !== 'undefined' && https === true) {
89+
protocol = 'https://';
90+
}
91+
let url = protocol + 'placeimg.com/' + width + '/' + height;
92+
if (typeof category !== 'undefined') {
93+
url += '/' + category;
94+
}
95+
96+
if (randomize) {
97+
url += '?' + this.faker.datatype.number();
98+
}
99+
100+
return url;
101+
}
102+
103+
/**
104+
* abstract
105+
*
106+
* @method faker.image.abstract
107+
* @param width
108+
* @param height
109+
* @param randomize
110+
*/
111+
abstract(width?: number, height?: number, randomize?: boolean): string {
112+
return this.faker.image.imageUrl(width, height, 'abstract', randomize);
113+
}
114+
115+
/**
116+
* animals
117+
*
118+
* @method faker.image.animals
119+
* @param width
120+
* @param height
121+
* @param randomize
122+
*/
123+
animals(width?: number, height?: number, randomize?: boolean): string {
124+
return this.faker.image.imageUrl(width, height, 'animals', randomize);
125+
}
126+
127+
/**
128+
* business
129+
*
130+
* @method faker.image.business
131+
* @param width
132+
* @param height
133+
* @param randomize
134+
*/
135+
business(width?: number, height?: number, randomize?: boolean): string {
136+
return this.faker.image.imageUrl(width, height, 'business', randomize);
137+
}
138+
139+
/**
140+
* cats
141+
*
142+
* @method faker.image.cats
143+
* @param width
144+
* @param height
145+
* @param randomize
146+
*/
147+
cats(width?: number, height?: number, randomize?: boolean): string {
148+
return this.faker.image.imageUrl(width, height, 'cats', randomize);
149+
}
150+
151+
/**
152+
* city
153+
*
154+
* @method faker.image.city
155+
* @param width
156+
* @param height
157+
* @param randomize
158+
*/
159+
city(width?: number, height?: number, randomize?: boolean): string {
160+
return this.faker.image.imageUrl(width, height, 'city', randomize);
161+
}
162+
163+
/**
164+
* food
165+
*
166+
* @method faker.image.food
167+
* @param width
168+
* @param height
169+
* @param randomize
170+
*/
171+
food(width?: number, height?: number, randomize?: boolean): string {
172+
return this.faker.image.imageUrl(width, height, 'food', randomize);
173+
}
174+
175+
/**
176+
* nightlife
177+
*
178+
* @method faker.image.nightlife
179+
* @param width
180+
* @param height
181+
* @param randomize
182+
*/
183+
nightlife(width?: number, height?: number, randomize?: boolean): string {
184+
return this.faker.image.imageUrl(width, height, 'nightlife', randomize);
185+
}
186+
187+
/**
188+
* fashion
189+
*
190+
* @method faker.image.fashion
191+
* @param width
192+
* @param height
193+
* @param randomize
194+
*/
195+
fashion(width?: number, height?: number, randomize?: boolean): string {
196+
return this.faker.image.imageUrl(width, height, 'fashion', randomize);
197+
}
198+
199+
/**
200+
* people
201+
*
202+
* @method faker.image.people
203+
* @param width
204+
* @param height
205+
* @param randomize
206+
*/
207+
people(width?: number, height?: number, randomize?: boolean): string {
208+
return this.faker.image.imageUrl(width, height, 'people', randomize);
209+
}
210+
211+
/**
212+
* nature
213+
*
214+
* @method faker.image.nature
215+
* @param width
216+
* @param height
217+
* @param randomize
218+
*/
219+
nature(width?: number, height?: number, randomize?: boolean): string {
220+
return this.faker.image.imageUrl(width, height, 'nature', randomize);
221+
}
222+
223+
/**
224+
* sports
225+
*
226+
* @method faker.image.sports
227+
* @param width
228+
* @param height
229+
* @param randomize
230+
*/
231+
sports(width?: number, height?: number, randomize?: boolean): string {
232+
return this.faker.image.imageUrl(width, height, 'sports', randomize);
233+
}
234+
235+
/**
236+
* technics
237+
*
238+
* @method faker.image.technics
239+
* @param width
240+
* @param height
241+
* @param randomize
242+
*/
243+
technics(width?: number, height?: number, randomize?: boolean): string {
244+
return this.faker.image.imageUrl(width, height, 'technics', randomize);
245+
}
246+
247+
/**
248+
* transport
249+
*
250+
* @method faker.image.transport
251+
* @param width
252+
* @param height
253+
* @param randomize
254+
*/
255+
transport(width?: number, height?: number, randomize?: boolean): string {
256+
return this.faker.image.imageUrl(width, height, 'transport', randomize);
257+
}
258+
259+
/**
260+
* dataUri
261+
*
262+
* @method faker.image.dataUri
263+
* @param width
264+
* @param height
265+
* @param color
266+
*/
267+
dataUri(width?: number, height?: number, color: string = 'grey'): string {
268+
const svgString =
269+
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" width="' +
270+
width +
271+
'" height="' +
272+
height +
273+
'"><rect width="100%" height="100%" fill="' +
274+
color +
275+
'"/><text x="' +
276+
width / 2 +
277+
'" y="' +
278+
height / 2 +
279+
'" font-size="20" alignment-baseline="middle" text-anchor="middle" fill="white">' +
280+
width +
281+
'x' +
282+
height +
283+
'</text></svg>';
284+
const rawPrefix = 'data:image/svg+xml;charset=UTF-8,';
285+
return rawPrefix + encodeURIComponent(svgString);
286+
}
287+
288+
// Object.assign(self, self.unsplash);
289+
// How to set default as unsplash? should be image.default?
290+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Fake } from './fake';
44
import { Git } from './git';
55
import { Hacker } from './hacker';
66
import { Helpers } from './helpers';
7+
import { Image } from './image';
78
import { Internet } from './internet';
89
import { Mersenne } from './mersenne';
910
import { Name } from './name';
@@ -180,7 +181,7 @@ export class Faker {
180181
readonly hacker: Hacker = new Hacker(this);
181182
// TODO @Shinigami92 2022-01-12: iban was not used
182183
// readonly iban = new (require('./iban'))(this);
183-
readonly image = new (require('./image'))(this);
184+
readonly image: Image = new Image(this);
184185
readonly internet: Internet = new Internet(this);
185186
readonly lorem = new (require('./lorem'))(this);
186187
readonly music = new (require('./music'))(this);

0 commit comments

Comments
 (0)