An open API service indexing awesome lists of open source software.

https://github.com/area44/node-image-meta-fetcher

Fetch metadata for images in a directory
https://github.com/area44/node-image-meta-fetcher

dimensions json nodejs plaiceholder sharp

Last synced: 2 months ago
JSON representation

Fetch metadata for images in a directory

Awesome Lists containing this project

README

          

# πŸ“Έ Image Meta Fetcher

**Image Meta Fetcher** is a lightweight Node.js utility that extracts image metadata from a folder using [Sharp](https://github.com/lovell/sharp) β€” including image dimensions, format, and a base64-encoded thumbnail preview.

It’s ideal for static site generators, gallery builders, or scripts that need fast metadata and previews without loading full images in the browser.

## ⚑ Features

* πŸ” Extracts width, height, format
* πŸ–ΌοΈ Generates base64 previews (default size: 10Γ—10)
* πŸ“ Supports glob patterns (e.g. `images/*.{jpg,png}`)
* βš™οΈ Optional resizing and sorting
* πŸ§ͺ Tested with [Vitest](https://vitest.dev/)
* πŸ”§ Built with [Bun](https://bun.sh)

## πŸš€ Getting Started

### 1. Clone the repository

```bash
git clone https://github.com/AREA44/node-image-meta-fetcher
cd node-image-meta-fetcher
```

### 2. Install dependencies

```bash
bun install
```

## πŸ–ΌοΈ Usage Example

Add images (`.jpg`, `.jpeg`, `.png`, `.webp`) to the `examples/assets/` folder.

### Run the basic example:

```bash
bun run examples/basic.js
```

This will print image metadata to the console.

### Run with custom options:

```bash
bun run examples/options.js
```

This will:

* Resize each preview to `20x20`
* Disable filename sorting
* Write the result to `examples/output/thumbnails.json`

## πŸ“¦ Output Format

Each image is returned as an object like:

```json
{
"src": "example.jpg",
"width": 1024,
"height": 768,
"format": "jpeg",
"base64": "data:image/jpeg;base64,..."
}
```

| Field | Description |
| -------- | ------------------------------------------------------ |
| `src` | File name (e.g. `photo.jpg`) |
| `width` | Original width in pixels |
| `height` | Original height in pixels |
| `format` | File format (`jpeg`, `png`, `webp`, etc.) |
| `base64` | Base64 preview of a resized thumbnail (default: 10Γ—10) |

## πŸ“š API

```js
ImageMetaFetcher(globPattern, options?)
```

| Parameter | Type | Description |
| ------------- | ---------------------- | ------------------------------------------------- |
| `globPattern` | `string` or `string[]` | File path(s) to match (e.g. `images/*.{jpg,png}`) |
| `options` | `object` | Optional configuration |

### Options

```js
{
resize: {
width: 10,
height: 10,
fit: "inside" // or "cover", "contain", etc. (from sharp)
},
sort: true // sort results by filename
}
```

## πŸ§ͺ Running Tests

Run the full test suite:

```bash
bun test
```

Watch mode:

```bash
bun test:watch
```

Tests cover:

* Basic metadata extraction
* Sorting
* Thumbnail resizing
* Handling invalid/corrupt images

## πŸ“ Project Structure

```
.
β”œβ”€β”€ examples/
β”‚ β”œβ”€β”€ assets/ # Sample input images
β”‚ β”œβ”€β”€ basic.js # Basic usage example
β”‚ └── options.js # Example with custom options
β”œβ”€β”€ tests/
β”‚ β”œβ”€β”€ fixtures/ # Test images (valid + broken)
β”‚ └── ImageMetaFetcher.test.js
β”œβ”€β”€ index.js # Main module
β”œβ”€β”€ README.md
└── package.json
```

## πŸ“„ License

MIT Β© [AREA44](https://github.com/area44)