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

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

Open
DefiantCatgirl opened this issue Nov 6, 2024 · 0 comments · May be fixed by #1357

Comments

@DefiantCatgirl
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant