ACF Library

base64ImageTag

Back to List

Description: Retrieve an base64 image tag from a file

FileMaker Prototype:

Set Variable [$res; ACF_Run("base64ImageTag"; string_path;  string_alt;  string_style)]

Category: WEB

Function source:

function base64ImageTag ( string path, string alt, string style )
   // Ensure we have a file. 
   if ( path == "") then
      path = select_file ("Choose an image file?", desktop_directory()) ; 
      if (path == "") then
         return ""; 
      end if
   end if
   // Read the content of the file
   int x = open (path, "r");
   string tag, ftype, content = read (x); 
   close (x); 
   
   array string options; 
   // Get file type
   path = substitute ( path, ":", "/");
   path = substitute ( path, "\\", "/");
   ftype = regex_replace("^(.+/)(.+\.)(.+)$", path, "\3");
   // look for options
   if ( alt != "" ) then
      options[] = 'alt="'+alt+'"'; 
   end if
   if (style != "") then
      options[] = 'style="' + style + '"'; 
   end if
   
   // Construct the tag. 
   tag = format ('<img src="data:image/%s;base64,%s" %s/>', ftype, base64_encode(content,1), implode(" ", options)); 
   
   return tag; 
end

The base64ImageTag Function creates an base64 image tag from file content. If the path is empty, the function asks the user for a file. The alt parameter provides a text description of the image for accessibility and search engines. The style parameter is a semicolon separated list of style applied to the image.

Example

Set Variable [$res; ACF_Run("base64ImageTag"; "/path/to/file";  "My Alt Text";  "width=200;heigth=300")]

The result:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAy........"  alt="My Alt Text" style="width=200;heigth=300"/>

Use case

  • For use in Web-viewer, etc. The tag is not dependant of the file anymore. You has to have the file available when constructing the tag, but the result can be stored in a text field, to example.
  • If you need to create a single-file HTML document, images can be incorporated using base64 image tags. The HTML document then becomes "stand alone", for sending with e-mail, etc.
Back to List