MCPcopy
hub / github.com/faker-js/faker / dataUri

Method dataUri

src/modules/image/index.ts:303–348  ·  view source on GitHub ↗

* Generates a random data uri containing an URL-encoded SVG image or a Base64-encoded SVG image. * * @param options Options for generating a data uri. * @param options.width The width of the image. Defaults to a random integer between `1` and `3999`. * @param options.height The height of

(
    options: {
      /**
       * The width of the image.
       *
       * @default faker.number.int({ min: 1, max: 3999 })
       */
      width?: number;
      /**
       * The height of the image.
       *
       * @default faker.number.int({ min: 1, max: 3999 })
       */
      height?: number;
      /**
       * The color of the image. Must be a color supported by svg.
       *
       * @default faker.color.rgb()
       */
      color?: string;
      /**
       * The type of the image to return. Consisting of
       * the file extension and the used encoding.
       *
       * @default faker.helpers.arrayElement(['svg-uri', 'svg-base64'])
       */
      type?: 'svg-uri' | 'svg-base64';
    } = {}
  )

Source from the content-addressed store, hash-verified

301 * @since 4.0.0
302 */
303 dataUri(
304 options: {
305 /**
306 * The width of the image.
307 *
308 * @default faker.number.int({ min: 1, max: 3999 })
309 */
310 width?: number;
311 /**
312 * The height of the image.
313 *
314 * @default faker.number.int({ min: 1, max: 3999 })
315 */
316 height?: number;
317 /**
318 * The color of the image. Must be a color supported by svg.
319 *
320 * @default faker.color.rgb()
321 */
322 color?: string;
323 /**
324 * The type of the image to return. Consisting of
325 * the file extension and the used encoding.
326 *
327 * @default faker.helpers.arrayElement(['svg-uri', 'svg-base64'])
328 */
329 type?: 'svg-uri' | 'svg-base64';
330 } = {}
331 ): string {
332 const {
333 width = this.faker.number.int({ min: 1, max: 3999 }),
334 height = this.faker.number.int({ min: 1, max: 3999 }),
335 color = this.faker.color.rgb(),
336 type = this.faker.helpers.arrayElement(['svg-uri', 'svg-base64']),
337 } = options;
338
339 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="${
340 width / 2
341 }" y="${
342 height / 2
343 }" font-size="20" alignment-baseline="middle" text-anchor="middle" fill="white">${width}x${height}</text></svg>`;
344
345 return type === 'svg-uri'
346 ? `data:image/svg+xml;charset=UTF-8,${encodeURIComponent(svgString)}`
347 : `data:image/svg+xml;base64,${toBase64(svgString)}`;
348 }
349}

Callers 1

image.spec.tsFile · 0.80

Calls 3

intMethod · 0.80
rgbMethod · 0.80
arrayElementMethod · 0.80

Tested by

no test coverage detected