Jump to content

Script to scan a folder and embed images into html as base64


Recommended Posts

I'm trying to get web pages into a database, there isn't any storage available to store the images (among other problems) so I'm being forced to convert the images to base64 and include them into the web page as encoded text. 

I started with the script on '?do=embed' frameborder='0' data-embedContent>> and soon found the base64 encoding function on  to do it all inside autoit. My problem is I need to be able to scan a folder, look for the image tags inside the html to find the image and replace the image tag with the base64 data. 

So far as I can tell, the image tags are the only one with the SRC attribute as there aren't any javascripts so it should be possible to Regexp for src="images/image.gif" and get the filename.

This is what I have so far:

#include"Base64.au3"
$file = FileOpenDialog("Base64 Image Encoder", "", "Images (*.png;*.gif;*.jpg;*.jpeg;*.bmp;*tif;*.tiff)", 1)
If @error Then Exit
$tmp = @ScriptDir & "\temp.txt"

$type = StringSplit($file, ".")
$name = StringSplit($type[$type[0] - 1], "\")
$name = $name[$name[0]]
$type = $type[$type[0]]
FileOpen($file, 16)
Local $sFileRead = FileRead($file)
$Encrypt = _Base64Encode($sFileRead)
FileClose($file)
$out = '<img src="data:image/' & $type & ';base64,' & $Encrypt & '"/>'

$loc = StringReplace($file, $name & "." & $type, "") & $name & "[" & $type & "].html"
FileWrite($loc, $out)

it does an OK job of opening a single image and then saving a html with the image data, but scanning a folder (recursively), pulling the reference and then injecting the code back in is beyond me.

I've attached some example files

base64_example.zip

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...