meltingice / psd.js

A Photoshop PSD file parser for NodeJS and browsers
MIT License
2.75k stars 391 forks source link

How to change 'child.layer.image.pixelData' to Blob? #113

Open QRL909109 opened 7 years ago

QRL909109 commented 7 years ago

I want to switch 'child.layer.image.pixelData' into blob file and transfer the file to server, for example,

window.URL.createObjectURL( new Blob([child.layer.image.pixelData], {type: 'image/png'}) )

but I cannot get REAL data when running the program. Any idea/suggestion?

QRL909109 commented 7 years ago

I can use child.layer.image.toBase64() transform to Blob, but the img quality decrease #110, , I change the source code has no effect

psd/image_exports/png.coffee

png = new PNG(filterType: 0, width: @width(), height: @height())

so I want to use pixelData transform to Blob, Any idea/suggestion?

fabulousduck commented 7 years ago

Hey @QRL909109

As the pixelData is an uint8buffer, you could use the new Buffer() function to transform it into a binary buffer. And from there you could make it into a blob.

How to do this you can read here

Regards, Duck

QRL909109 commented 7 years ago

Thanks, Duck I'm sorry I did not make it clear, I use Browser , no Node.
I find child.layer.image.pixelData is typeof Array , child.layer.image.file.data is typeof Uint8Array I convert Uint8Array to ArrayBuffer and then Blob

window.URL.createObjectURL( new Blob([child.layer.image.file.data.buffer], {type: 'image/png'}) )

However, the blob:http://XXX/cb26dafc-a661-484a-9dc9-5511f8817892 in the browser is still a black Any good suggestions?

fabulousduck commented 7 years ago

Hey @QRL909109.

Sorry for taking so long to respond first of all, i did not mean to take this long.

As for the problem of the file displaying as a black (square i assume ? ), I have not been able to pinpoint the problem with the creation of the blob as it is stored correctly in my DBMS. Thus the problem most likely lies somewhere in the code that you use to reconstruct the image into something the browser can show.

If you still have this problem, could you please share the code that you use to display the image once you have obtained from the database ?

QRL909109 commented 6 years ago

psd.js.zip Hey, Duck. I made a Demo, run in the browser