johansatge / jpeg-autorotate

📸 Node module to rotate JPEG images based on EXIF orientation.

Home Page:https://www.npmjs.com/package/jpeg-autorotate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to pass Buffer as argument instead of path? How to solve following error?

mounikakella opened this issue · comments

Basically, I have image info in base64 encoded. so, I'm converting the base64 to buffer by using the following code

const thumbnailBuffer = await Buffer.from(thumbnailBase64, "base64");
The Buffer im passing looks like this to the rotate() function.
buffer <Buffer 75 ab 5a 8a 66 a0 7b fa 67 81 b6 ac 7b ae 22 54 13 91 c3 42 86 82 80 00 00 03 52 52 11 14 80 00 00 3b c0 00 00 3b c2 00 80 00 00 25 44 85 3b 80 00 00 ... 8626 more bytes>
Below is the error when Im executing.

An error occurred when rotating the file: Could not read EXIF data (Error: 'load' gots invalid file data.)
(node:21457) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'width' of undefined
    at PDFDocument.image (/home/user/***/***/***/node_modules/pdfkit/js/mixins/images.js:27:17)
    at WriteStream.<anonymous> (/home/user/***/***/***/***/***/file.js:192:11)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
(node:21457) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:21457) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Done in 2.17s.

Please LMK if Im doing any wrong.

commented

If you have your image encoded as base64 you can do the following to get a file buffer:

const fileBuffer = new Buffer(file.replace(/^data:.+;base64,/, ""), 'base64');

Then you can use the rotate method:
const rotateResult = await jo.rotate(fileBuffer, {quality: 100});

I hope that helps your case!

Hello,

since this error was impossible to reproduce, I will go ahead and close the issue.

Please do not hesitate to reopen if you still reproduce and have a more complete example to share :)