Skip to content

Commit

Permalink
Let Jimp.read accept a Buffer (#1332)
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie authored Sep 7, 2024
1 parent 314718f commit 11cd408
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export function createJimp<
}

/**
* Create a Jimp instance from a URL or a file path
* Create a Jimp instance from a URL, a file path, or a Buffer
* @example
* ```ts
* import { Jimp } from "jimp";
Expand All @@ -213,9 +213,13 @@ export function createJimp<
* const image = await Jimp.read("https://upload.wikimedia.org/wikipedia/commons/0/01/Bot-Test.jpg");
* ```
*/
static async read(url: string) {
static async read(url: string | Buffer | ArrayBuffer) {
if (Buffer.isBuffer(url) || url instanceof ArrayBuffer) {
return this.fromBuffer(url);
}

if (existsSync(url)) {
return Jimp.fromBuffer(await readFile(url));
return this.fromBuffer(await readFile(url));
}

const [fetchErr, response] = await to(fetch(url));
Expand Down
2 changes: 2 additions & 0 deletions packages/docs/src/content/docs/guides/migrate-to-v1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ async function main() {
}
```

It will also read from a `Buffer` or `ArrayBuffer`.

### `Jimp.fromBuffer`

You can load an image from a buffer.
Expand Down

0 comments on commit 11cd408

Please sign in to comment.