Skip to content

Commit 9368df4

Browse files
committed
support type data img source, remove duplicate packages, support ReadableStream for placeholder and meta
1 parent b33886d commit 9368df4

40 files changed

+383
-816
lines changed

README.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@ The easiest way to get started is to integrate openimg as an image optimization
1818
- [openimg/react](./docs/api/react.md): Image React component to query for optimized images
1919
- [openimg/vite](./docs/api/vite.md): Vite plugin for integrating local image assets from your Vite project
2020

21-
## Changelogs
21+
## Changelog
2222

23-
Find the changelogs for each npm package here:
24-
25-
- [openimg](./packages/core/CHANGELOG.md)
26-
- [openimg-bun](./packages/bun/CHANGELOG.md)
27-
- [openimg-node](./packages/node/CHANGELOG.md)
28-
- [openimg-react](./packages/react/CHANGELOG.md)
23+
Find the changelog here: [CHANGELOG.md](./packages/core/CHANGELOG.md)
2924

3025
## Motivation & references
3126

bun.lock

+124-232
Large diffs are not rendered by default.

bun.lockb

-32.5 KB
Binary file not shown.

docs/api/bun.md

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# openimg/bun
22

3-
openimg/bun (Open Image Bun) provides an HTTP request handler function to optimize images using sharp, using Bun APIs where possible. It also provides utilities for generating low quality image placeholders and retrieving image metadata.
3+
openimg/bun (Open Image Bun) provides an HTTP request handler function to optimize images with [sharp](https://sharp.pixelplumbing.com), using Bun APIs where possible. It also provides utilities for generating low quality image placeholders and retrieving image metadata.
44

55
## Features
66

@@ -12,19 +12,13 @@ You can find the API reference for each function below.
1212

1313
## Installation
1414

15-
The code is available via `openimg/bun` and `openimg-bun`. For most use cases, you probably want to install `openimg`:
15+
The code is available via `openimg/bun`:
1616

1717
```bash
1818
npm i sharp openimg
1919
```
2020

21-
However, if you only want to use the server-side utilities for Bun, you can also install `openimg-bun`:
22-
23-
```bash
24-
npm i sharp openimg-bun
25-
```
26-
27-
openimg package uses [sharp](https://sharp.pixelplumbing.com) and can only be used in environments that can run sharp (and [libvips](https://github.com/libvips/libvips)).
21+
Note, openimg uses [sharp](https://sharp.pixelplumbing.com) and can only be used in environments that can run sharp (and [libvips](https://github.com/libvips/libvips)).
2822

2923
## getImgResponse
3024

@@ -254,7 +248,7 @@ getImgResponse(request, { getImgSource });
254248

255249
## getImgPlaceholder
256250

257-
Import `getImgPlaceholder` from `openimg/bun` and pass in a Readable stream (like `ReadStream`) or image Buffer. The function will return a low quality image placeholder as a base64-encoded string using [thumbhash](https://github.com/evanw/thumbhash). The generated string can be stored in a database or inlined in your client bundle as a placeholder until the full image is loaded.
251+
Import `getImgPlaceholder` from `openimg/bun` and pass in a Readable stream (like `ReadStream`), a ReadableStream, or image Buffer. The function will return a low quality image placeholder as a base64-encoded string using [thumbhash](https://github.com/evanw/thumbhash). The generated string can be stored in a database or inlined in your client bundle as a placeholder until the full image is loaded.
258252

259253
Example using a Readable stream:
260254

@@ -267,6 +261,17 @@ const placeholder = await getImgPlaceholder(stream);
267261
console.log(placeholder); // data:image/png;base64,...
268262
```
269263

264+
Example using a ReadableStream:
265+
266+
```typescript
267+
import { getImgPlaceholder } from "openimg/bun";
268+
269+
const response = await fetch("https://example.com/cat.png");
270+
const readableStream = response.body;
271+
const placeholder = await getImgPlaceholder(readableStream);
272+
console.log(placeholder); // data:image/png;base64,...
273+
```
274+
270275
Example using an image Buffer:
271276

272277
```typescript
@@ -280,7 +285,7 @@ console.log(placeholder); // data:image/png;base64,...
280285

281286
## getImgMetadata
282287

283-
Import `getImgMetadata` from `openimg/bun` and pass in a Readable stream (like `ReadStream`) or image Buffer. The function will return the width, height, and format of the image.
288+
Import `getImgMetadata` from `openimg/bun` and pass in a Readable stream (like `ReadStream`), a ReadableStream, or image Buffer. The function will return the width, height, and format of the image.
284289

285290
Example using a Readable stream:
286291

@@ -289,8 +294,19 @@ import { getImgMetadata } from "openimg/bun";
289294
import { createReadStream } from "node:fs";
290295

291296
const stream = createReadStream("./public/cat.png");
292-
const placeholder = await getImgMetadata(stream);
293-
console.log(placeholder); // { width: 300, height: 300, format: "png" }
297+
const metadata = await getImgMetadata(stream);
298+
console.log(metadata); // { width: 300, height: 300, format: "png" }
299+
```
300+
301+
Example using a ReadableStream:
302+
303+
```typescript
304+
import { getImgMetadata } from "openimg/bun";
305+
306+
const response = await fetch("https://example.com/cat.png");
307+
const readableStream = response.body;
308+
const metadata = await getImgMetadata(readableStream);
309+
console.log(metadata); // { width: 300, height: 300, format: "png" }
294310
```
295311

296312
Example using an image Buffer:
@@ -299,7 +315,7 @@ Example using an image Buffer:
299315
import { getImgMetadata } from "openimg/bun";
300316
import { readFileSync } from "node:fs";
301317

302-
const buffer = getImgMetadata("./public/cat.png");
303-
const placeholder = await readFileSync(buffer);
304-
console.log(placeholder); // { width: 300, height: 300, format: "png" }
318+
const buffer = readFileSync("./public/cat.png");
319+
const metadata = await getImgMetadata(buffer);
320+
console.log(metadata); // { width: 300, height: 300, format: "png" }
305321
```

docs/api/node.md

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# openimg/node
22

3-
openimg/node (Open Image Node) provides a HTTP request handler function to optimize images using sharp, using Node-compatible APIs for Bun, Deno, and Node.js. It also provides utilities for generating low quality image placeholders and retrieving image metadata.
3+
openimg/node (Open Image Node) provides a HTTP request handler function to optimize images with [sharp](https://sharp.pixelplumbing.com), using Node-compatible APIs for Bun, Deno, and Node.js. It also provides utilities for generating low quality image placeholders and retrieving image metadata.
44

55
## Features
66

@@ -16,19 +16,13 @@ The `openimg/bun` and `openimg/node` request handlers utilize the Web Fetch API'
1616

1717
## Installation
1818

19-
The code is available via `openimg/node` and `openimg-node`. For most use cases, you probably want to install `openimg`:
19+
The code is available via `openimg/node`:
2020

2121
```bash
2222
npm i sharp openimg
2323
```
2424

25-
However, if you only want to use the server-side utilities for Node, you can also install `openimg-node`:
26-
27-
```bash
28-
npm i sharp openimg-node
29-
```
30-
31-
openimg package uses [sharp](https://sharp.pixelplumbing.com) and can only be used in environments that can run sharp (and [libvips](https://github.com/libvips/libvips)).
25+
Note, openimg uses [sharp](https://sharp.pixelplumbing.com) and can only be used in environments that can run sharp (and [libvips](https://github.com/libvips/libvips)).
3226

3327
## getImgResponse
3428

@@ -258,7 +252,7 @@ getImgResponse(request, { getImgSource });
258252

259253
## getImgPlaceholder
260254

261-
Import `getImgPlaceholder` from `openimg/node` and pass in a Readable stream (like `ReadStream`) or image Buffer. The function will return a low quality image placeholder as a base64-encoded string using [thumbhash](https://github.com/evanw/thumbhash). The generated string can be stored in a database or inlined in your client bundle as a placeholder until the full image is loaded.
255+
Import `getImgPlaceholder` from `openimg/node` and pass in a Readable stream (like `ReadStream`), a ReadableStream, or image Buffer. The function will return a low quality image placeholder as a base64-encoded string using [thumbhash](https://github.com/evanw/thumbhash). The generated string can be stored in a database or inlined in your client bundle as a placeholder until the full image is loaded.
262256

263257
Example using a Readable stream:
264258

@@ -271,6 +265,17 @@ const placeholder = await getImgPlaceholder(stream);
271265
console.log(placeholder); // data:image/png;base64,...
272266
```
273267

268+
Example using a ReadableStream:
269+
270+
```typescript
271+
import { getImgPlaceholder } from "openimg/node";
272+
273+
const response = await fetch("https://example.com/cat.png");
274+
const readableStream = response.body;
275+
const placeholder = await getImgPlaceholder(readableStream);
276+
console.log(placeholder); // data:image/png;base64,...
277+
```
278+
274279
Example using an image Buffer:
275280

276281
```typescript
@@ -284,7 +289,7 @@ console.log(placeholder); // data:image/png;base64,...
284289

285290
## getImgMetadata
286291

287-
Import `getImgMetadata` from `openimg/node` and pass in a Readable stream (like `ReadStream`) or image Buffer. The function will return the width, height, and format of the image.
292+
Import `getImgMetadata` from `openimg/node` and pass in a Readable stream (like `ReadStream`), a ReadableStream, or image Buffer. The function will return the width, height, and format of the image.
288293

289294
Example using a Readable stream:
290295

@@ -293,8 +298,19 @@ import { getImgMetadata } from "openimg/node";
293298
import { createReadStream } from "node:fs";
294299

295300
const stream = createReadStream("./public/cat.png");
296-
const placeholder = await getImgMetadata(stream);
297-
console.log(placeholder); // { width: 300, height: 300, format: "png" }
301+
const metadata = await getImgMetadata(stream);
302+
console.log(metadata); // { width: 300, height: 300, format: "png" }
303+
```
304+
305+
Example using a ReadableStream:
306+
307+
```typescript
308+
import { getImgMetadata } from "openimg/node";
309+
310+
const response = await fetch("https://example.com/cat.png");
311+
const readableStream = response.body;
312+
const metadata = await getImgMetadata(readableStream);
313+
console.log(metadata); // { width: 300, height: 300, format: "png" }
298314
```
299315

300316
Example using an image Buffer:
@@ -303,7 +319,7 @@ Example using an image Buffer:
303319
import { getImgMetadata } from "openimg/node";
304320
import { readFileSync } from "node:fs";
305321

306-
const buffer = getImgMetadata("./public/cat.png");
307-
const placeholder = await readFileSync(buffer);
308-
console.log(placeholder); // { width: 300, height: 300, format: "png" }
322+
const buffer = readFileSync("./public/cat.png");
323+
const metadata = await getImgMetadata(buffer);
324+
console.log(metadata); // { width: 300, height: 300, format: "png" }
309325
```

docs/api/react.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# openimg/react
22

3-
openimg-react (Open Image React) provides an `Image` component (aliased as `Img`) for querying optimized images from an image optimization endpoint (e.g., openimg/node) and a context provider `OpenImgContextProvider` for global configuration options. The `Image` component renders an HTML picture element utilizing modern HTML attributes and best practices.
3+
openimg/react (Open Image React) provides an `Image` component (aliased as `Img`) for querying optimized images from an image optimization endpoint (e.g., openimg/node or openimg/bun) and a context provider `OpenImgContextProvider` for global configuration options. The `Image` component renders an HTML picture element utilizing modern HTML attributes and best practices.
44

55
## Features
66

@@ -14,20 +14,14 @@ openimg-react (Open Image React) provides an `Image` component (aliased as `Img`
1414

1515
## Installation
1616

17-
The code is available via `openimg/react` and `openimg-react`. For most use cases, you probably want to install `openimg`:
17+
The code is available via `openimg/react`:
1818

1919
```bash
2020
npm i sharp openimg
2121
```
2222

2323
Note, `sharp` is only needed if you want to use the server-side functionality.
2424

25-
If you only want to use the React utilities, you can also install `openimg-react` independently:
26-
27-
```bash
28-
npm i openimg-react
29-
```
30-
3125
## API
3226

3327
Import the `Img`/`Image` component from `openimg/react`:

packages/bun/CHANGELOG.md

-32
This file was deleted.

packages/bun/README.md

-7
This file was deleted.

packages/bun/package.json

-38
This file was deleted.

packages/bun/scripts/build.sh

-19
This file was deleted.

packages/core/CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
# 0.7.0
4+
5+
- Remove `openimg-node`, `openimg-bun`, and `openimg-react` duplicate packages.
6+
7+
## openimg/node & openimg/bun
8+
9+
- Add support for `getImgSource` to return `type` `"data"` with a `data` property that contains the image data as a buffer or stream.
10+
- `getImgPlaceholder` now supports passing in a `ReadableStream`.
11+
- `getImgMetadata` now supports passing in a `ReadableStream`.
12+
313
# 0.6.0
414

515
## openimg/node & openimg/bun

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openimg",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"homepage": "https://github.com/andrelandgraf/openimg",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)