Skip to content

Commit 11cd408

Browse files
authoredSep 7, 2024··
Let Jimp.read accept a Buffer (#1332)
1 parent 314718f commit 11cd408

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed
 

‎packages/core/src/index.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export function createJimp<
201201
}
202202

203203
/**
204-
* Create a Jimp instance from a URL or a file path
204+
* Create a Jimp instance from a URL, a file path, or a Buffer
205205
* @example
206206
* ```ts
207207
* import { Jimp } from "jimp";
@@ -213,9 +213,13 @@ export function createJimp<
213213
* const image = await Jimp.read("https://upload.wikimedia.org/wikipedia/commons/0/01/Bot-Test.jpg");
214214
* ```
215215
*/
216-
static async read(url: string) {
216+
static async read(url: string | Buffer | ArrayBuffer) {
217+
if (Buffer.isBuffer(url) || url instanceof ArrayBuffer) {
218+
return this.fromBuffer(url);
219+
}
220+
217221
if (existsSync(url)) {
218-
return Jimp.fromBuffer(await readFile(url));
222+
return this.fromBuffer(await readFile(url));
219223
}
220224

221225
const [fetchErr, response] = await to(fetch(url));

‎packages/docs/src/content/docs/guides/migrate-to-v1.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ async function main() {
9191
}
9292
```
9393

94+
It will also read from a `Buffer` or `ArrayBuffer`.
95+
9496
### `Jimp.fromBuffer`
9597

9698
You can load an image from a buffer.

0 commit comments

Comments
 (0)