简体   繁体   中英

Converting a base64-encoded jpeg to a png in React in browser

I have a base64-encoded jpeg string that I'm holding in state in React. I've been trying to work out how to convert it to a png format base64 string browser-side.

I briefly looked at sharp , but I found that requires a server side node.js environment. I don't really want to have to write my own conversion script. Further searching on npm hasn't yielded anything for me.

Any help with this is very much appreciated.

This is untested, but you should be able to set the base64 JPEG as the source of an image, then draw that image to a canvas using context.drawImage ; once it's on a canvas, you can use canvas.toDataURL() to get a base64 PNG of it.

You can use the base64 image to display your image without converting it.

See this answer for a reference

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM