HELP! HOW TO Covert Image to base64
Hi,
Had the same issue some 9 months ago and although one would think it would be a subject, I never found any simple answer.
So here it goes, this works for me.
The theory
To encode an image to base64 need to be a binary file.
I tried to do this with the typical image files one have like regular png and jpg, but didnt work.
The only way I’ve found to do this is to first load or download an image,
then save it as a binary and reload it as a binary.
Then it can be encoded to base64
The code
local mime=require("mime")
-- the code assume you have stored your local or downloaded image in the variable image local image -- saving the file as a binary local path=system.pathForFile(image,system.TemporaryDirectory) local file=io.open(path,"wb") file:write(image) io.close(file) file=nil -- loading the binary file local path=system.pathForFile(image,system.TemporaryDirectory) local file=io.open(path,"rb") local temp=file:read("\*a") io.close(file) file=nil -- converting the binary stored in temp to base64 local result=mime.b64(temp) -- thats it! -- and this is how to decode a base64 image local dcode=mime.unb64(result)
Remember that when you want to decode and use an image, in a similar fashion you need to first decode it like shown in code above, then save it to disc using the “w” flag instead of the “wb”, meaning it will no longer be a binary.
You will then be able to load the image to a display object or use it anyway you need.
Hope this helps 
Anaqim
Anaqim is basically right. First, all images are already binary files. That’s important because the file.io functions have to be in “binary” mode to work, i.e. “rb”, “wb” for read binary and write binary.
If you have your image file in a place Corona can access it (i.e. system.TemporaryDirectory, system.CachesDirectory, system.DocumentsDirectory or system.ResourceDirectory for non-Android builds) then you just open the file in binary mode, read it in, pump the data in to the mime.b64() function when you can transmit the string data that mime.b64() outputs to your server that needs it. Depending on what you’re doing you may not need to write it back out to a file.
If you’re building Android apps and you want to get an image from the system.ResourceDirectory (your project folder with main.lua) make sure to read the Gotcha section here: https://docs.coronalabs.com/api/library/system/ResourceDirectory.html
Rob
I did this with an audio file so it could be uploaded from a Corona app to our server and then decoded using ASP.Net at the other end.
https://forums.coronalabs.com/topic/44199-example-file-upload-using-aspnet-and-a-progress-bar/
…just in case you’re trying to do something similar.
Hi,
Had the same issue some 9 months ago and although one would think it would be a subject, I never found any simple answer.
So here it goes, this works for me.
The theory
To encode an image to base64 need to be a binary file.
I tried to do this with the typical image files one have like regular png and jpg, but didnt work.
The only way I’ve found to do this is to first load or download an image,
then save it as a binary and reload it as a binary.
Then it can be encoded to base64
The code
local mime=require("mime")
-- the code assume you have stored your local or downloaded image in the variable image local image -- saving the file as a binary local path=system.pathForFile(image,system.TemporaryDirectory) local file=io.open(path,"wb") file:write(image) io.close(file) file=nil -- loading the binary file local path=system.pathForFile(image,system.TemporaryDirectory) local file=io.open(path,"rb") local temp=file:read("\*a") io.close(file) file=nil -- converting the binary stored in temp to base64 local result=mime.b64(temp) -- thats it! -- and this is how to decode a base64 image local dcode=mime.unb64(result)
Remember that when you want to decode and use an image, in a similar fashion you need to first decode it like shown in code above, then save it to disc using the “w” flag instead of the “wb”, meaning it will no longer be a binary.
You will then be able to load the image to a display object or use it anyway you need.
Hope this helps 
Anaqim
Anaqim is basically right. First, all images are already binary files. That’s important because the file.io functions have to be in “binary” mode to work, i.e. “rb”, “wb” for read binary and write binary.
If you have your image file in a place Corona can access it (i.e. system.TemporaryDirectory, system.CachesDirectory, system.DocumentsDirectory or system.ResourceDirectory for non-Android builds) then you just open the file in binary mode, read it in, pump the data in to the mime.b64() function when you can transmit the string data that mime.b64() outputs to your server that needs it. Depending on what you’re doing you may not need to write it back out to a file.
If you’re building Android apps and you want to get an image from the system.ResourceDirectory (your project folder with main.lua) make sure to read the Gotcha section here: https://docs.coronalabs.com/api/library/system/ResourceDirectory.html
Rob
I did this with an audio file so it could be uploaded from a Corona app to our server and then decoded using ASP.Net at the other end.
https://forums.coronalabs.com/topic/44199-example-file-upload-using-aspnet-and-a-progress-bar/
…just in case you’re trying to do something similar.