UNPKG

6.34 kBJavaScriptView Raw
1/**
2 *
3 * @namespace faker.image
4 * @property {object} lorempixel - faker.image.lorempixel
5 * @property {object} unsplash - faker.image.unsplash
6 * @property {object} unsplash - faker.image.lorempicsum
7 * @default Default provider is unsplash image provider
8 */
9var Image = function (faker) {
10
11 var self = this;
12 var Lorempixel = require('./image_providers/lorempixel');
13 var Unsplash = require('./image_providers/unsplash');
14 var LoremPicsum = require('./image_providers/lorempicsum');
15
16 /**
17 * image
18 *
19 * @param {number} width
20 * @param {number} height
21 * @param {boolean} randomize
22 * @method faker.image.image
23 */
24 self.image = function (width, height, randomize) {
25 var categories = ["abstract", "animals", "business", "cats", "city", "food", "nightlife", "fashion", "people", "nature", "sports", "technics", "transport"];
26 return self[faker.random.arrayElement(categories)](width, height, randomize);
27 };
28 /**
29 * avatar
30 *
31 * @method faker.image.avatar
32 */
33 self.avatar = function () {
34 return faker.internet.avatar();
35 };
36 /**
37 * imageUrl
38 *
39 * @param {number} width
40 * @param {number} height
41 * @param {string} category
42 * @param {boolean} randomize
43 * @method faker.image.imageUrl
44 */
45 self.imageUrl = function (width, height, category, randomize, https) {
46 var width = width || 640;
47 var height = height || 480;
48 var protocol = 'http://';
49 if (typeof https !== 'undefined' && https === true) {
50 protocol = 'https://';
51 }
52 var url = protocol + 'placeimg.com/' + width + '/' + height;
53 if (typeof category !== 'undefined') {
54 url += '/' + category;
55 }
56
57 if (randomize) {
58 url += '?' + faker.datatype.number()
59 }
60
61 return url;
62 };
63 /**
64 * abstract
65 *
66 * @param {number} width
67 * @param {number} height
68 * @param {boolean} randomize
69 * @method faker.image.abstract
70 */
71 self.abstract = function (width, height, randomize) {
72 return faker.image.imageUrl(width, height, 'abstract', randomize);
73 };
74 /**
75 * animals
76 *
77 * @param {number} width
78 * @param {number} height
79 * @param {boolean} randomize
80 * @method faker.image.animals
81 */
82 self.animals = function (width, height, randomize) {
83 return faker.image.imageUrl(width, height, 'animals', randomize);
84 };
85 /**
86 * business
87 *
88 * @param {number} width
89 * @param {number} height
90 * @param {boolean} randomize
91 * @method faker.image.business
92 */
93 self.business = function (width, height, randomize) {
94 return faker.image.imageUrl(width, height, 'business', randomize);
95 };
96 /**
97 * cats
98 *
99 * @param {number} width
100 * @param {number} height
101 * @param {boolean} randomize
102 * @method faker.image.cats
103 */
104 self.cats = function (width, height, randomize) {
105 return faker.image.imageUrl(width, height, 'cats', randomize);
106 };
107 /**
108 * city
109 *
110 * @param {number} width
111 * @param {number} height
112 * @param {boolean} randomize
113 * @method faker.image.city
114 */
115 self.city = function (width, height, randomize) {
116 return faker.image.imageUrl(width, height, 'city', randomize);
117 };
118 /**
119 * food
120 *
121 * @param {number} width
122 * @param {number} height
123 * @param {boolean} randomize
124 * @method faker.image.food
125 */
126 self.food = function (width, height, randomize) {
127 return faker.image.imageUrl(width, height, 'food', randomize);
128 };
129 /**
130 * nightlife
131 *
132 * @param {number} width
133 * @param {number} height
134 * @param {boolean} randomize
135 * @method faker.image.nightlife
136 */
137 self.nightlife = function (width, height, randomize) {
138 return faker.image.imageUrl(width, height, 'nightlife', randomize);
139 };
140 /**
141 * fashion
142 *
143 * @param {number} width
144 * @param {number} height
145 * @param {boolean} randomize
146 * @method faker.image.fashion
147 */
148 self.fashion = function (width, height, randomize) {
149 return faker.image.imageUrl(width, height, 'fashion', randomize);
150 };
151 /**
152 * people
153 *
154 * @param {number} width
155 * @param {number} height
156 * @param {boolean} randomize
157 * @method faker.image.people
158 */
159 self.people = function (width, height, randomize) {
160 return faker.image.imageUrl(width, height, 'people', randomize);
161 };
162 /**
163 * nature
164 *
165 * @param {number} width
166 * @param {number} height
167 * @param {boolean} randomize
168 * @method faker.image.nature
169 */
170 self.nature = function (width, height, randomize) {
171 return faker.image.imageUrl(width, height, 'nature', randomize);
172 };
173 /**
174 * sports
175 *
176 * @param {number} width
177 * @param {number} height
178 * @param {boolean} randomize
179 * @method faker.image.sports
180 */
181 self.sports = function (width, height, randomize) {
182 return faker.image.imageUrl(width, height, 'sports', randomize);
183 };
184 /**
185 * technics
186 *
187 * @param {number} width
188 * @param {number} height
189 * @param {boolean} randomize
190 * @method faker.image.technics
191 */
192 self.technics = function (width, height, randomize) {
193 return faker.image.imageUrl(width, height, 'technics', randomize);
194 };
195 /**
196 * transport
197 *
198 * @param {number} width
199 * @param {number} height
200 * @param {boolean} randomize
201 * @method faker.image.transport
202 */
203 self.transport = function (width, height, randomize) {
204 return faker.image.imageUrl(width, height, 'transport', randomize);
205 };
206 /**
207 * dataUri
208 *
209 * @param {number} width
210 * @param {number} height
211 * @param {string} color
212 * @method faker.image.dataUri
213 */
214 self.dataUri = function (width, height, color) {
215 color = color || 'grey';
216 var 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>';
217 var rawPrefix = 'data:image/svg+xml;charset=UTF-8,';
218 return rawPrefix + encodeURIComponent(svgString);
219 };
220
221 self.lorempixel = new Lorempixel(faker);
222 self.unsplash = new Unsplash(faker);
223 self.lorempicsum = new LoremPicsum(faker);
224
225 // Object.assign(self, self.unsplash);
226 // How to set default as unsplash? should be image.default?
227}
228
229
230module["exports"] = Image;