Avatar of E=mc2
E=mc2
Flag for Canada asked on

Embedding a png image into an html file generated from a Powershell script

I would like to embed a png image into an html file which will then be sent as an attachment to an email.

What code would I use to embed an image such an png or jpg image into an html file?
Does the image have to be pulled externally over the internet or can it be embedded into the html as it were?

A powershell script is being used to create the html file. 
PowershellImages and PhotosHTMLOutlookScripting Languages

Avatar of undefined
Last Comment
E=mc2

8/22/2022 - Mon
Katy Nicholson MVP

You can do either - link to a file hosted on the Internet, or embedded into the HTML.

To embed you'd use a tool to convert the image into Base64 e.g. Base64 Image Encoder | Online Tool (base64-encode.org)  and in your HTML you'd use something like:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAaSURBVDhPY3gro0ISGtVADBrVQAwadBpkVADPEy0Q0V9lSAAAAABJRU5ErkJggg==">

Open in new window


If you've just got a static image you want to use then that's probably the easiest way, otherwise if you've got different images depending on various things and want to do the base64 encoding in PowerShell you'd need something like:

[convert]::ToBase64String((Get-Content -path "your_file_path" -Encoding byte))

Open in new window


which will output the base64 string, to insert into the code above. You'd also need to set the content type in the string accordingly.
E=mc2

ASKER
Thanks Katy, much appreciated.
With regards to the Base 64 encoder, is the image stored on their online database, or is it stored within the algorithm numbers?

ASKER CERTIFIED SOLUTION
Katy Nicholson MVP

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
E=mc2

ASKER
Thanks Katy.
So the image is not saved in any way on anyone else's server?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Katy Nicholson MVP

Generally no (as they delete it after converting) but if you need to guarantee this then use the PowerShell code to generate the base64 data rather than uploading to the web converter.
E=mc2

ASKER
Ok let me consider that then and test it.. many thanks.