Xbasic
base64EncodedImage Function
IN THIS PAGE
Syntax
c base64EncodedImage(c imagefilename)
Arguments
- imagefilenameCharacter
The full or relative path to an image file to encode.
Returns
- resultCharacter
A Data URI string in the format data:image/<extension>;base64,<data>. Returns an empty string if the file does not exist.
Description
Returns a Data URI string for an image file by reading the file, Base64-encoding its bytes, and prefixing the correct image MIME header based on the file extension.
How It Works
The function verifies the file exists, loads it as a blob, encodes the blob as Base64, derives the extension for the MIME type segment, and concatenates the final Data URI string.
Example
Convert a PNG file to a Data URI
dim uri as c
uri = base64EncodedImage("c:\images\logo.png")
if uri <> "" then
? left(uri,60) + "..."
else
? "Image file not found."
end ifLimitations
Per source note: X.