# optimal-img-format

> Get optimal image format on the browser or server.

Latest version **1.5.0** (published 2024-09-03) · UNLICENSE (HTTP://UNLICENSE.ORG/) license · 0 weekly downloads

## Install

```sh
npm install optimal-img-format
pnpm add optimal-img-format
yarn add optimal-img-format
bun add optimal-img-format
```

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.0 |
| Published | 2024-09-03 |
| First published | 2023-12-04 |
| Weekly downloads | 0 |
| License | UNLICENSE (HTTP://UNLICENSE.ORG/) |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | lbb00 |
| Keywords | detect image format, is support, webp, avif, jpeg |

## Links

- npm: https://www.npmjs.com/package/optimal-img-format
- Repository: https://github.com/lbb00/optimal-img-format
- Homepage: https://github.com/lbb00/optimal-img-format#readme
- Issues: https://github.com/lbb00/optimal-img-format/issues
- npm.io page: https://npm.io/package/optimal-img-format

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.5.0 (latest) — 2024-09-03
- 1.4.0 — 2023-12-18
- 1.3.1 — 2023-12-17
- 1.3.0 — 2023-12-17
- 1.2.0 — 2023-12-11
- 1.1.0 — 2023-12-10
- 1.0.0 — 2023-12-04

## README

# optimal-img-format

[![Npm](https://badgen.net/npm/v/optimal-img-format)](https://www.npmjs.com/package/optimal-img-format)
[![Bundlephobia](https://badgen.net/bundlephobia/minzip/optimal-img-format)](https://bundlephobia.com/result?p=optimal-img-format)
[![Coverage](https://img.shields.io/codecov/c/github/lbb00/optimal-img-format.svg)](https://codecov.io/gh/lbb00/optimal-img-format)
![Typescript](https://img.shields.io/badge/TS-Typescript-blue)
[![License](https://img.shields.io/github/license/lbb00/optimal-img-format.svg)](https://github.com/lbb00/optimal-img-format/blob/master/LICENSE)
[![Npm download](https://img.shields.io/npm/dw/optimal-img-format.svg)](https://www.npmjs.com/package/optimal-img-format)

> Get optimal image format on the browser and server.

- Support for AVIF, WebP and JPEG XL
- Zero dependency
- Fast, using promise singleton and cache for detecting

[Online demo](https://lbb00-OptimalImgFormatDemo.web.val.run)

## Usage

### Install

```bash
npm install optimal-img-format
```

### Formats

Formats refer to the subtypes of the MIME type `images/*`.

- jxl
- avif
- webp

### Browser

```javascript
import {
  getOptimalImgFormatOnBrowser,
  isSupportWebP,
} from "optimal-img-format";

const format = await getOptimalImgFormatOnBrowser(["avif", "webp"]);
const formatWebp = await isSupportWebP();
```

Detecting browser supports by:

- [ImageDecoder.isTypeSupported](https://developer.mozilla.org/en-US/docs/Web/API/ImageDecoder/isTypeSupported_static)
- <https://avif.io/blog/tutorials/css/#avifsupportdetectionscript>

### Server

```javascript
import {
  getOptimalImgFormatByAccept,
  getOptimalImgFormatByAgent,
} from "optimal-img-format";

const format =
  getOptimalImgFormatByAccept(request.headers.accept, ["avif", "webp"]) ||
  getOptimalImgFormatByAgent(request.headers.agent, ["avif", "webp"]);
```

## Changelog

[CHANGELOG](./CHANGELOG.md)

## Contributions

If you have any suggestions for this project or discover a bug, please raise an [Issue](https://github.com/lbb00/optimal-img-format/issues).

## License

[UNLICENSE](./LICENSE)

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