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

Get error "extract_area: bad extract area" when second extract call in toBuffer() #4105

Closed
flyforme2016 opened this issue May 16, 2024 · 2 comments
Labels

Comments

@flyforme2016
Copy link

Sharp Version: 0.32.1

Description:
My goal is to extract frames from an animated WebP file. However, a 'bad extract area' error occurs when calling toBuffer() in the second loop.

Sample image
https://cafe24img.poxo.com/benitomaster/web/product/small/202404/e45078df2e402a9b4a9e48bfc08b0b0e.webp

async function decodeAwebp(buffer) {
    const webpImage = sharp(buffer, { animated: true, pages: -1 });
    const metadata = await webpImage.metadata();
    const { pages, width, height, pageHeight, delay } = metadata;

    if (!pages || pages < 2) {
        throw new Error('This WebP image is not animated.');
    }
    const frames = [];

    for (let i = 0; i < pages; i++) {
        const frame = image
            .extract({ left: 0, top: pageHeight * i, width, height: pageHeight }) 
            .ensureAlpha()
            .raw()
            .toBuffer();
        frames.push(frame);
    }

    return {
        width,
        pageHeight,
        delay,
        frames
    };
}

Problem:
The code runs without issue for the first frame, but when it reaches the second loop, I encounter a bad extract area error during the toBuffer() call.

Is there an issue with my code, or is this a potential bug in the Sharp library? Any guidance or assistance would be greatly appreciated.

@lovell
Copy link
Owner

lovell commented May 16, 2024

The page constructor parameter allows you to select a specific frame/page, no need to roll you own logic.

-        const frame = image
-            .extract({ left: 0, top: pageHeight * i, width, height: pageHeight }) 
+        const frame = await sharp(buffer, { animated: true, page: i })

@lovell
Copy link
Owner

lovell commented Jun 1, 2024

I hope this information helped. Please feel free to re-open with more details if further assistance is required.

@lovell lovell closed this as completed Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants