Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let Jimp.read accept a Buffer #1332

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -80,6 +80,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
Loading