jimp-dev / jimp-dev/jimp

Decoder options are not passed to the underlying decoder when using `Jimp.read()` for buffers or files

Open
#1,356 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
14.7k
Forks
777
PR merge metrics
No merged PRs in 30d

Description

# Expected Behavior

When using `Jimp.read()`, decoder options, e.g.
```js
Jimp.read(buffer, { 'image/jpeg': { maxMemoryUsageInMB: 2048 } }))
```
are passed to the decoder when decoding an image from a Buffer or a file.

# Current Behavior

Decoder options are not passed to the underlying decoder, unless the first argument in `Jimp.read(url, options)` is a URL. This leads to, e.g., the inability to decode large JPEGs due to memory errors, since `maxMemoryUsageInMB` cannot be set.

# Failure Information (for bugs)

I suspect the issue is that the `read()` method returns `this.fromBuffer(...)` instead of `this.fromBuffer(..., options)` unless the first argument is a URL.

```ts
static async read(
url: string | Buffer | ArrayBuffer,
options?: MimeTypeToDecodeOptions
) {
if (Buffer.isBuffer(url) || url instanceof ArrayBuffer) {
return this.fromBuffer(url); // <--------------------------------- here
}

if (existsSync(url)) {
return this.fromBuffer(await readFile(url)); // <---------------------- here
}

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

if (fetchErr) {
throw new Error(`Could not load Buffer from URL: ${url}`);
}

if (!response.ok) {
throw new Error(`HTTP Status ${response.status} for url ${url}`);
}

const [arrayBufferErr, data] = await to(response.arrayBuffer());

if (arrayBufferErr) {
throw new Error(`Could not load Buffer from ${url}`);
}

const buffer = bufferFromArrayBuffer(data);
return this.fromBuffer(buffer, options); // <---------------------- but here returns correctly
}
```

## Context

- Jimp Version: 1.6.0
- Operating System: Windows 10
- Node version: 22.11.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.