How to determine image format from Base64 encoded string without prefix?

41 views Asked by At

I received a base64 encoded string of an image from a friend, but the string doesn't include the standard prefix (e.g., "data:image/png;base64," "data:image/jpeg;base64," etc.) to indicate the image format. Without this prefix, I'm unable to determine the image format (JPEG, PNG, WebP, etc.).

Can I somehow determine and display the image format at runtime without knowing the exact image format beforehand? I've tried appending different prefixes (e.g., "data:image/png;base64,") to the base64 encoded string and displaying it as an image, but it didn't work consistently. For example, when the image is encoded in the WebP format but I tried displaying it as PNG, it didn't show up on the website.

Is there a reliable way to determine the image format from a base64 encoded string without the prefix and display it accordingly? I need a solution that works dynamically at runtime. Any suggestions or guidance would be greatly appreciated. Thank you!

0

There are 0 answers