Agamnentzar / ag-psd

Javascript library for reading and writing PSD files
Other
504 stars 66 forks source link

Error writePsd on big file #206

Open labsforge opened 3 weeks ago

labsforge commented 3 weeks ago

Hi, I've pin-pointed this problem to when I call writePsd to get an ArrayBuffer or Buffer so I can finally write it to disk:

"RangeError: Array buffer allocation failed
    at new ArrayBuffer (<anonymous>)
    at resizeBuffer (http://localhost:4200/main.js:267873:19)
    at ensureSize (http://localhost:4200/main.js:267883:5)
    at writeBytes (http://localhost:4200/main.js:267382:5)
    at Object.write (http://localhost:4200/main.js:259576:74)
    at http://localhost:4200/main.js:267812:17
    at writeSection (http://localhost:4200/main.js:267477:3)
    at _loop_4 (http://localhost:4200/main.js:267811:7)
    at writeAdditionalLayerInfo (http://localhost:4200/main.js:267820:5)
    at http://localhost:4200/main.js:267595:5"

Everything works fine, but in this case the final file should end up with more than 1Gb. I there a way to refactor the construction of the ArrayBuffer to be done in chunks?

Kind regards!

Agamnentzar commented 3 weeks ago

I don't see any way to write file in chunks in browser: https://developer.mozilla.org/en-US/docs/Web/API/File so I'm not sure how adding chunked write api would help in this case. Reducing memory usage could potentially reduce this problem, but it doesn't guarantee that it will be fixed.

Other things that you can try: don't save thumbnail and composite image if you don't need them, they're not needed if you just plan on opening the file in Photoshop later. You can also try passing { compress: true } in writePsd options, it's still experimental feature though.