Sharp: I don't know the difference between these two codes. input missing error.

Created on 18 Jul 2019  路  2Comments  路  Source: lovell/sharp

What are you trying to achieve?
I'd like to know the difference between the two codes I've tried in an image size situation.

Have you searched for similar questions?
Yes But I couldn't get a clear answer.

Are you able to provide a standalone code sample that demonstrates this question?
yes

Are you able to provide a sample image that helps explain the question?
yes

this code work

sharp('./vvvanillA.jpg')
    .resize(200)
    .jpeg()
    .toBuffer()
    .then( data => {
        fs.writeFileSync('./yellow.jpg', data);
    })
    .catch( err => {
        console.log(err);
    }); 

This code does not work and causes an error.
"input missing"

fs.readdir(dirname, function(err , filenames) {
    if (err) {
      return;
    }
    filenames.forEach(function(filename) {
      fs.readFile(dirname + "/" + filename, 'utf-8', async function(err, content) {
        if(filename.includes("jpg")){
          sharp(content).resize(300, 400).toFile("transformed.jpg").then((info) => {
            fs.writeFileSync('test1.jpg', info);
          }).catch(err=>console.log(err));
        }

        if (err) {
          return;
        }
      });
    });
  });

Hello.
I have a question.

The second code has an error and the first code works properly.

I don't know the difference between these two codes.
From the list of differences that appear to be explicit,
The input of the first code directly refers to the file.
The second code is the content used in the callback using readFile.

I tried debugging to solve the error.
What I deduced is that...
The idea is that the content from the callback of the second code fs.readFile is not the input of the desired form in the sharp.

So I tried to check the sourcode but I couldn't check the source code of the sharp because it was not js.

I want you to let me know the error in my code.

Thank you.

question

Most helpful comment

Hi, this code has a race condition where multiple instance of sharp can all write to "transformed.jpg" at the same time. Also, the resolved info is an Object containing metadata rather than image data.

This sort of question might be more appropriate for StackOverflow.

All 2 comments

Hi, this code has a race condition where multiple instance of sharp can all write to "transformed.jpg" at the same time. Also, the resolved info is an Object containing metadata rather than image data.

This sort of question might be more appropriate for StackOverflow.

Thank you very much. I saw the error 'missing input'. And I thought it was an error caused by different input values that are accepted by 'sharp' when it is executed with readfile and directly by 'path'.

Was this page helpful?
0 / 5 - 0 ratings