# avatar-local-cache

> Saves a image URL for an avatar to the local file system (and optimizes the image). Read the [blog post that inspired this utility: A Featherweight Facepile](https://www.zachleat.com/web/featherweight-facepile/).

Latest version **2.0.6** (published 2020-02-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install avatar-local-cache
pnpm add avatar-local-cache
yarn add avatar-local-cache
bun add avatar-local-cache
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.6 |
| Published | 2020-02-13 |
| First published | 2019-08-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 22.2 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | zachleat |
| Keywords | avatar |

## Links

- npm: https://www.npmjs.com/package/avatar-local-cache
- npm.io page: https://npm.io/package/avatar-local-cache

## Dependencies (5)

- [sharp](https://npm.io/package/sharp.md) ^0.23.0
- [imagemin](https://npm.io/package/imagemin.md) ^7.0.0
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.0
- [imagemin-jpegtran](https://npm.io/package/imagemin-jpegtran.md) ^6.0.0
- [imagemin-pngquant](https://npm.io/package/imagemin-pngquant.md) ^8.0.0

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 2.0.6 (latest) — 2020-02-13
- 2.0.5 — 2019-10-10
- 2.0.4 — 2019-10-10
- 2.0.3 — 2019-08-23
- 2.0.2 — 2019-08-23
- 2.0.1 — 2019-08-23
- 2.0.0 — 2019-08-22
- 1.0.3 — 2019-08-20
- 1.0.2 — 2019-08-20
- 1.0.1 — 2019-08-20
- 1.0.0 — 2019-08-20

## README

# Avatar Local Cache

Saves a image URL for an avatar to the local file system (and optimizes the image). Read the [blog post that inspired this utility: A Featherweight Facepile](https://www.zachleat.com/web/featherweight-facepile/).

* Workaround for large avatar images on hosted services.
* Image URLs won’t break in the future.
* Only keeps the smallest images (`webp` if smallest and one of `jpg` or `png`)

## Install

```
npm install avatar-local-cache
```

## Usage

The default behavior will only keep `webp` if it has the smallest file size and then will pick one of `jpg` or `png` based on which one is the smallest of the two. To disable this, see the `Keep all formats` example below.

```js
const AvatarLocalCache = require("avatar-local-cache");

let cache = new AvatarLocalCache();
cache.fetchUrl("https://opencollective-production.[…].jpeg", "nhoizey").then(function(files) {
    console.log( `Wrote ${files.map(entry => entry.path).join(", ")}.` );
});
```

The above writes three files but only keeps one: `nhoizey.webp` (20KB), `nhoizey.jpg` (22KB), and `nhoizey.png` (9KB).

1. It only keeps the `webp` file if it is the smallest (it is not).
2. It then picks the smaller of the `jpg` and the `png` (in this case, the `png` wins by 13KB).

This allows you to iterate over the object returned from the promise to create an `img` (if only one source remains) or a `picture` element (if the `webp` survived alongside a `jpg` or `png`).

### Image Maximum Width

Images will be resized down to this width. Images smaller than this width will not be resized.

```js
let cache = new AvatarLocalCache();
cache.width = 400;
```

### Change formats

```js
let cache = new AvatarLocalCache();
cache.formats = ["jpeg"];

// or
cache.formats = ["png"];

// or (order doesn’t matter)
cache.formats = ["webp", "jpeg"];
```

### Keep all formats

To disable this file size comparison and file pruning, just set `onlyKeepSmallestFormats` to false.

```js
let cache = new AvatarLocalCache();
cache.onlyKeepSmallestFormats = false;
```

### Skip Metadata

_Added in 2.0.6_

Faster. Won’t return size or `sharp` metadata.

```js
let cache = new AvatarLocalCache();
cache.skipMetadata = true;
```

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