Skip to content

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

Open
@DefiantCatgirl

Description

@DefiantCatgirl

Expected Behavior

When using Jimp.read(), decoder options, e.g.

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.

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions