# image2uri

> Convert image file to data URI.

Latest version **2.1.2** (published 2023-11-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install image2uri
pnpm add image2uri
yarn add image2uri
bun add image2uri
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.1.2 |
| Published | 2023-11-27 |
| First published | 2020-08-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 12 |
| Author | Kenny |
| Maintainers | wcjiang |
| Keywords | image, uri, fs, base64, image-to-uri, image2uri |

## Links

- npm: https://www.npmjs.com/package/image2uri
- Repository: https://github.com/jaywcjlove/image2uri
- Homepage: https://jaywcjlove.github.io/image2uri/
- Issues: https://github.com/jaywcjlove/image2uri/issues
- Funding: https://jaywcjlove.github.io/#/sponsor
- npm.io page: https://npm.io/package/image2uri

## Dependencies (1)

- [node-fetch](https://npm.io/package/node-fetch.md) ^3.3.1

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 2.1.2 (latest) — 2023-11-27
- 2.1.1 — 2023-09-28
- 2.1.0 — 2023-09-27
- 2.0.3 — 2023-09-27
- 2.0.2 — 2023-04-06
- 2.0.1 — 2022-04-14
- 2.0.0 — 2022-04-14
- 1.1.0 — 2022-01-26
- 1.0.5 — 2021-09-30
- 1.0.4 — 2020-08-29
- 1.0.3 — 2020-08-17
- 1.0.2 — 2020-08-15
- 1.0.1 — 2020-08-15
- 1.0.0 — 2020-08-15

## README

Image To URI
===

[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor)
[![NPM Downloads](https://img.shields.io/npm/dm/image2uri.svg?style=flat)](https://www.npmjs.com/package/image2uri)
[![Build & Test](https://github.com/jaywcjlove/image2uri/actions/workflows/ci.yml/badge.svg)](https://github.com/jaywcjlove/image2uri/actions/workflows/ci.yml)
[![npm version](https://img.shields.io/npm/v/image2uri.svg)](https://www.npmjs.com/package/image2uri)
[![Coverage Status](https://jaywcjlove.github.io/image2uri/badges.svg)](https://jaywcjlove.github.io/image2uri/lcov-report/)

Convert image file to data URI. Support `.png`,`.gif`,`.jpg`,`.jpeg`,`.bm`,`.bmp`,`.webp`,`.ico`,`.svg`.

### Install

This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): Node 12+ is needed to use it and it must be import instead of require.

```bash
npm i image2uri
```

```js
const recursiveReaddirFiles = await import('image2uri');

// Fix compiling in typescript.
// https://github.com/microsoft/TypeScript/issues/43329#issuecomment-922544562
const { getExt, recursiveReaddirFiles } = await (Function('return import("image2uri")')()) as Promise<typeof import("image2uri")>;
```

### Basic Usage

```js
import image2uri from "image2uri";

console.log(image2uri('./example.bmp'));
// data:image/bmp;base64,Qk0YCAAAAAAAADYAAAAoAAAAGAAAABwAAAABABgAAAAAAOIHAAA....
console.log(image2uri('./example.jpg'));
// data:image/jpeg;base64,Qk0YCAAAAAAAADYAAAAoAAAAGAAAABwAAAABABgAAAAAAOIHAAA....

const uri = await image2uri('https://avatars.githubusercontent.com/u/1680273?v=4', { ext: '.apng' });
// data:image/apng;base64,/9j/2wCEAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDc
const avatar = await image2uri('https://avatars.githubusercontent.com/u/1680273?v=4');
// /9j/2wCEAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDc
```

### API

```ts
export declare const validUrl: (url: string) => boolean;
export declare const extTypeMap: {
    '.png': string;
    '.apng': string;
    '.gif': string;
    '.jpg': string;
    '.jpeg': string;
    '.bm': string;
    '.bmp': string;
    '.webp': string;
    '.ico': string;
    '.svg': string;
};
export type ExtType = keyof typeof extTypeMap;
export default function image2uri(file: string, options?: {
    ext?: string;
}): string | Promise<string>;
```

### Development

```bash
npm run watch # Listen compile .ts files.
npm run build # compile .ts files.

npm run start
```

### Related

- [recursive-readdir-files](https://github.com/jaywcjlove/recursive-readdir-files) Node.js module to list all files in a directory or any subdirectories.

## Contributors

As always, thanks to our amazing contributors!

<a href="https://github.com/jaywcjlove/image2uri/graphs/contributors">
  <img src="https://jaywcjlove.github.io/image2uri/CONTRIBUTORS.svg" />
</a>

Made with [action-contributors](https://github.com/jaywcjlove/github-action-contributors).

### License

Licensed under the MIT License.

---
_Source: https://npm.io/package/image2uri · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
