ACF Library

EmbedHTMLImages

Back to List

Description: Embed images in HTML document as Base64 image tags

FileMaker Prototype:

Set Variable [$res; ACF_Run("EmbedHTMLImages"; string_path)]

Category: WEB

Function source:

function EmbedHTMLImages ( string path )

   //    <img src="https://cdn2.hubspot.net/hubfs/53/tools/email-signature-generator/icons/email-icon-2x.png" color="#f86295" alt="emailAddress" width="13" style="display: block; background-color: rgb(248, 98, 149);">
    // 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 src, ftype, imgCont, content = read (x); 
    close (x); 
   // Extract the images
   array string imgtags = regex_extract ("<img.*?>", content);
   int noTags = sizeof ( imgtags); 
   int i; bool skip; 
   xml tag; 
   JSON xx1; 
   for (i=1, noTags)
      clear (tag); 
      tag = imgtags[i]+"**</img>"; 
      xx1 = XMLattributes ( tag["img"]);
      src = xx1["intermediateRoot.src"];    
      skip = false;    
      case
         : ( left(src, 4) == "http") 
         print "Getting image....\n"; 
         imgCont = http_get(src); 
         
         : ( left(src,4) == "file") // file://
         print "Opening image....\n"; 
         x = open (substring(src,7) ,"r"); 
         imgCont = read(x); 
         close (x);
      default
         skip = true; 
      end case
      if ( ! skip ) then
         ftype = regex_replace("^(.+/)(.+\.)(.+)$", src, "\3");
         imgCont = format('"data:image/%s;base64,%s"',ftype, base64_encode(imgCont,1)); 
         content = substitute ( content, '"'+src+'"', imgCont); 
      end if
   end for
   x = open ( path+".converted.html", "w"); 
   write ( x, content); 
   close (x); 
   return "OK"; 
   
end

The EmbedHTMLImages function replaces HTML image tags with embedded images, enabling the HTML file to be used without external image references—ideal for offline access.

This function uses extract_regex to retrieve all image tags in the document. For each tag, it extracts the src attribute and retrieves the image using http_get. The image content is then base64-encoded and converted into a base64 image tag. Finally, the function substitutes the URL in the HTML content, repeating the process until all image tags have been embedded.

The updated HTML is saved to disk as a new file.

Example

Set Variable [$res; ACF_Run("EmbedHTMLImages"; "~/Desktop/oles-signature.html")]

This results in a file on the desktop called oles-signature.html.converted.html.

In Apple Mail, you can incorporate this converted HTML by editing the file located at:

~/Library/Mail/V10/MailData/Signatures/

Each signature file has a UUID-style name. Replace the HTML content in the appropriate file with the new, converted HTML, then close and lock the file to prevent Apple Mail from altering it. Now, when sending email, the client will not need to pull images from external sites.

An image tag like this:

<img src="https://cdn2.hubspot.net/hubfs/53/tools/email-signature-generator/icons/email-icon-2x.png" color="#f86295" alt="emailAddress" width="13" style="display: block; background-color: rgb(248, 98, 149);">

is changed to this:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAMCAQAAAD8fJRsAAAAcUlEQVR42pWPwQmAMBAErwAFH9bhR7SAqC/BFlJQCvUhSFLBeERFDeThLhy7exzLCRlIfuGoqRLWOKGkp5MX6JWlCgweE7PUsRAwVxxYnmPLhmdWelX2jgd2GiZW5UijbhQ5y9tPeRvLcToSUODk9+cHAaCuO/P2W6cAAAAASUVORK5CYII=" color="#f86295" alt="emailAddress" width="13" style="display: block; background-color: rgb(248, 98, 149);">

And here is the final view from the mail program displaying the embedded image.

Screenshot of Embedded Image in Mail

Back to List