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

Error "text: invalid value (typically too big) for the size of the input" #4071

Closed
3 tasks done
Chaitanyabsprip opened this issue Apr 18, 2024 · 10 comments
Closed
3 tasks done
Labels

Comments

@Chaitanyabsprip
Copy link

Possible bug

Is this a possible bug in a feature of sharp, unrelated to installation?

  • Running npm install sharp completes without error.
  • Running node -e "require('sharp')" completes without error.

If you cannot confirm both of these, please open an installation issue instead.

Are you using the latest version of sharp?

  • I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of sharp that is not the latest, please open an issue against that package instead.

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

System: OS: Linux 6.6 Alpine Linux CPU: (8) arm64 unknown Memory: 2.98 GB / 3.83 GB Container: Yes Shell: 1.36.1 - /bin/ash Binaries: Node: 20.12.2 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 10.5.0 - /usr/local/bin/npm npmPackages: sharp: ^0.33.3 => 0.33.3

What are the steps to reproduce?

const sharp = require('sharp');

async function addText() {
    const img = sharp('./images/image.jpg')
    const metadata = await img.metadata()
    const width = metadata.width;
    const height = metadata.height;

    const text = sharp({
        text: {
            text: '<span foreground="#ffffff" background="#00000066">19.0748\n72.8856\n2024-04-16 13:37</span>',
            rgba: true,
            width: width,
            height: height * 0.1,
            align: 'right',
        },
    })

    const buf = await text.toBuffer()
    console.log(await text.metadata())

    img.composite([{
        input: buf,
        gravity: 'southeast',
    }])
        .toFile(__dirname + '/pimages/text_robo.jpg')
}
addText();

What is the expected behaviour?

The text should composite over the image without an error.

Please provide sample image(s) that help explain this problem

https://imgur.com/a/AsjIlNl

I even tried to resize the text sharp object to 20x20 and I still got the error.

@lovell
Copy link
Owner

lovell commented Apr 18, 2024

-     const buf = await text.toBuffer()
+     const buf = await text.png().toBuffer()

@Chaitanyabsprip
Copy link
Author

same error with that, I've also tried const buf = await tex.png({compressionLevel: 0}).toBuffer(), same error with that as well.

@lovell
Copy link
Owner

lovell commented Apr 19, 2024

Error converting...

I still got the error.

same error

Please can you provide the error message.

@Chaitanyabsprip
Copy link
Author

/app/node_modules/sharp/lib/output.js:161
  const stack = Error();
                ^

Error: text: invalid value (typically too big) for the size of the input (surface, pattern, etc.)
    at Sharp.toBuffer (/app/node_modules/sharp/lib/output.js:161:17)
    at addText (/app/_text.js:19:34)

Node.js v20.12.2

@lovell
Copy link
Owner

lovell commented Apr 19, 2024

And what are the specific values of text.width and text.height that produce this error?

@Chaitanyabsprip
Copy link
Author

width: 201, height: 18.9, however when I resize the text image like text.resize(20, 20).png().toBuffer() I still get the same error.

@lovell
Copy link
Owner

lovell commented Apr 19, 2024

-            height: height * 0.1,
+            height: Math.round(height * 0.1),

@Chaitanyabsprip
Copy link
Author

I made the change you suggested, and I still get the same error.

const sharp = require('sharp');

async function addText() {
    const img = sharp('./images/robo.jpg')
    const metadata = await img.metadata()
    const width = metadata.width;
    const height = metadata.height;

    let text = sharp({
        text: {
            text: '<span foreground="#ffffff" background="#00000066">19.0748\n72.8856\n2024-04-16 13:37</span>',
            rgba: true,
            width: width,
            height: Math.round(height * 0.1),
            align: 'right',
        },
    })
    console.log(text)

    const buf = await text.png().toBuffer()
    console.log(await text.metadata())

    img.composite([{
        input: buf,
        gravity: 'southeast',
    }])
        .toFile(__dirname + '/pimages/text_robo.jpg')
}

addText();
/app/node_modules/sharp/lib/input.js:486
  const stack = Error();
                ^

Error: text: invalid value (typically too big) for the size of the input (surface, pattern, etc.)
    at Sharp.metadata (/app/node_modules/sharp/lib/input.js:486:17)
    at addText (/app/_text.js:23:28)

Node.js v20.12.2

@lovell lovell changed the title Error converting sharp object with text to buffer, in an attempt to composite over image. Error "text: invalid value (typically too big) for the size of the input" Apr 19, 2024
@lovell
Copy link
Owner

lovell commented Apr 19, 2024

Commit a1309aa tightens the validation of some of the text properties to ensure integers are provided.

@Chaitanyabsprip
Copy link
Author

I believe that was the issue, I was able to make it work by rounding the dimensions. However, in an alpine docker image, I had to add the fontconfig and font-noto packages to make it work.

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