catdad-experiments / heic-convert

🤳 convert heic/heif images to jpeg and png
245 stars 24 forks source link

TypeError: Cannot read property 'slice' of undefined #5

Closed jzarca01 closed 4 years ago

jzarca01 commented 4 years ago

With a multiple image heic

TypeError: Cannot read property 'slice' of undefined
    at uint8ArrayUtf8ByteString (/Users/jeremie/Desktop/dossier sans titre/node_modules/heic-decode/index.js:4:39)
    at isHeic (/Users/jeremie/Desktop/dossier sans titre/node_modules/heic-decode/index.js:10:22)
    at decodeBuffer (/Users/jeremie/Desktop/dossier sans titre/node_modules/heic-decode/index.js:47:8)
    at module.exports (/Users/jeremie/Desktop/dossier sans titre/node_modules/heic-decode/index.js:69:46)
    at convert (/Users/jeremie/Desktop/dossier sans titre/node_modules/heic-convert/index.js:39:25)
    at module.exports (/Users/jeremie/Desktop/dossier sans titre/node_modules/heic-convert/index.js:56:70)
    at init (/Users/jeremie/Desktop/dossier sans titre/heicextract.js:21:26)
catdad commented 4 years ago

Hmm... based on where in the code that throws, it looks like you may not be passing in a buffer? Do you have the code that you used to run into this?

jzarca01 commented 4 years ago

I don't think I did anything wrong, here's my code

async function init() {
  try {
    const inputBuffer = await promisify(fs.readFile)("cat.heic");
    const images = await convert({ inputBuffer, format: "JPEG" });

    for (let idx in images) {
      const image = images[idx];
      const outputBuffer = await image.convert();
      await promisify(fs.writeFile)(`./result-${idx}.jpg`, outputBuffer);
    }
  } catch (err) {
    console.log(err);
  }
}
catdad commented 4 years ago

The property is named buffer and you are providing inputBuffer to the convert function. Try doing

await convert({ buffer: inputBuffer, format: "JPEG" });
jzarca01 commented 4 years ago

You're right, my bad

Now the error seems to be TypeError: image.convert is not a function

catdad commented 4 years ago

I just noticed that my example code had all sorts of issues. That's what I get for writing code directly into the readme late at night. Take a look at it now, it should be all better.

It's essentially the same API as the heic-decode example you fixed earlier. Call convert to do a single image and convert.all to get all images.

jzarca01 commented 4 years ago

Thanks a lot @catdad !!!